关于“对象打开时,不允许操作”的问题
--------------------编程问答-------------------- 是哪一行出错? --------------------编程问答--------------------'建议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
为什么还是不行的?
你可以把修改过的代码完整写出来吗? --------------------编程问答-------------------- 需要在“工程”->“引用”—> ADO控件(Microsoft ActiveX Data Objects x.x Library )
Option Explicit--------------------编程问答-------------------- 实在看不惯rs!字段
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
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
补充:VB , 数据库(包含打印,安装,报表)