【问题标题】:Fill Listbox with Results of SQL Query用 SQL 查询的结果填充列表框
【发布时间】:2014-05-11 13:39:32
【问题描述】:

好吧,如果我已经尝试过,我可能会走得更远,但是就这样吧。我正在尝试执行 SQL 查询以返回一些记录,然后填充列表框。本质上,我希望每条记录都显示在单独的行中。我在一个单独的模块中引用了主表单。我听说过一些关于用 SQL 结果填充数据表然后也将列表框链接到该数据表的事情,但我也不是 100% 确定在那里做什么。我一直在尝试使用每个结果手动更新列表框,但它似乎并不喜欢它。我使用的是记录集而不是数据表,但就像我说的那样,这是因为我不确定如何使用数据表(以前没有做过,但我愿意学习)

Public Sub addCases()
'Uses windows login credentials to determine and return CSP's Manager's Name

'C
Dim i As Integer
Dim intX As Integer


Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Dim strSQL As String, strManager As String
Set c = New ADODB.Connection
c.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Commit Tracker.accdb; Persist Security Info=False;"
strSQL = "SELECT "
    strSQL = strSQL & "* "
strSQL = strSQL & "FROM "
    strSQL = strSQL & "CommitTrk "
strSQL = strSQL & "WHERE "
    strSQL = strSQL & "EMP_ID = '" & Username() & "'"
Set r = c.Execute(strSQL)

If r.EOF = False Then
    intX = 0
    vararray = r.GetRows()
    For i = LBound(vararray) To UBound(vararray)
       With frmCommitViewer.lstCases
           .AddItem
           .List(intX, 0) = r.Index(i)
       End With
       intX = intX + 1
    Next i
End If
r.Close
c.Close
Set r = Nothing
Set c = Nothing


End Sub

【问题讨论】:

  • 它似乎不喜欢它

标签: sql vba listbox ms-access-2010 recordset


【解决方案1】:

我想通了。而不是愚蠢的数组转换,我仍然直接使用记录集。

If r.EOF = False Then
    With frmCommitViewer.lstCases
        .Clear
        Do
            .AddItem r![CASE_ID_NBR]
            r.MoveNext
        Loop Until r.EOF
    End With
End If

超级简单。感谢http://www.fontstuff.com/vba/vbatut10pfv.htm

【讨论】:

  • 不要用AddItem,真的很慢,frmCommitViewer.lstCases.Columns = r.GetRows()可以一次性dump所有记录
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多