Search Form using Option Buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NMarks
    New Member
    • Feb 2008
    • 7

    Search Form using Option Buttons

    Hello all,

    I have created a database for my work that stores information for keys/locks, doors and employees.

    Specifically the database contains all the information of our lock system, which doors certain locks are attached to and what employees have keys to certain locks.

    The form has 3 subforms, 1 is the Key List which is basically the master subform as the other 2 subforms are a Door List and Staff List both of which are linked to the Key List using the [KeyNumber] field.

    I am trying to make a search form to this that will allow me to search certain fields in all 3 of the subforms, the fields will be chosen by option buttons. With the search box being just a simple AfterUpdate command.

    This is the code I currently have ...

    Code:
    Private Sub SearchFor_AfterUpdate()
    On Error GoTo Err_SearchFor_AfterUpdate
        Dim MySQL As String
        
        MySQL = "SELECT KeyNumber, KeyCode, KeyName "
        MySQL = MySQL & "FROM fKeyList "
        
        If InStr(1, Me.SearchFor, Chr(34)) Then
            MsgBox "Do not use quotes"
        Else
    
            Select Case Me.SearchField
        
            Case 1  'keynumber
                MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
            Case 2  'keycode
                MySQL = MySQL & "WHERE [KeyCode] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
            Case 3  'keyname
                If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
                    MsgBox "Must use letters or single apostrophe. No spaces or special characters"
                Else
                    MySQL = MySQL & "WHERE [KeyName] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
    
                End If
            
            Case 4  'doornumber
                MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfDoorList WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
            
            Case 5  'lastname
                If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
                    MsgBox "Must use letters or single apostrophe. No spaces or special characters"
                Else
                    MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfStaffList WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
    
                End If
            End Select
    
        'MsgBox MySQL
        Me.sfKeyList.Form.RecordSource = MySQL
        End If
        
    Exit_SearchFor_AfterUpdate:
        Exit Sub
    
    Err_SearchFor_AfterUpdate:
        MsgBox Err.Description
        Resume Exit_SearchFor_AfterUpdate
        
    End Sub
    The problem is it's not working. I keep getting this error when searching for anything using any of the options ...

    The record source 'SELECT KeyNumber, KeyCode, KeyName FROM fKeyList WHERE [KeyNumber] like "****" specified on
    this form or report does not exist.

    The **** indicates the item i'm searching for.

    Any help would be appreciated.

    Thanks
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Code:
    ...MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
    Hi Nmarks. At a glance the SQL looks fine - but then I notice the use of Chr(34) before and after all search phrases as above. ASCII 34 is the double-quote character ("). I guess it should instead be the single quote. Why not just refer directly to the character involved, like this:
    [code=sql]MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"[/code]
    For clarity I would suggest not using Chr at all in your search strings unless you want to refer to a non-printable character.

    -Stewart
    ps Access string literals within a string built as above are referred to within single quotes, although strings in general are referred to within double quotes.
    Last edited by Stewart Ross; Mar 3 '08, 08:30 PM. Reason: clarification and ps

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      Hi again. Notwithstanding my concerns about the quote marks, it isn't that. I have tried a skeleton version of your code in a new form using one of my own tables, but without the subforms. The SQL works without error on the recordsource of a main form.

      After writing this I tried it successfully on a subform within a main form. Since everything seems to work, is sfKeyList a valid subform name? I too used
      Code:
      me.[subformname].form.recordsource = ...
      and it worked fine for me.

      -Stewart
      Last edited by Stewart Ross; Mar 3 '08, 09:51 PM. Reason: subform now tried

      Comment

      • NMarks
        New Member
        • Feb 2008
        • 7

        #4
        In order to get all the forms to display in a tabular format like I wanted, and linked together, I had to create the form, then the "main" subform labeled sfKeyList.

        The 2 additional subforms are sfDoorList and sfStaffList which are both subforms of sfKeyList.

        Comment

        • NMarks
          New Member
          • Feb 2008
          • 7

          #5
          Originally posted by Stewart Ross Inverness
          Code:
          ...MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
          Hi Nmarks. At a glance the SQL looks fine - but then I notice the use of Chr(34) before and after all search phrases as above. ASCII 34 is the double-quote character ("). I guess it should instead be the single quote. Why not just refer directly to the character involved, like this:
          [code=sql]MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"[/code]
          For clarity I would suggest not using Chr at all in your search strings unless you want to refer to a non-printable character.

          -Stewart
          ps Access string literals within a string built as above are referred to within single quotes, although strings in general are referred to within double quotes.
          I will have to edit this then.

          The majority of my Access experience has been via Google and a guy I work with. He uses the Chr(34) a lot and I picked it up along the way.

          Thank you for the advice.

          Comment

          • Stewart Ross
            Recognized Expert Moderator Specialist
            • Feb 2008
            • 2545

            #6
            Originally posted by NMarks
            I will have to edit this then.

            The majority of my Access experience has been via Google and a guy I work with. He uses the Chr(34) a lot and I picked it up along the way.

            Thank you for the advice.
            Hi again. Just one more thought, since the SQL works fine for strings - it is a string you are comparing, isn't it? If the unique key number is a numeric value, not a string, the SQL 'where' condition will fail (there are no quote marks at all used when comparing numeric values, nor is the string comparison 'like' used). Long shot, though, given that your where clause seems well thought-out.

            -Stewart

            Comment

            • NMarks
              New Member
              • Feb 2008
              • 7

              #7
              Originally posted by Stewart Ross Inverness
              Hi again. Just one more thought, since the SQL works fine for strings - it is a string you are comparing, isn't it? If the unique key number is a numeric value, not a string, the SQL 'where' condition will fail (there are no quote marks at all used when comparing numeric values, nor is the string comparison 'like' used). Long shot, though, given that your where clause seems well thought-out.

              -Stewart
              The unique Key Number field is a letter/numeric value. It contains 2 letters followed by 6 numbers manually entered by us, the user.

              An example is "HB123456".

              The KeyCode and DoorNumber fields are also setup the same way. Just less letters and numbers.

              Comment

              • NMarks
                New Member
                • Feb 2008
                • 7

                #8
                Alright I finally got it.

                This is the end result of my code ...

                Code:
                Private Sub SearchFor_AfterUpdate()
                On Error GoTo Err_SearchFor_AfterUpdate
                    Dim MySQL As String
                    
                    MySQL = "SELECT KeyNumber, KeyCode, KeyName "
                    MySQL = MySQL & "FROM tKey "
                    
                    If InStr(1, Me.SearchFor) Then
                        MsgBox "Do not use quotes"
                    Else
                 
                        Select Case Me.SearchField
                    
                        Case 1  'keynumber
                            MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"
                        Case 2  'keycode
                            MySQL = MySQL & "WHERE [KeyCode] like '" & Me![SearchFor] & "'"
                        Case 3  'keyname
                            If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
                                MsgBox "Must use letters or single apostrophe. No spaces or special characters"
                            Else
                                MySQL = MySQL & "WHERE [KeyName] like '" & Me![SearchFor] & "'"
                 
                            End If
                        
                        Case 4  'doornumber
                            MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tDoor WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
                        
                        Case 5  'lastname
                            If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
                                MsgBox "Must use letters or single apostrophe. No spaces or special characters"
                            Else
                                MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tStaff WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
                        End If
                        End Select
                 
                    'MsgBox MySQL
                    Me.sfKeyList.Form.RecordSource = MySQL
                    End If
                    
                Exit_SearchFor_AfterUpdate:
                    Exit Sub
                 
                Err_SearchFor_AfterUpdate:
                    MsgBox Err.Description
                    Resume Exit_SearchFor_AfterUpdate
                    
                End Sub
                I replaced all the FROM fields to reference the tables directly as opposed the subforms on the form.

                This worked like a charm. I did also clean up the code a bit as you advised, more so in the first section I still have to go through the bottom 2 sections that reference the other subforms, I tried once already but keep getting a syntax error so I just need to work it out a bit.

                Thank you for all your help though it was greatly appreciated.

                Comment

                • Stewart Ross
                  Recognized Expert Moderator Specialist
                  • Feb 2008
                  • 2545

                  #9
                  Hi again. Well done for solving what was a very puzzling problem. You did all the work, and should take all the credit!

                  Cheers

                  Stewart

                  Comment

                  Working...