Results 1 to 4 of 4
  1. #1
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538

    Query Run From Form Using VBA to set criteria from List Box

    I am running a query with criteria established in a search form. Additionally, the client wishes to select mulitple locations. I have employed a list box (with 66 locations).



    The following code when started with a command button does nothing. No error message no query opened. Nothing. It does not seem to pass the strCriteria from the list box to the SQL statement.

    The SQL statement when employed in a query and the Div criteria manually entered runs without issue.

    What am I missing?

    Code:
    Option Compare Database
    
    
    Private Sub cmdAllProduct_Click()
        'Define variables
        Dim db As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim varItem As Variant
        Dim strCriteria As String
        Dim strSQL As String
        
        If IsNull(Me.txtStart) Or IsNull(Me.txtEnd) Then
        MsgBox "You need to enter valid Start and End Dates"
        End If
        Exit Sub
        
        'Establish variables
        Set db = CurrentDb()
        Set qdf = db.QueryDefs("qFI71NoProdFilter")
        
        'Determine Criteria from list box selections
        For Each varItem In Me!List0.ItemsSelected
            strCriteria = strCriteria & "," & Me!List0.ItemData(varItem)
        Next varItem
        
        'Test if selections have been made.  If none, exit routine
        If Len(strCriteria) = 0 Then
            MsgBox "You did not select anything from the list" _
                , vbExclamation, "Nothing to find!"
            Exit Sub
        End If
        
        'Remove the leading comma from the criteria set above
        strCriteria = Right(strCriteria, Len(strCriteria) - 1)
        
        'Create Query using SQL
        strSQL = "SELECT tblSource.[Rpt Name], t_DIV.BRNCH_CD, tblSource.Div, t_DIV.BRNCH_NM, t_DIV.RGN_NM,"
        strSQL = strSQL & "tblSource.TRANSACTION, tblTranXCode.[Transaction Description], tblTranXCode.GLAccount, tblSource.ClassCode,"
        strSQL = strSQL & "[Class Table].[Class Name], tblSource.[TRANS DT], tblSource.[PROD NBR], tblSource.DESCRIPTON, tblSource.STS, tblSource.[SLS UOM],"
        strSQL = strSQL & "tblSource.CASE, tblSource.Each, tblSource.QUANTITY, tblSource.[UNIT COST], tblSource.[EXTND COST]"
        strSQL = strSQL & "FROM ((tblSource LEFT JOIN [Class Table] ON tblSource.ClassCode = [Class Table].[Class Code]) LEFT JOIN t_DIV ON tblSource.Div = t_DIV.BRNCH_ID) LEFT JOIN tblTranXCode ON tblSource.TRANSACTION = tblTranXCode.TRANSACTION"
        strSQL = strSQL & "WHERE (((tblSource.[TRANS DT]) Between [Forms]![frmSearch].[txtStart] And [Forms]![frmSearch].[txtEnd]) AND tblSource.Div IN(" & strCriteria & "));"
        
        qdf.SQL = strSQL
        
        'Run Query
        DoCmd.OpenQuery "qFI71NoProdFilter"
        
        'Clear Memory
        Set db = Nothing
        Set qdf = Nothing
        
        
    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The following jumped out at me.

    Code:
        If IsNull(Me.txtStart) Or IsNull(Me.txtEnd) Then
        MsgBox "You need to enter valid Start and End Dates"
        End If
        Exit Sub
    Perhaps an end if or else is in order. Is there a need for further analysis?

  3. #3
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    I see what you mean. Need to put the Exit Sub before the End if.

    Thanks much. Whew! This has been killing me as it was working before I put the test for null strings in yesterday and then not working today. Good eyes!!

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    A fresh set of eyes are nice when yours are burning from the screen radiation!

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

Similar Threads

  1. Code for using a list box for criteria in a form
    By tomnsd in forum Programming
    Replies: 7
    Last Post: 01-23-2013, 10:52 PM
  2. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  3. Query Criteria Function and Operator List?
    By samanthaM in forum Access
    Replies: 2
    Last Post: 06-03-2012, 04:52 PM
  4. Multi-Select List Box as Criteria in Query
    By broadwat in forum Queries
    Replies: 6
    Last Post: 09-19-2011, 07:47 AM
  5. Replies: 6
    Last Post: 06-29-2010, 09:56 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