好的,我要做的是将ADODB.Recordset分配给VBA中的ListBox。我收到错误:“您输入的对象不是有效的记录集属性”..我在这里做错了什么?
注意:adoConn有效并设置在代码中的其他位置。
Private Sub Form_Load()
' Error Management
On Error GoTo ErrHandler:
Dim adoRS As New ADODB.Recordset
Dim sqlStmt As String
' Create the SQL statement
sqlStmnt = "SELECT GroupName FROM tblGroups"
' Execute the statement
adoRS.Open sqlStmnt, adoConn
' Add items to the lstGroups
If (adoRS.RecordCount <> 0) Then
Set lstGroups.Recordset = adoRS
End If
' Clean up
adoRS.Close
Set adoRS = Nothing
Exit Sub
ErrHandler:
' Clean up
If (adoRS.State = adStateOpen) Then
adoRS.Close
End If
Set adoRS = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
这就是ADO的开放方式
Public Sub openConnection()
' The path to the database
Dim strDBPath As String
strDBPath = "C:\Users\Vincent\****\****\"
' The database name to connect to
Dim strDBName As String
strDBName = "Permissions_be.accdb"
' Full path to the database
Dim strDBFull As String
strDB = strDBPath & "\" & strDBName
' Instantiate an ADO object
Set adoConn = New ADODB.Connection
' Connect to database
With adoConn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Mode = adModeShareDenyNone
.Open (strDBPath & strDBName)
End With
End Sub
更新 因此,如果有人在Access 2010/13上遇到此问题。 - 将列表框设置为值列表。 - 然后在VBA侧循环通过记录集
' Add items to the lstGroups
If (adoRS.RecordCount <> 0) Then
Do While Not adoRS.EOF
' This is how to add two columns to one listbox if you need only
' one then put only the (adoRS.Fields(0))
lstGroups.AddItem (adoRS.Fields(0) & ";" & adoRS.Fields(1))
adoRS.MoveNext
Loop
lstGroups.Requery
End If
答案 0 :(得分:0)
你如何设置adoConn?应该是这样的: -
Dim cnn As ADODB.Connection
Set adoConn= New ADODB.Connection
With adoConn
.Provider = "Microsoft.Access.OLEDB.10.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = "10.******"
.Properties("User ID").Value = "*****readonly"
.Properties("Password").Value = "*****readonly"
.Open
End With
答案 1 :(得分:0)
问题在于这一行
#wrapper{
background-color: white;
width: 940px;
max-width: 2048px;
box-shadow: 3px 3px 5px #333333;
}
如果您查看文档,您会注意到它不是&#34; Recordset&#34;列表中的财产。
ListBox的属性名为List,它接受变量数组。您可以从记录集中获取值并将它们放入数组中,然后放到List上。
像
这样的东西Set lstGroups.Recordset = adoRS
我没有测试任何这个,但它可能会让你走上正轨。
此外,如果你没有让它与列表一起工作,这个链接有一个很好的例子,说明如何逐个添加它们 enter link description here