Search form to pull up records with values between two numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheKimmy
    New Member
    • Jul 2010
    • 6

    #1

    Search form to pull up records with values between two numbers

    Okay, so I'm working on a database application with a search form that covers a ton of fields. And everything worked fine, until I tried to set it to pull records that have a value between two numbers. These numbers are years, but the fields aren't in a date format, just a normal integer format. The field in the table is called ActiveDate, and the search form has numActiveFrom and numActiveTo. I can't seem to get the code to run it, and I can't figure out what's wrong with the damned thing. The text fields all work fine, but not the number fields specified above. Below is the bit of code that relates to this field. I'm also including the bit just before and the bit just after it for context. Any help would be very much appreciated. Please let me know if you need any other details.

    Code:
        ' Do Public Knowledge next
        If Not IsNothing(Me.txtPublic) Then
            ' .. add to the predicate
            varWhere = (varWhere + " AND ") & "[PublicKnowledge] LIKE '*" & Me.txtPublic & "*'"
        End If
        
        ' Check Active From first
        If Not IsNothing(Me.numActiveFrom) Then
            ' .. build the predicate
            varWhere = (varWhere + " AND ") & "[ActiveDate] >= #" & CInt(Me.numActiveFrom) & "#"
        End If
        
        ' Then, do Active To
        If Not IsNothing(Me.numActiveTo) Then
            ' .. add to the predicate
            varWhere = (varWhere + " AND ") & "[ActiveDate] <= #" & CInt(Me.numActiveTo) & "#"
        End If
    
       ' Check to see that we built a filter
        If IsNothing(varWhere) Then
            MsgBox "You must enter at least one search criteria.", vbInformation, gstrAppTitle
            Exit Sub
        End If
        
        ' Open a recordset to see if any rows returned with this filter
        Set rst = db.OpenRecordset("SELECT * FROM [tblCharactersMain] WHERE " & varWhere)
        ' See if found none
        If rst.RecordCount = 0 Then
            MsgBox "No characters meet your criteria.", vbInformation, gstrAppTitle
            ' Clean up recordset
            rst.Close
            Set rst = Nothing
            Exit Sub
        End If
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    If all the fields are integers, the problem is you need to remove the "#" from your code. You don't use the # delimiter unless you are working with date/time fields.

    Jim

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Assuming your [From] and [To] Fields represent Years (Integers), then the General Syntax would be:
      The 1st Day of the from Field to the Last Day of the To Field, namely:
      Code:
      "SELECT * FROM YaDa-YaDa WHERE [ActiveDate] BETWEEN #" & DateSerial(Me![numActiveFrom], 1, 1) & _
      "# AND #" & DateSerial(Me![numActiveTo], 12, 31) & "#"

      Comment

      • TheKimmy
        New Member
        • Jul 2010
        • 6

        #4
        jimatqsi, I tried using the same code with the ' instead, as I've done with my other fields, but it didn't work then either. Is there something else I should be doing?

        ADezii, I tried using the code you provided (with the proper adaptations, of course). But when I try to run a search it tells me there are no records that fit the search, no matter what years I enter. Also, I need the search form to work even if the person only enters the start year, or if they only enter the end year. I did something similar to this several years ago (I'm really out of practice with this), but it was with date fields, not plain number fields, so the code doesn't translate across quite right.

        Thanks so much for trying to help, guys, but I'm just not quite there yet. Any other suggestions?

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by TheKimmy
          jimatqsi, I tried using the same code with the ' instead, as I've done with my other fields, but it didn't work then either. Is there something else I should be doing?

          ADezii, I tried using the code you provided (with the proper adaptations, of course). But when I try to run a search it tells me there are no records that fit the search, no matter what years I enter. Also, I need the search form to work even if the person only enters the start year, or if they only enter the end year. I did something similar to this several years ago (I'm really out of practice with this), but it was with date fields, not plain number fields, so the code doesn't translate across quite right.

          Thanks so much for trying to help, guys, but I'm just not quite there yet. Any other suggestions?
          I tested the code before I sent it to you and it does work. Your Integer Field does represent Years, correct (2000, 2010, 2005, 1942, etc.)?

          Comment

          • TheKimmy
            New Member
            • Jul 2010
            • 6

            #6
            Originally posted by ADezii
            I tested the code before I sent it to you and it does work. Your Integer Field does represent Years, correct (2000, 2010, 2005, 1942, etc.)?
            Those are the types of things that are entered, yes. It's just a general number field, so it's not specific to just years, any kind of number could be entered. Maybe it was the alterations I had to make to make that search string part of the main search string? It's possible that I did that wrong. Here's how it looks when I do it:

            Code:
                ' Do Public Knowledge next
                If Not IsNothing(Me.txtPublic) Then
                    ' .. add to the predicate
                    varWhere = (varWhere + " AND ") & "[PublicKnowledge] LIKE '*" & Me.txtPublic & "*'"
                End If
                
                ' Check Active From First
                If Not IsNothing(Me.numActiveFrom) Then
                    ' .. build the predicate
                    varWhere = (varWhere + " AND ") & "[ActiveDate] BETWEEN #" & DateSerial(Me![numActiveFrom], 1, 1) & _
            "# AND #" & DateSerial(Me![numActiveTo], 12, 31) & "#"
                End If
            
               ' Check to see that we built a filter
                If IsNothing(varWhere) Then
                    MsgBox "You must enter at least one search criteria.", vbInformation, gstrAppTitle
                    Exit Sub
                End If
                
                ' Open a recordset to see if any rows returned with this filter
                Set rst = db.OpenRecordset("SELECT * FROM [tblCharactersMain] WHERE " & varWhere)
                ' See if found none
                If rst.RecordCount = 0 Then
                    MsgBox "No characters meet your criteria.", vbInformation, gstrAppTitle
                    ' Clean up recordset
                    rst.Close
                    Set rst = Nothing
                    Exit Sub
                End If

            Comment

            • TheKimmy
              New Member
              • Jul 2010
              • 6

              #7
              Has nobody had any luck figuring this?

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                What do you have at the moment Kimmy?

                This shouldn't be too difficult, but I expect some confusion is getting into the picture via the communication over a web page.

                If both the search items and the item being searched are in the form of integers then hashes (#) will not be required. What will be (almost certainly) is the "Between" form of criteria. Something like :
                Code:
                strWhere = strWhere & " AND " & _
                           "([ActiveDate] Between " & Me.numActiveFrom & _
                                            " And " & Me.numActiveTo & ")"
                Check out Example Filtering on a Form and post back if you find you're still struggling. I'm confident we can find your solution assuming we can fully understand the precise problem.
                Last edited by NeoPa; Jul 21 '10, 10:27 AM.

                Comment

                • TheKimmy
                  New Member
                  • Jul 2010
                  • 6

                  #9
                  Originally posted by NeoPa
                  What do you have at the moment Kimmy?

                  This shouldn't be too difficult, but I expect some confusion is getting into the picture via the communication over a web page.

                  If both the search items and the item being searched are in the form of integers then hashes (#) will not be required. What will be (almost certainly) is the "Between" form of criteria. Something like :
                  Code:
                  strWhere = strWhere & " AND " & _
                             "([ActiveDate] Between " & Me.numActiveFrom & _
                                              " And " & Me.numActiveTo & ")"
                  Check out Example Filtering on a Form and post back if you find you're still struggling. I'm confident we can find your solution assuming we can fully understand the precise problem.
                  The very first post has the code for what I started with, and my last post before yours has the code for what ADezii had suggested, with a few alterations I had to make that I was afraid might have screwed up his code.

                  I tried the code that you provided, which worked beautifully when both the From and the To dates were entered, which is a far sight further than I've gotten before. The only problem with that is that I also need to be able to only enter the From or To years by themselves. For example, if I put 1980 in the To field with nothing in the From field, I want to be able to pull up every record at or before 1980. Or, if I put 1960 in the From field with nothing in the To field, I want to pull up every record at or after 1960.

                  Would that be possible, or am I dreaming?

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Originally posted by TheKimmy
                    The very first post has the code for what I started with, and my last post before yours has the code for what ADezii had suggested, with a few alterations I had to make that I was afraid might have screwed up his code.

                    I tried the code that you provided, which worked beautifully when both the From and the To dates were entered, which is a far sight further than I've gotten before. The only problem with that is that I also need to be able to only enter the From or To years by themselves. For example, if I put 1980 in the To field with nothing in the From field, I want to be able to pull up every record at or before 1980. Or, if I put 1960 in the From field with nothing in the To field, I want to pull up every record at or after 1960.

                    Would that be possible, or am I dreaming?
                    It is definately possible, but unfortunately I'm on Vacation right now. If no one comes up with the answer, I'll provide it to you as soon as I get home.

                    Comment

                    • TheKimmy
                      New Member
                      • Jul 2010
                      • 6

                      #11
                      Originally posted by ADezii
                      It is definately possible, but unfortunately I'm on Vacation right now. If no one comes up with the answer, I'll provide it to you as soon as I get home.
                      Thanks much. I don't mean to be a noodge, but this is for my boyfriend so I'm trying to be brilliant beyond my habits. Enjoy your vacation!

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        For more flexibility in this I could do no better than to point you to a similar thread Is it possible to have flexible formatting on date in filter? I'm currently involved in.

                        Let us know if you find enough in there or if you need further explanation.

                        by its very nature this is more complex than a simple From/To situation, so don't be surprised if the code is somewhat more complicated.

                        Comment

                        Working...