用ADO对象加载mdb数据库出错
最近用到ListView控件,用ADO加载access数据库文件出错,请大虾给予指点.Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
Dim i As Integer
Dim itmX As ListItem '声明一个ListItem对象
Dim MyIcon As Integer '声明一个整型变量
Dim Mystr, MyMsg As String '声明字符串变量
Private Sub Command4_Click()
Unload Me
End Sub
Private Sub Form_Load()
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Exp.mdb;Persist Security Info=False"
rs1.Open "tb_user", cnn, adOpenKeyset, adLockOptimistic '打开数据对象
For i = 4 To Check1.UBound
'向控件中添加字段
Check1(i).Caption = rs1.Fields(i).Name
Next i
rs1.Close
rs2.Open "select * from tb_user", cnn, adOpenStatic
If rs2.RecordCount > 0 Then
rs2.MoveFirst
'向ListView中添加图片
Do While rs2.EOF = False
Mystr = rs2.Fields("user_name")
MyIcon = Val(Right(rs2.Fields("user_tx"), Val(Len(rs2.Fields("user_tx")) - 2)))
Set itmX = ListView1.ListItems.Add(, , Mystr, MyIcon)
rs2.MoveNext
Loop
End If
rs2.Close
End Sub
Private Sub Command1_Click() '全选
For i = 4 To Check1.UBound
Check1(i).Value = 1
Next i
End Sub
Private Sub Command2_Click() '全不选
For i = 4 To Check1.UBound
Check1(i).Value = 0
Next i
End Sub
Private Sub Command3_Click() '为操作员设置权限
Adodc1.RecordSource = "select * from tb_user where user_name ='" + ListView1.SelectedItem + "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
For i = 4 To Check1.UBound
Adodc1.Recordset.Fields(i) = Check1(i).Value
Next i
Adodc1.Recordset.Update
Adodc1.Refresh
MsgBox "操作员权限设置成功!", , "信息提示"
End If
End Sub
Private Sub ListView1_Click() '查看操作员权限
Adodc1.RecordSource = "select * from tb_user where user_name ='" + ListView1.SelectedItem + "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
For i = 4 To Check1.UBound
If Adodc1.Recordset.Fields(i) = "" Then
Check1(i).Value = 0
Else
Check1(i).Value = Adodc1.Recordset.Fields(i)
End If
Next i
Adodc1.Recordset.Update '更新数据记录
Adodc1.Refresh
End If
End Sub
Public Function cnn() As ADODB.Connection '定义函数
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Exp.mdb;Persist Security Info=False"
End Function
运行后提示:实时错误'-2147024770(8007007e)':自动化错误
这两条语句不能执行
Set cnn = New ADODB.Connection
Adodc1.Refresh