求助:我的数据库联接出现 实时错误3265 在对应所需名称或序数的集合中,未找到项目
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Persist Security Info=True;Data Source= " & App.Path & "\strdatabase.mdb "
Set cn = New ADODB.Connection
cn.Open strCnn
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockReadOnly
rs.ActiveConnection = cn
rs.Open "strdatatablename ", cn, , , adCmdTable
rs.MoveFirst
Do While Not rs.EOF
field_value1 = rs.Fields(cborun.Item(0).Text).Value
field_value2 = rs.Fields(cborun.Item(1).Text).Value
rs.MoveNext
Call foundfile
If rs.EOF Then
MsgBox "数据库已经查找结束 ", vbOKOnly, "完成 "
rs.Close
cn.Close
Exit Sub
End If
Loop
调试错误出在field_value1 = rs.Fields(cborun.Item(0).Text).Value这里,错误提示是实时错误3265 在对应所需名称或序数的集合中,未找到项目.cborun.Item(0)这是一个combox对象,这个控件读的是数据库里面的一个字段.在执行这上面这段代码前首先要执行了下面的代码:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Password=;Persist Security Info=True;Data Source= " & App.Path & "\strdatabase.mdb "
Set cn = New ADODB.Connection
cn.Open strCnn
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockReadOnly
rs.Open "strdatatablename ", cn, , , adCmdTable
Dim fldEach As ADODB.Field
For Each fldEach In rs.Fields
cborun.Item(0).AddItem fldEach.Name
Next
For Each fldEach In rs.Fields
cborun.Item(1).AddItem fldEach.Name
Next
rs.Close
cn.Close
MsgBox "数据库已经联接成功 ", vbOKOnly, "数据库联接 "
就将数据库的字段赋给combox控件了,combox控件就选择的是表里面的字段.
下面这段代码的数据库能成功联接.
[解决办法]
在
field_value1 = rs.Fields(cborun.Item(0).Text).Value
field_value2 = rs.Fields(cborun.Item(1).Text).Value
前分别加:
if not null(rs.Fields(cborun.Item(0).Text))
在其后分别加:
Endif
试试看
[解决办法]
cborun.Item(0)中有默认选项吗?
运行出错时,你看看cborun.Item(0).Text值是多少??
[解决办法]
解决啦!
恭喜哈!
接分!