使用SQL填充列表框

时间:2016-10-06 10:04:32

标签: sql ms-access access-vba

我被困 - 我使用SQL并希望结果填充在列表框中。我尝试过以下方法:

AdvisorOverall = "SELECT TOP 5 tbl_CallLogs.Advisor, Count(tbl_CallLogs.[Date Escalated]) AS [CountOfDate Escalated]" & _
                "FROM tbl_CallLogs" & _
                "GROUP BY tbl_CallLogs.Advisor" & _
                "HAVING (((tbl_CallLogs.Advisor) <> ""))" & _
                "ORDER BY Count(tbl_CallLogs.[Date Escalated]) DESC;"

也尝试过:

me.list3.rowsource = "SELECT TOP 5 tbl_CallLogs.Advisor, Count(tbl_CallLogs.[Date Escalated]) AS [CountOfDate Escalated]" & _
                "FROM tbl_CallLogs" & _
                "GROUP BY tbl_CallLogs.Advisor" & _
                "HAVING (((tbl_CallLogs.Advisor) <> ""))" & _
                "ORDER BY Count(tbl_CallLogs.[Date Escalated]) DESC;"

也尝试过:

me.list3.rowsource = AdvisorOverall

以上每一项都没有错误,所以不确定我哪里出错了

感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

Martin Green有很多很好的例子。

Private Sub UserForm_Initialize()
    On Error GoTo UserForm_Initialize_Err
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source=C:\Databases\StaffDatabase.mdb"
    rst.Open "SELECT DISTINCT [Department] FROM tblStaff ORDER BY [Department];", _
             cnn, adOpenStatic
    rst.MoveFirst
    With Me.ComboBox1
        .Clear
        Do
            .AddItem rst![Department]
            rst.MoveNext
        Loop Until rst.EOF
    End With
UserForm_Initialize_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
UserForm_Initialize_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume UserForm_Initialize_Exit
End Sub

在这里查看他的网站。

http://www.fontstuff.com/vba/