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
15 Views
Last Modified: 2016-11-30
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:ScootterP
  • 2
4 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
Comment Utility
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:ScootterP
Comment Utility
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 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
Comment Utility
<<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:ScootterP
Comment Utility
Thanks, works now.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now