am trying to set up a 'generic' apply filter module. What I want it to do: look at the open form and get three values. The value I want filtered for, the box I want it to apply the filter to, and the type string (may be date, may be text). Using the 'tag' property of the txtbox I'm using for input to tell it which box I'm wanting to apply filter for. Using the 'value' of the box for the data. Using the format of the box to give the format (evaluating using if statement. When I use the watch box, my values are showing up properly, but when it applies the filter, it is either filtering showing all data or filtering showing no data. I believe I may have the syntax wrong, but am not sure quite where. I want to be able to call this module from any form so I don't have to have 'apply filter' coded behind all of my forms (so i'm lazy!)
example values:
datFilter.tag = datPerdiem
datFilter.value = 09/17/2003
datFilter.Format = Short Date
when run with watch open, values that are showing are [datPerdiem]
#09/17/2003#
Short Date
These are what I think should be showing, but when it runs the apply filter, I either get a blank record or 16341 records. What it should do is show the 23 records that have a perdiem date of 09/17/2003. When I get it working properly, I am planning on using an if statement to evaluate the format and depending on the format, either use "#" or "'". Note: debug.print is there for evaluation purposes!
Option Compare Database
Option Explicit
Dim dbs As DAO.Database
Dim ctl As Control
Dim frm As Form
Public Function ApplyFilter()
On Error GoTo ApplyFilter_Err
Dim strErrMsg As String 'For Error Handling
Set dbs = CurrentDb()
Dim ctlCurrentControl As Control
Dim strControlName As String
Dim FilterVal As String
Dim FilterStr As Variant
Dim FilterType As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.NAME
FilterVal = ctlCurrentControl.Tag
FilterStr = ctlCurrentControl.Value
FilterType = ctlCurrentControl.Format
Quotes = "'"
DoCmd.GoToControl FilterVal
If FilterType = "Short Date" Then 'FilterType = Short Date
FilterStr = "#" & FilterStr & "#"
FilterVal = "[" & FilterVal & "]"
Debug.Print FilterVal
Debug.Print FilterStr
Debug.Print FilterType
DoCmd.ApplyFilter , "'" & FilterVal & " like " & FilterStr & "'"
End If
ApplyFilter_Exit:
Exit Function
ApplyFilter_Err:
Select Case Err
Case Else
strErrMsg = "An error occurred in ApplyFilter" & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error #: " & Format$(Err.number) & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.DESCRIPTION
msgbox strErrMsg, vbInformation, "ApplyFilter"
Resume ApplyFilter_Exit
End Select
End Function
example values:
datFilter.tag = datPerdiem
datFilter.value = 09/17/2003
datFilter.Format = Short Date
when run with watch open, values that are showing are [datPerdiem]
#09/17/2003#
Short Date
These are what I think should be showing, but when it runs the apply filter, I either get a blank record or 16341 records. What it should do is show the 23 records that have a perdiem date of 09/17/2003. When I get it working properly, I am planning on using an if statement to evaluate the format and depending on the format, either use "#" or "'". Note: debug.print is there for evaluation purposes!
Option Compare Database
Option Explicit
Dim dbs As DAO.Database
Dim ctl As Control
Dim frm As Form
Public Function ApplyFilter()
On Error GoTo ApplyFilter_Err
Dim strErrMsg As String 'For Error Handling
Set dbs = CurrentDb()
Dim ctlCurrentControl As Control
Dim strControlName As String
Dim FilterVal As String
Dim FilterStr As Variant
Dim FilterType As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.NAME
FilterVal = ctlCurrentControl.Tag
FilterStr = ctlCurrentControl.Value
FilterType = ctlCurrentControl.Format
Quotes = "'"
DoCmd.GoToControl FilterVal
If FilterType = "Short Date" Then 'FilterType = Short Date
FilterStr = "#" & FilterStr & "#"
FilterVal = "[" & FilterVal & "]"
Debug.Print FilterVal
Debug.Print FilterStr
Debug.Print FilterType
DoCmd.ApplyFilter , "'" & FilterVal & " like " & FilterStr & "'"
End If
ApplyFilter_Exit:
Exit Function
ApplyFilter_Err:
Select Case Err
Case Else
strErrMsg = "An error occurred in ApplyFilter" & vbCrLf & vbCrLf
strErrMsg = strErrMsg & "Error #: " & Format$(Err.number) & vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.DESCRIPTION
msgbox strErrMsg, vbInformation, "ApplyFilter"
Resume ApplyFilter_Exit
End Select
End Function