Results 1 to 7 of 7
  1. #1
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150

    Set listbox row source in VBA but results always blank

    Hello,

    I was wondering if anyone can tell me what's wrong with the following code. It looks right to me, but when I execute it the listbox always shows up empty. I've attached a screenshot of the underlying query containing dummy data.



    Code:
    Private Sub cboFilterStatus_AfterUpdate()
        Me.lstBlankets.RowSource = build_SQL_query()
        Me.lstBlankets.Requery
    End Sub
    
    
    Private Sub cboFilterType_AfterUpdate()
        Me.lstBlankets.RowSource = build_SQL_query()
        Me.lstBlankets.Requery
    End Sub
    
    
    
    
    Function build_SQL_query() As String
        
        Dim SQL As String
        
        ' Always include the blanket name filter
        SQL = "SELECT * FROM gryCurrentBlanketStatus WHERE [BlanketName] LIKE *" & [Forms]![frmBlankets]![txtFilterName] & "*"
    
    
        ' If the type is not null include this
        If Not IsNull(Me.cboFilterType) Then
            SQL = SQL & " AND [Type]='" & Me.cboFilterType & "'"
        End If
        
        ' If the status is not null, filter on this
        If Not IsNull(Me.cboFilterStatus) Then
            SQL = SQL & " AND  [LastOfStatus1]=" & Me.cboFilterStatus
        End If
    
    
        build_SQL_query = SQL
    End Function
    Click image for larger version. 

Name:	qryCurrentBlanketStatus.PNG 
Views:	20 
Size:	17.0 KB 
ID:	47411

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Have you tried with Debug.Print the sql string and paste that into a query window and try and execute that?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you try this (you don't need the .Requery as setting the RowSource would requery the list box anyway):
    Code:
    Function build_SQL_query() As String    
        Dim sSQL As String
        
        ' Always include the blanket name filter
        sSQL = "SELECT * FROM gryCurrentBlanketStatus WHERE [BlanketName] LIKE '*" & [Forms]![frmBlankets]![txtFilterName] & "*'"
    
    
    
    
        ' If the type is not null include this
        If Not IsNull(Me.cboFilterType) Then
            sSQL = sSQL & " AND [Type] = '" & Me.cboFilterType & "'"
        End If
        
        ' If the status is not null, filter on this
        If Not IsNull(Me.cboFilterStatus) Then
            sSQL = sSQL & " AND  [LastOfStatus1] = " & Me.cboFilterStatus
        End If
    
    
    
    
        build_SQL_query = sSQL
    End Function
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @Robyn_P,

    Be aware that:
    Code:
    SQL = SQL & " AND [Type]='" & Me.cboFilterType & "'"
    "Type" is a reserved word (Field property) in Access





    Code:
    Dim SQL As String
    "SQL" is a reserved word in Access (JET reserved (kb248738);ODBC (kb125948);ANSI-92 Reserved (kb287417))

    Note: Vlad changed it to
    Code:
    Dim sSQL As String
    in his example....

  5. #5
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Thanks everyone. I'll replace the reserved words and remove the re-queries and get back to you

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Robyn,
    It was not only the reserved words, I also added single quotes after the Like which you were missing:
    Code:
    LIKE '*" & [Forms]![frmBlankets]![txtFilterName] & "*'"
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Quote Originally Posted by Gicu View Post
    Hi Robyn,
    It was not only the reserved words, I also added single quotes after the Like which you were missing:
    Code:
    LIKE '*" & [Forms]![frmBlankets]![txtFilterName] & "*'"
    Cheers,
    Yes, that was the actual problem. Thanks for spotting that

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

Similar Threads

  1. Blank Spaces in Listbox
    By gatsby in forum Access
    Replies: 3
    Last Post: 03-11-2015, 04:45 AM
  2. Replies: 4
    Last Post: 06-18-2014, 11:47 AM
  3. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  4. Requery Does Not Update Listbox Row Source
    By szucker1 in forum Forms
    Replies: 7
    Last Post: 02-11-2014, 08:58 AM
  5. Replies: 1
    Last Post: 03-23-2012, 12:15 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