[英]Populate Listbox using ADO Recordset
Below is my Code for POpulating ListBox using ADO Recordset . 以下是我使用ADO Recordset填充ListBox的代码。 It is working but not in a way it should be display in form.
它正在工作,但不能以某种形式显示。
Function PopulatelstReview()
Dim sList As String
Dim i As Long
Dim ADORs As ADODB.Recordset
Dim strSearchResult As String
Set AdoCn = New ADODB.Connection
Set AdoCmd = New ADODB.Command
AdoCn.Open AdoConnectionString
AdoCmd.ActiveConnection = AdoConnectionString
Debug.Print AdoConnectionString
Set ADORs = New ADODB.Recordset
'rs.Open "TestListReview", AdoCn
ADORs.CursorLocation = adUseClient
'rs.Open strSQL, AdoConnectionString, adOpenDynamic, adLockOptimistic, adCmdText
AdoCmd.CommandType = adCmdStoredProc
AdoCmd.CommandText = "TestListReview"
Set rs = AdoCmd.Execute
Set ADORs = ExecuteStoredProcedure("TestListReview", AdoCmd)
With ADORs
lstReview.RowSourceType = "Value List"
lstReview.ColumnHeads = False
Label43.Caption = ADORs.Fields(0).Name
Label44.Caption = ADORs.Fields(1).Name
Label45.Caption = ADORs.Fields(2).Name
Label48.Caption = ADORs.Fields(3).Name
Label49.Caption = ADORs.Fields(4).Name
Label50.Caption = ADORs.Fields(5).Name
Label51.Caption = ADORs.Fields(6).Name
Label52.Caption = ADORs.Fields(7).Name
lstReview.AddItem ADORs!id
lstReview.AddItem ADORs!TradePartner
lstReview.AddItem ADORs!TrustAccount
lstReview.AddItem ADORs!Date
lstReview.AddItem ADORs!CurrentBalance
lstReview.AddItem ADORs!FileName
lstReview.AddItem ADORs!RecordNum
lstReview.AddItem ADORs!ImportDateTime
sList = ADORs.GetString(adClipString, , ";", ";")
'lstReview.RowSourceType = "Value List"
'lstReview.RowSource = sList
End With
'Call RS2WS(ADORs, "A3")
'lstReview.Column(0, 1) = "ID"
End Function
I need that each value should come under appropiate columns which i placed in labels.please tell me how can i do this 我需要每个值都应该放在我放置在标签中的适当列下。请告诉我我该怎么做
Are you using Excel or Access? 您正在使用Excel还是Access? if you are using Access you could import/linked table and use normal queries as (recordsource) to populate list boxes.
如果您使用的是Access,则可以导入/链接表并将正常查询用作(记录源)来填充列表框。
anyhow, here is a pseudo code to populate a listbox using your method. 无论如何,这是使用您的方法填充列表框的伪代码。 After you have connected to the server and executed your SQL statement try following codes.
连接到服务器并执行SQL语句后,请尝试以下代码。
Dim RC, CC As Long
ADORs.MoveLast ' vba will not know the total row until you scroll down to the last
RC = ADORs.RecordCount ' get the result row count
CC = ADORs.Fields.count ' get the field/column count
ADORs.MoveFirst ' Move back to the first record
lstReview.ColumnHeads = True
lstReview.ColumnCount = CC ' Number of column as in the recordset
'Fill the column names
Dim I As Integer
For I = 0 To CC - 1
lstReview.Column(I, 0).value = ADORs.Fields(I).name
Next I
'Fill the data
Dim J As Integer
While Not ADORs.EOF
For I = 1 To RC - 1
For J = 0 To CC - 1
lstReview.Column(J, I).value = ADORs.Fields(J).value
Next J
Next I
ADORs.MoveNext 'moving to next record
Wend
Also you should consider trapping errors such as if the SQL execution has returned any records. 您还应该考虑捕获错误,例如SQL执行是否返回了任何记录。
for access you can use this 访问,您可以使用此
If Nz(ADORs.RecordCount, 0) = 0 Then
MsgBox "Sorry no records found.."
Exit Sub
End If
if you are using excel you can use something like this 如果您使用的是excel,则可以使用类似的内容
If isNull(ADORs.RecordCount) or (ADORs.RecordCount = 0) Then
MsgBox "Sorry no records found.."
Exit Sub
End If
hope this helps.. enjoy :) 希望这可以帮助..享受:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.