Results 1 to 9 of 9
  1. #1
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    20

    Search Form Using Combo Box

    Hi All,

    I have this form below and this is working well with the below coding. I just want to add one more category which is [DocStatus]. Can help me please. I've been trying but it really wont run if I add one more field. Thank you guys.
    Click image for larger version. 

Name:	D.jpg 
Views:	31 
Size:	49.9 KB 
ID:	52582




    Function SearchCriteria()
    Dim Dept, strDocType, strStatus As String
    Dim task, strCriteria As String


    If IsNull(Me.cbo_Dept) Then
    Dept = "(Dept) like '*'"
    Else
    Dept = "(Dept) = '" & Me.cbo_Dept & "'"
    End If




    If IsNull(Me.cbo_DocType) Then
    strDocType = "(DocType) like '*'"
    Else
    strDocType = "(DocType) = '" & Me.cbo_DocType & "'ORDER BY tbl_TR.ID ASC"

    End If


    strCriteria = Dept & "And" & strDocType
    task = "Select * from tbl_TR where " & strCriteria
    Me.SubForm_TR.Form.RecordSource = task
    Me.SubForm_TR.Form.Requery
    End Function

  2. #2
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    try this:

    Code:
    Function SearchCriteria()    Dim Dept, strDocType, strStatus As String
        Dim task, strCriteria As String
            
        task = "Select * from tbl_TR"
        
        If Not IsNull(Me.cbo_Dept) Then
           strCriteria = strCriteria & "[Dept] = '" & Me.cbo_Dept & "' and "
        End If
        
        
        If Not IsNull(Me.cbo_DocType) Then
            strCriteria = strCriteria & "[DocType] = '" & Me.cbo_DocType & "' and "      'ORDER BY tbl_TR.ID ASC"
        
        End If
        
        If Len(strCriteria) <> 0 Then
            strCriteria = " " & Left$(strCriteria, Len(strCriteria) - 5)
        End If
        task = task & strCriteria & " ORDER BY tbl_TR.ID ASC;"
        
        Me.SubForm_TR.Form.RecordSource = task
        Me.SubForm_TR.Form.Requery
    End Function
    on design view, add this to cbo_dept and cbo_docType AfterUpdate event:

    Code:
    =SearchCriteria()
    
    Last edited by jojowhite; 01-18-2025 at 07:17 AM. Reason: Replaced parenthesis with square brackets

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    Step through your code to check values, use debug.print to see what you actually have and tell us any error messages you get.

    on the face of it, you need spaces either side of ‘and’

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Further to the advice provided, be aware that:

    Code:
    Dim Dept, strDocType, strStatus As String
    does not define each variable as a string. In this case only strStatus is a string.
    Dept and strDocType will be dimmed as variant.

    With vba you must explicitly dim each variable otherwise the default is variant.

    You could use:

    Code:
    Dim Dept as string, strDocType as string, strStatus As String
    OR
    Code:
    Dim Dept as string
    Dim strDocType as string
    Dim strStatus As String

  5. #5
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    20
    Hi Orange,

    So I have this search form below. My search is based on what I enter in [Dept], [Doctype] and [PIC]. I followed what you have said but i have error below. Please help.

    Click image for larger version. 

Name:	PIC.jpg 
Views:	23 
Size:	265.8 KB 
ID:	52595

    Click image for larger version. 

Name:	error.jpg 
Views:	23 
Size:	71.2 KB 
ID:	52596

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Debug.Print strCriteria AND Task
    In fact I would Debug.Print all of your concatenated strings.

    Plus task is NOT defined.
    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

  7. #7
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,422
    Maybe some part of this will help you figure it out.
    http://allenbrowne.com/ser-62.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Looks to me like you are concatenating strings with no spaces between elements, which is jamming it all together as a meaningless string. Debug.print would instantly show this.
    For example, "And" should be " And ", otherwise "And" gets crowded on both sides.

  9. #9
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    you may try to Replace your function with this one:

    Code:
    Function SearchCriteria()
        Dim task As String
        Dim strCriteria As String
            
        task = "Select * from tbl_TR"
        
        If Me.cbo_Dept.ListIndex <> -1 Then
           strCriteria = strCriteria & "[Dept] = '" & Me.cbo_Dept & "' And "
        End If
        
        If Me.cbo_DocType.ListIndex <> -1 Then
            strCriteria = strCriteria & "[DocType] = '" & Me.cbo_DocType & "' And "      'ORDER BY tbl_TR.ID ASC"
        
        End If
        
        If Me.cbo_PIC.ListIndex <> -1 Then
            strCriteria = strCriteria & "[PIC] = '" & Me.cbo_PIC & "' And "
        End If
    
        If Len(strCriteria) <> 0 Then
            strCriteria = " Where " & Left$(strCriteria, Len(strCriteria) - 5)
        End If
        
        task = task & strCriteria & " ORDER BY tbl_TR.ID ASC;"
        
        Me.SubForm_TR.Form.RecordSource = task
        Me.SubForm_TR.Form.Requery
    End Function

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

Similar Threads

  1. Replies: 13
    Last Post: 10-09-2021, 02:07 AM
  2. Replies: 6
    Last Post: 09-22-2015, 03:30 PM
  3. Replies: 7
    Last Post: 08-08-2012, 03:28 PM
  4. Replies: 1
    Last Post: 04-20-2012, 03:16 AM
  5. Replies: 4
    Last Post: 08-16-2011, 05:54 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