Private Sub Form_Load() On Error GoTo ErrRstEmp
     
    Dim strSql As String
     
    strSql = "select empid,empcode,name ,"
    strSql = strSql + "department=case when department is null  then ''else   (select name from Department where id=department)  end ,"
    strSql = strSql + "duty=case when duty is null then '' else duty  end,"
    strSql = strSql + "countmeth=case when countmeth='True ' then '是'  else '否'  end ,"
    strSql = strSql + "isoperate=case when Isoperate='True ' then '有'  else '无'   end,"
    strSql = strSql + "sex=Case  when sex ='True ' then   '男'  else '女'  end ,"
    strSql = strSql + "birthday=case when birthday is Null then null  else birthday  end,"
    strSql = strSql + "email=case when email is null  then ' '  else email   end ,"
    strSql = strSql + "culture=case when culture is null then ' '  else  culture  end ,"
    strSql = strSql + "postdate=case when postdate is null then null else postdate  end,"
    strSql = strSql + "forefather=case when forefather is null then ' 'else forefather end"
    strSql = strSql + "  " + "From Employ"
     
    ' MsgBox strSql
     Set rstEmp = New ADODB.Recordset
     rstEmp.CursorLocation = adUseClient
     rstEmp.Open strSql, conn, adOpenDynamic, adLockBatchOptimistic, adCmdText
      
     If rstEmp.RecordCount = 0 Then GoTo ErrRstEmp
       
     Set dgdEmp.DataSource = rstEmpend subPrivate Function SaveNewEmp() As Boolean
'保存新增'On Error GoTo AddEmpErr    Dim varIndex As Variant
    Dim nID As Integer
    Dim rstID As New ADODB.Recordset
    
    Set rstID = New ADODB.Recordset
    
    rstID.Open "select Max(empid) as empid from employ ", conn, adOpenKeyset, adLockOptimistic, adCmdText
    
    If rstID.RecordCount = 0 Then     '数据库中无数据
       nID = 1
       varIndex = 1
    Else
       nID = rstID.Fields("empid").Value + 1       '最大的id +1
       varIndex = nID
    End If
                      
    rstEmp.AddNew
    rstEmp!empid = nID
    rstEmp!empcode = Trim(txtEmpCode.Text)
    rstEmp!Name = Trim(txtEmpName.Text)
      
     If cboDept.ListIndex = -1 Then
         'rstEmp!department = ""
      Else
         rstEmp!department = cboDept.Text '出现上述错误
     End If
               ’==========================以下也同样出现错误     If cboSex.ListIndex = 0 Then
        rstEmp!sex = "True "        
     ElseIf cboSex.ListIndex = 1 Then
        rstEmp!sex = "False"
     End If
     
     If chkOperate.Value = 1 Then
       rstEmp!isoperate = "True"
     ElseIf chkOperate.Value = 0 Then
       rstEmp!isoperate = "False"
     End If     If chkWork.Value = 1 Then
       rstEmp!countmeth = "True"
     ElseIf chkWork.Value = 0 Then
      rstEmp!countmeth = "False"
     End If
         
     rstEmp.UpdateBatch            '提交到数据库
     
     rstEmp.MoveLast
     
     SaveNewEmp = True
     Set rstID = Nothing
  
  Exit Function
    
AddEmpErr:        SaveNewEmp = False
        Call SQLError(conn)
        rstEmp.CancelBatch
        rstEmp.MoveLast
        Set rstID = Nothingend function

解决方案 »

  1.   

    rstID 没有close 吧!
    在Set rstID = Nothing 前面加一个rstID.close 再看看??
      

  2.   

    rstEmp!department = cboDept.Text '出现上述错误
    字段是不是够长,如果用汉字最好用nvarchar类型。
      

  3.   

    哎,没说清楚,我经常见过这种问题,都是用断点调试搞定的。
    你把On Error GoTo ErrRstEmp这句先去掉,运行就会出现err.Number及Err.Description
      

  4.   

    rstEmp!department = cboDept.Text '出现上述错误
    把字段department的長度加長一點,就是不夠長
      

  5.   

    你把你生成表的SSQL语句贴出来看看~这样的错误一般是你在某个字段写入了错误的数据导致的
      

  6.   

    你试着用语句插入~应该没问题~是你的ADO的SQL语句有问题~你这样写语句本身没问题,但是把里面的值固定了
      

  7.   

    还没解决吗?就是你把你ADO里面CASE 那些东西去掉保存就没问题了。你非要的话就直接用INSERT INTO 插入
      

  8.   

    就想当于用"SELECT ID=1 FROM ABC"打开,ID这个字段只能写进1一样的道理
      

  9.   

    不用SQL 语句 就不行吗
      

  10.   

    你先试试不要CASE后面的行不行
      

  11.   

    语句本身没错,但是新增的时候会出错。对,如果不用SQL Insert 
    怎么解决这个问题
      

  12.   

    你写那条语句的时候的目的是想把比如TRUE显示成"男"之类的。我想你就"select * from 表",然后用代码来做把TRUE 显示成"男",虽然麻烦些~但肯定没问题。
      

  13.   

    或者你打开另一个记录集来新增,否则应该是没办法的。因为你那样写就相当于用"SELECT ID=1 FROM ABC"打开,ID这个字段只能写进1一样的道理。就是说把那个值写固定住了~写进去都会出错。
      

  14.   

    个人认为用SQL INSERT INTO 插入比较好~你的改动比较少。
      

  15.   

    数据直接帮定到DataGrid中的!!!!!!!!!!!!!!!!!!!!!!!
    只有 从数据库中直接提取 true 通过select sex case when sex='True' then '男' else '女' end  from employ   再绑定到DataGrid 上   注意:sex 是 Boolean 型的
      
      

  16.   

    我不是用的DGRID,我们的网格可以在网格中实现这样的功能。那你再打开一个记录集去做吧。我看你是用的批处理,用SQL语句不行。
      

  17.   

    只能用SQL insert  
    揭贴
      

  18.   

    哎,你用以下语句看看:
    if 条件成立 then
    rs.布尔字段=1
    else
    rs.布尔字段=0
    end if
    如果向你所写的那样,可能系统会认为你将要赋值为一个长度为4的字符串了