?
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
Medium Priority
?
65 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 1000 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 1000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

719 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