Search Form Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • f430
    New Member
    • Aug 2010
    • 43

    #1

    Search Form Code

    hi, i have a master table with all my part information, and it has columns with part number, date, defects,...
    and i am trying to write a code for my search form.
    i have created a search form on Access, and it has 5 combo boxes for:
    part number
    defects
    assembled by
    and the other 2 combo boxes are for "from" and "to" date range. i have made the combo boxes for the date range to display a calendar when clicked and choose a date.

    i have been trying to write a code that only requires one of these fields to be filled out and it can filter my master table.
    i would appreciate any help on this
    Thanks
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Check out Example Filtering on a Form.

    When you've had a play, let us know if there is anything you still feel unsure about. The more clear and specific you can be about what you're struggling with, the more quickly and easily we we be able to help you.

    Comment

    • f430
      New Member
      • Aug 2010
      • 43

      #3
      Thanks NeoPa for the info, the article provides some helpful hints. however i do have some questions about my code.
      1) in my search form i have a date range, which is made up of a combo box at each "from" and "to" when they are clicked a calendar pops up and a date can be picked. my question in regards to this is, how can i do a search of my master table of any entry in between these 2 dates? because i believe that the code u provided above was only meant to pick the exact date entered.

      2)in my second combo box, im trying to filter my master table according to the defect entered into the search. however in my master table i have 3 columns for defects (labeled defect 1, 2, 3)since some parts have more than one defect. how can i make my code search all 3 columns for the defect entered into the search field?
      would it be something like this:

      Code:
      If Me!Defect > "" Then _
           strFilter = strFilter & _
           " AND ([Defect Code 1, defect code 2, defect code 3]=" & _
           Me!Defect & ")"
      thanks
      Last edited by NeoPa; Aug 9 '10, 10:26 PM. Reason: Please use the [CODE] tags provided

      Comment

      • f430
        New Member
        • Aug 2010
        • 43

        #4
        one more thing. i have tried manipulating your code to fit mine, but since im using a command button for my search,nothing really seemed to work out. after i tried to fix everything to on click and saved, exited and ran the code. but it didnt work

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          Originally posted by f430
          f430: 1) in my search form i have a date range, which is made up of a combo box at each "from" and "to" when they are clicked a calendar pops up and a date can be picked. my question in regards to this is, how can i do a search of my master table of any entry in between these 2 dates? because i believe that the code u provided above was only meant to pick the exact date entered.
          There is a Between X And Y construct in a SQL WHERE clause or filter. EG :
          Code:
          ...
          WHERE [DateField] Between #1/1/2009# And #12/31/2009#
          Originally posted by f430
          f430: 2)in my second combo box, im trying to filter my master table according to the defect entered into the search. however in my master table i have 3 columns for defects (labeled defect 1, 2, 3)since some parts have more than one defect. how can i make my code search all 3 columns for the defect entered into the search field?
          would it be something like this:

          Code:
          If Me!Defect > "" Then _
          strFilter = strFilter & _
          " AND ([Defect Code 1, defect code 2, defect code 3]=" & _
          Me!Defect & ")"
          This is a good reason for not storing your data that way of course, but you could use (if exact matches were being looked for) :

          Code:
          strSQL = "SELECT ..." & _
                   "WHERE '" & Me.DefectCode & "' In([defect code 1], _
                                                     [defect code 2], _
                                                     [defect code 3])

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            Originally posted by f430
            f430: one more thing. i have tried manipulating your code to fit mine, but since im using a command button for my search,nothing really seemed to work out. after i tried to fix everything to on click and saved, exited and ran the code. but it didnt work
            I can't really comment or advise on where you went wrong unless you show me what you've done. I'm clever I know, but not that clever :D

            Comment

            • f430
              New Member
              • Aug 2010
              • 43

              #7
              i keep getting an error when using this
              Code:
              strSQL = "SELECT ..." & _ "WHERE '" & Me.Defect & "'In([defect code 1], _ [defect code 2], _ [defect code 3])
              once i write this statement and move to another line, it highlights [defect code 2] and says compile error:expected: end of statement.

              also, im not sure what comes before the code you provided for the date range. and i am also assuming that this would look something like this:
              Code:
              WHERE [DateField] Between #fromcombobox# And #tocombobox#

              i will post my complete code after i solve the current issues.

              Thank you for your help.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                As the underline character (_) indicates line continuation, it is not valid to use it as you have within code on a line.

                As for your Between code :
                Code:
                WHERE [DateField] Between #fromcombobox# And #tocombobox#
                This is not quite correct as dates should be formatted properly for use in SQL (See Literal DateTimes and Their Delimiters (#)). It is pretty close otherwise though. It may even be that you have explicit formatting for the two ComboBoxes which is compatible with SQL. In that case it would not be so wrong to use the code you've posted (Personally I'd always reformat it explicitly, but that's a choice if the code is already working).

                Comment

                • f430
                  New Member
                  • Aug 2010
                  • 43

                  #9
                  i have defined my from box as "date1" and to box as "date2"
                  i am not really sure what you mean by explicitly formatting these two boxes.
                  i was also wondering what kind of code i should use before the where statement.

                  Thanks

                  Comment

                  • f430
                    New Member
                    • Aug 2010
                    • 43

                    #10
                    this is the code that i currently have, but it is giving me a lot of problems. i also have not included the code for my date range, because i am not quite sure how to build it.
                    Code:
                    Private Sub cmdfindrecords_Click()
                       Dim strFilter As String, strOldFilter As String
                    
                       strOldFilter = Me.Filter
                    
                    If Me!productnum > "" Then _
                         strFilter = strFilter & _
                         " AND ([Part Number]=" & _
                         Me!productnum & ")"
                         
                         
                    strFilter = "SELECT ..." & _
                                 "WHERE '" & Me.Defect & "' In([Defect Code 1], [Defect Code 2], _ [Defect Code 3])"
                    
                    
                    strFilter = "SELECT ..." & _
                                 "WHERE '" & Me.associateid & "' In([associate], [associate 2])"
                    
                         
                    If strFilter > "" Then strFilter = Mid(strFilter, 6)
                    If strFilter <> strOldFilter Then
                          Me.Filter = strFilter
                          Me.FilterOn = (strFilter > "")
                    End If
                    
                    
                    End Sub
                    when i search using the part number, nothing happens.
                    when i search using either associate number or defect, i get an error msg: u cant assign a value for this object, and when i go to debug it highlights this code:
                    Code:
                    Me.Filter = strFilter

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32669

                      #11
                      Originally posted by f430
                      f430: i am not really sure what you mean by explicitly formatting these two boxes.
                      I suggest you look at the article I linked you to in post #8 then. It is explained fully there.
                      Originally posted by f430
                      f430: i was also wondering what kind of code i should use before the where statement.
                      That would be a standard SQL query string. If this is something you're not familiar with then we may need a crash course on the very basics.

                      Queries in Access are actually QueryDef objects. These can be saved as you know, and run, and edited etc.

                      At the heart of every QueryDef there is the fundamental query instructions. These are written in SQL code and can be found, when editing a QueryDef, by selecting SQL View from the View menu. Such SQL code can form the basis of a QueryDef object, but there are also ways of executing SQL code from a simple string variable (or literal) from within VBA.

                      To learn more about what SQL can do for you look in the Help system (Finding Jet SQL Help).

                      The (almost) most basic form of a SQL instruction would be to display the contents of a table and goes like :
                      Code:
                      SELECT *
                      FROM   [TableName]
                      The SQL of a QueryDef is updatable. This can prove important.

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        Having seen your code some things become clearer to me.

                        Firstly, you are working on a filter string. Filter strings are essentially the WHERE clause of a full SQL statement, but without the word "WHERE ". The SELECT clause is equally unnecessary when dealing with filters (By the way, SELECT ... was just my use of the ellipsis (...) to indicate whatever you had before here. I wasn't suggesting the literal use of the dots).

                        You still have an underline character (_) on your line #13 which is an error (See post #8).

                        When building a filter string, it is advisable (generally) to start from scratch unless you know what you can expect it to contain already. It appears that your code would go around and around making the filter string longer and longer re-applying filtering on the same set of fields. Not good for you.

                        Comment

                        • f430
                          New Member
                          • Aug 2010
                          • 43

                          #13
                          NeoPa, i appreciate all the help.
                          i apologize for not keeping up with you, but my skills in access/vba are very basic. i am having some problems building a query string, would you be able to provide more information about building a standard sql string.

                          i fixed the minor errors and the here is what it looks like
                          Code:
                          If Me!productnum > "" Then _
                               strFilter = strFilter & _
                               " AND ([Part Number]=" & _
                               Me!productnum & ")"
                               
                               
                          strFilter = "If Me!defect > "" Then strFilter = strFilter & " And ([Defectcode] = " & Me!Defect & ") & "WHERE '" & Me.Defect & "' In([Defect Code 1], [Defect Code 2], [Defect Code 3])"
                          
                          
                          strFilter = "If Me!associateid > "" Then strFilter = strFilter & " And ([associates] = " & Me!associateid & ") & "WHERE '" & Me.associateid & "' In([associate], [associate 2])"
                          Thanks

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32669

                            #14
                            I must rush for now, but try looking more carefully at the code in the article linked in post #2. I can go into more detail later, but trust me, your understanding will benefit from a bit more looking at it. Even if you still need further explanation, at least when that comes you'll be able to put what you read into context. Those "aaaaaah" moments are really quite precious. I'd hate you to miss out ;)

                            PS. I know it probably all looks like a foreign language at the moment, but I expect it will become clearer soon.

                            Comment

                            • f430
                              New Member
                              • Aug 2010
                              • 43

                              #15
                              my date1 and date2 comboboxes are formatted as short dates in the properties of the combo box, so i guess they are explicitly formatted, and the code im trying to use is :
                              Code:
                              strFilter = "SELECT [CreationDate]" & "FROM Master Table" & "WHERE [CreationDate] Between #date1# And #date2#"
                              however, i am a little unsure about the codes on line 7 and 10 above, because i have not really defined what "defectcode " and "associates " are. and i keep getting them highlighted when i debug

                              Comment

                              Working...