2016-10-06 110 views
0

我被困 - 我使用SQL並希望結果填充到列表框中。我曾嘗試以下:使用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 

上述每個倒退沒有錯誤的,所以不能確定我要去的地方錯了

任何幫助表示讚賞。

+0

創建使用GUI(忘了'TOP 5'現在,一切是可行的與GUI)的查詢。看看你得到了什麼結果。一旦你在GUI中工作,複製粘貼SQL代碼表單並添加'TOP 5' – SunKnight0

+0

另外,是否有一個原因,你試圖在VBA中做到這一點,而不是在設計模式中分配'RouSource'?在VBA中,在設置「RowSource」之後,您將不得不「查看」列表框,在看到任何更改之前。 – SunKnight0

回答

-1

馬丁格林有這些事情的一些很好的例子。

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/