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
36 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:ScootterP
  • 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:ScootterP
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 57

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:ScootterP
ID: 41907921
Thanks, works now.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

816 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

13 Experts available now in Live!

Get 1:1 Help Now