Results 1 to 3 of 3
  1. #1
    Kipandrews06 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    54

    Need to filter records by a currency field range

    I have a splitform that is bound to a query. I'm making this form a search criteria form and am trying to get it to filter results based on a range on a currency field. For example, I have a field (from my query) called "Total Cost". I also have two unbound text boxes on my form header that I'd like to have the capability to do a range between the currency values entered in the text boxes. Like if someone typed in "$1,000" on the "FROM" textbox and "$5,000" on the "TO" texbox, it would filter and show all the records that have a Total Cost in that range in the datasheet part of of the split form. I've been playing around with it alot and can't seem to get it to work. My code is below. The other search criteria I have is working great, it's just the currency range that has been giving me fits. My code is below which is for the OnClick event for a command filter button. Any help would be greatly appreciated!



    Code:
    Private Sub cmdFilter_Click()
        Dim strWhere As String                  'The criteria string.
        Dim lngLen As Long                      'Length of the criteria string to append to.
        Const conJetDate = "\#mm\/dd\/yyyy\#"  
     
        'Look at each search box, and build up the criteria string from the non-blank ones.
        If Not IsNull(Me.txtFilterOrderID) Then
            strWhere = strWhere & "([ID] = " & Me.txtFilterOrderID & ") AND "
        End If
     
        If Not IsNull(Me.txtFilterContractorName) Then
            strWhere = strWhere & "([Name of Contractor] Like ""*" & Me.txtFilterContractorName & "*"") AND "
        End If
              
        If Not IsNull(Me.txtFilterCostFrom) Then
            strWhere = strWhere & "([Total Cost] = " & Format(Me.txtFilterCostFrom) & ") AND "
        End If
       
        If Not IsNull(Me.txtFilterCostTo) Then
            strWhere = strWhere & "([Total Cost] = " & Format(Me.txtFilterCostTo ) & ") AND"
        End If
       
        'Use the format string to add the # delimiters and get the right international format.
        If Not IsNull(Me.txtStartDate) Then
            strWhere = strWhere & "([Date Requested] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
        End If
       
    ‘Use "less than the next day" since this field has times as well as dates.
        If Not IsNull(Me.txtEndDate) Then   'Less than the next day.
            strWhere = strWhere & "([Date Requested] < " & Format(Me.txtEndDate + 1, conJetDate) & ")"
        End If
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then     'Nah: there was nothing in the string.
            MsgBox "No criteria was entered", vbInformation, "Error"
        Else                    'Yep: there is something there, so remove the " AND " at the end.
            strWhere = Left$(strWhere, lngLen)
     
              Me.Filter = strWhere
            Me.FilterOn = True
        End If
    End Sub

  2. #2
    Kipandrews06 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    54
    Nevermind this post, I actually got lucky and figured it out shortly after I posted this. I'm new to this forum and didn't see how to delete my post so I'll just mark it as solved so nobody wastes their time on me. For anyone who comes across with a similar issue and is interested, here is the line of vba code I put in for the currency range to filter the results.

    Code:
     If Not IsNull(Me.txtFilterCostFrom) Then        strWhere = strWhere & "([Total Cost] >= " & Me.txtFilterCostFrom & " And [Total Cost] <= " & Me.txtFilterCostTo & ") And "
        End If

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Nothing obvious there. Troubleshooting 101: debug.print sql, clauses, variables, etc. and look at them in the immediate window. If the cause of an issue isn't obvious there, copy and paste into a new query in sql view and try switching to datasheet view. If it balks, the offending part is often highlighted and you can focus on that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 7
    Last Post: 09-10-2014, 12:33 PM
  2. Replies: 4
    Last Post: 02-03-2014, 12:16 PM
  3. Replies: 4
    Last Post: 07-18-2013, 03:14 AM
  4. Multiple Field & date range filter
    By mrkandohi001 in forum Reports
    Replies: 6
    Last Post: 01-18-2012, 03:11 AM
  5. Replies: 2
    Last Post: 03-23-2011, 11:43 AM

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