Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 75
  • Last Modified:

Access db, trying to list the results of query (in a recordset) in a list box using VBA

I am using VBA in Access 2010.

I am trying to show the results of a query in a list box.  I am putting the data into a recordset and then using a function to put it into the list box.  I have used the code before many time with no problem, but I decided to modify the codes a bit to use DAO.  

The problem is it only grabs the first row.  The record count for the query is 87 rows.  Not sure why it is not pulling all of them.  The code is below.


Private Sub cmdFindBatch_Click()
    Dim rst As DAO.Recordset
    Dim db As DAO.Database
    Dim strSQL As String
    Dim strList As String
                     
    strSQL = "SELECT BATCH_NO, DateRan " _
    & "FROM batch LEFT JOIN tblChron ON batch.BATCH_NO = tblChron.BatchDate " _
    & "WHERE Vendor = 'M' " _
    & "ORDER BY BATCH_NO DESC"
   
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strSQL)

    strList = BuildString(rst)
    Me.lstView.RowSource = strList
     
    MsgBox "Finished"

End Sub

Private Function BuildString(rst As DAO.Recordset) As String 'Follows Findbatch
    Dim strReturn As String
    Dim varItems As Variant
    Dim x As Integer
    Dim y As Integer
   
    rst.MoveFirst
   
    varItems = rst.GetRows()
    For x = LBound(varItems, 2) To UBound(varItems, 2)
        For y = LBound(varItems, 1) To UBound(varItems, 1)
            strReturn = strReturn & varItems(y, x) & ";"
        Next y
    Next x
    BuildString = strReturn
 
End Function
0
Scott Palmer
Asked:
Scott Palmer
  • 2
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
why not use the query as the rowsource of the listbox

strSQL = "SELECT BATCH_NO, DateRan " _
     & "FROM batch LEFT JOIN tblChron ON batch.BATCH_NO = tblChron.BatchDate " _
     & "WHERE Vendor = 'M' " _
     & "ORDER BY BATCH_NO DESC"

Me.lstView.RowSource = strSQL
0
 
Scott PalmerData AnalystAuthor Commented:
I tried what you suggested and the results are literally what is in the Select statement.

The results returned were:
SELECT DISTINCT BATCH_NO, DateRan From Batch

This is what I changed the code to:

Private Sub cmdFindBatch_Click()
   Dim strSQL As String
                   
    strSQL = "SELECT DISTINCT BATCH_NO, DateRan " _
    & "FROM batch LEFT JOIN tblChron ON batch.BATCH_NO = tblChron.BatchDate " _
    & "WHERE Vendor = 'M' " _
    & "ORDER BY BATCH_NO DESC"
   
    Me.lstView.RowSource = strSQL      
 
End Sub
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<I tried what you suggested and the results are literally what is in the Select statement.>>

Change the row source type to table/query from value list.

Jim.
0
 
Scott PalmerData AnalystAuthor Commented:
Thanks, works now.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now