Solved

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

Posted on 2016-11-30
4
56 Views
Last Modified: 2017-01-25
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
Comment
Question by:Scott Palmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 41907799
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
 

Author Comment

by:Scott Palmer
ID: 41907879
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
 
LVL 58

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 41907902
<<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
 

Author Closing Comment

by:Scott Palmer
ID: 41907921
Thanks, works now.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question