Results 1 to 10 of 10
  1. #1
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73

    Requery Listbox with the following code

    I have limited knowledge on programming Access. I have the following code that populates an access report (rptAllocation) for each client that I select in a list, this works fine:

    Code:
    Private Sub cmdAllocationMonthlyReview_Click()
        Dim i, s As Integer
        Dim strfilter As String
        For i = 0 To Me.List135.ListCount
            If Me.List135.Selected(i) = True Then
                s = s + 1
                If s = 1 Then strfilter = "Client = '" & Me.List135.Column(1, i) & "'"
                If s > 1 Then
                    strfilter = strfilter & " or " & "Client = '" & Me.List135.Column(1, i) & "'"
                End If
            End If
        Next
        If s = 0 Then Exit Sub
        If Len(strfilter) > 255 Then
            MsgBox "You must select fewer clients. Try selecting 5 to 10 at a time."
            Exit Sub
        End If
        
        DoCmd.OpenReport "rptAllocation", acViewPreview, "qryAllocation", strfilter
    End Sub
    I want to modify this code to requery a separate list box, List6, based on the client I have selected in List135. I'm assuming I need to change:
    Code:
    DoCmd.OpenReport "rptAllocation", acViewPreview, "qryAllocation", strfilter
    to perform the requery but I don't know how to reference List6 and include the "strfilter". Any ideas?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can requery a listbox control using the Requery method.
    Me.List6.Requery

    However, it sounds as though you want to change the RowSource property of List6.

    Perhaps you can add your strfilter string to an SQL string and use the result as the RowSource for List6

  3. #3
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    Thanks...so, how exactly would I do that? Currently, List6 rowsource is:
    Code:
    SELECT tblMonthlyReview.Manager AS Mgr, tblMonthlyReview.client, tblMonthlyReview.EquityTarget AS Trgt, tblMonthlyReview.PortfolioModel AS Model, tblMonthlyReview.account, tblMonthlyReview.accountID, tblMonthlyReview.CashReview
    FROM tblMonthlyReview;

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe
    dim strSQL = "SELECT tblMonthlyReview.Manager AS Mgr, tblMonthlyReview.client, tblMonthlyReview.EquityTarget AS Trgt, tblMonthlyReview.PortfolioModel AS Model, tblMonthlyReview.account, tblMonthlyReview.accountID, tblMonthlyReview.CashReview
    FROM tblMonthlyReview"

    strSQL = strSQL & " WHERE " & strfilter
    dubug.print strSQL

    Notice that I removed the semicolon from the original SQL before I concatenated strfilter to it.

  5. #5
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    My apologies but I still don't understand. Where do I add the code you suggested above?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can put that code behind the click event of a Command Button, like your other code that is behind a button. View the result from the Debug.Print line of code in the immediate window (keyboard shortcut to Immediate Window Ctrl + G).

  7. #7
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    So I created a new button, and added the code you suggested to the On Click event of the button but the code doesn't work. This part shows up in red:

    Code:
    dim strSQL = "SELECT tblMonthlyReview.Manager AS Mgr, tblMonthlyReview.client, tblMonthlyReview.EquityTarget AS Trgt, tblMonthlyReview.PortfolioModel AS Model, tblMonthlyReview.account, tblMonthlyReview.accountID, tblMonthlyReview.CashReview
    FROM tblMonthlyReview"

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Sorry - VBA syntax error on my part. Try...

    Code:
    'Previous code to iterate first listbox
        Dim i, s As Integer
        Dim strfilter As String
        For i = 0 To Me.List135.ListCount
            If Me.List135.Selected(i) = True Then
                s = s + 1
                If s = 1 Then strfilter = "Client = '" & Me.List135.Column(1, i) & "'"
                If s > 1 Then
                    strfilter = strfilter & " or " & "Client = '" & Me.List135.Column(1, i) & "'"
                End If
            End If
        Next
        If s = 0 Then Exit Sub
        If Len(strfilter) > 255 Then
            MsgBox "You must select fewer clients. Try selecting 5 to 10 at a time."
            Exit Sub
        End If
     
    'new code for the second listbox RowSource
    Dim strSQL As String
    strSQL = "SELECT tblMonthlyReview.Manager AS Mgr, tblMonthlyReview.client, tblMonthlyReview.EquityTarget AS Trgt, tblMonthlyReview.PortfolioModel AS Model, tblMonthlyReview.account, tblMonthlyReview.accountID, tblMonthlyReview.CashReview FROM tblMonthlyReview"
    strSQL = strSQL & " WHERE " & strfilter
    Debug.Print "strfilter = " & strfilter & vbCrLf
    Debug.Print "strSQL = " & vbCrLf & strSQL
    ''This will be for the requery method after testing
    'Me.LISTBOXNAME.RowSource = strSQL

  9. #9
    UTLee is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    73
    Amazing, it works perfectly! This has made my Form so much more useful! Thanks so much for your patience!

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Not a problem and I apologize for not answering your question earlier. The forum filters caused me to miss your reply. Glad to hear you have things sorted out.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Requery Does Not Update Listbox Row Source
    By szucker1 in forum Forms
    Replies: 7
    Last Post: 02-11-2014, 08:58 AM
  2. Requery ListBox on Mainform from Subform
    By Ruegen in forum Forms
    Replies: 11
    Last Post: 10-29-2013, 09:27 PM
  3. Replies: 19
    Last Post: 11-01-2012, 08:03 AM
  4. listbox code
    By dadas in forum Access
    Replies: 3
    Last Post: 10-27-2011, 12:44 AM
  5. after DAO update, listbox.requery not refreshing
    By EuniceH in forum Programming
    Replies: 2
    Last Post: 10-21-2011, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums