帮我看看这个代码,CASE 1后的代码有什么问题,那个是查找代码,查第二个的时候会提示对象打开时,不允许操作,而我在CASE 1里加了RS.CLOSE也不行,代码如下:
Option Explicit
Dim a, b As String
Private Sub command1_click(index As Integer)
a = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\VB\员工\db1.mdb;Persist Security Info=False"
b = "select*from 员工资料"
cn.ConnectionString = a
Select Case index
Case 0
cn.Open
rs.Open b, cn, 3, 3
rs.AddNew
rs!工号 = Text1.Text
rs!姓名 = Text2.Text
If Option1.Value = True Then
rs!性别 = "0"
Else
rs!性别 = "1"
End If
rs!年龄 = Val(Text3.Text)
rs!工龄 = Val(Text4.Text)
rs!职称 = Text5.Text
rs!部门 = Text6.Text
rs.MoveLast
rs.Update
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
rs.Close
cn.Close
Case 1
If Text1.Text <> "" Then
b = "select*from 员工资料 where 员工资料.姓名='" & Text1.Text & "'"
If rs.State = True Then
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
cn.Open
rs.CursorLocation = adUseClient
rs.Open b, cn, 1, 1
 Set DataGrid1.DataSource = rs
   End If
     End If
         End Select
End Sub

解决方案 »

  1.   

    '建议cn在case之前就打开,不需要在每个case中分别打开
    cn.connectionstring=a
    if cn.state<>adstateclosed then cn.close
    cn.open
    select case index
        case 0
            if rs.state<>adstateclosed then rs.close
            ....
            ....
        case 1
            if rs.state<>adstateclosed then rs.close
            ....
            ....
    end select
    cn.close    
      

  2.   

    需要在“工程”->“引用”—>  ADO控件(Microsoft ActiveX Data Objects x.x Library )
    Option Explicit
     Dim a, b As String
     Private Sub command1_click(index As Integer)
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
            
        cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\VB\员工\db1.mdb;persist security info=false")  
        b = "select*from 员工资料"
        
        Select Case index
           Case 0         
             rs.Open b, cn, 3, 3
             rs.AddNew
             rs!工号 = Text1.Text
             rs!姓名 = Text2.Text
              If Option1.Value = True Then
                rs!性别 = "0"
              Else
                rs!性别 = "1"
              End If
             rs!年龄 = Val(Text3.Text)
             rs!工龄 = Val(Text4.Text)
             rs!职称 = Text5.Text
             rs!部门 = Text6.Text
             rs.MoveLast
             rs.Update
             Text1.Text = ""
             Text2.Text = ""
             Text3.Text = ""
             Text4.Text = ""
             Text5.Text = ""
             Text6.Text = ""
             rs.Close
            
          Case 1
             If Text1.Text <> "" Then
                  b = "select*from 员工资料 where 员工资料.姓名='" & Text1.Text & "'"
                 rs.Open b, cn, 3, 3
                 Set DataGrid1.DataSource = rs
                 rs.close
             End If
         End Select
        cn.close
     End Sub
      

  3.   

    实在看不惯rs!字段
    Option Explicit
     Dim  b As String
     Private Sub command1_click(index As Integer)
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
            
        cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\VB\员工\db1.mdb;persist security info=false")  
        b = "select*from 员工资料"
        
        Select Case index
           Case 0         
             rs.Open b, cn, 3, 3
             rs.AddNew
             rs("工号").value = Text1.Text
             rs("姓名").value = Text2.Text
              If Option1.Value = True Then
                rs("性别").value = "0"
              Else
                rs("性别").value = "1"
              End If
             rs("年龄").value = Val(Text3.Text)
             rs("工龄").value = Val(Text4.Text)
             rs("职称").value = Text5.Text
             rs("部门").value = Text6.Text
             rs.MoveLast
             rs.Update
             Text1.Text = ""
             Text2.Text = ""
             Text3.Text = ""
             Text4.Text = ""
             Text5.Text = ""
             Text6.Text = ""
             rs.Close
            
          Case 1
             If Text1.Text <> "" Then
                  b = "select*from 员工资料 where 员工资料.姓名='" & Text1.Text & "'"
                 rs.Open b, cn, 3, 3
                 Set DataGrid1.DataSource = rs
                 rs.close
             End If
         End Select
        cn.close
     End Sub