Hi All!!
Hopefully you can help me. I am trying to run a search based on certain criteria. I would like the user to be able to fill in as much (or as little) criteria into different fields as they desire. I have five such fields that the user can put their criteria into. I want my database to look at the criteria, skip any null values that might be associated with the search and then spit out values that meet all the criteria. I have tried to do this a couple of ways, but now I am trying something new. Right now when I hit my Search button, nothing happens. I am hoping you can tell me what I am doing wrong. I need the search to run when the button is pressed, and then I need the filter to also work. Can you check my code to see if it should work, and then let me know if I am missing something. I am pretty new to this whole VBA thing.
Here is the code:
Any help is greatly appreciated. I am about to pull my hair out with this. Hopefully what I am trying to do makes sense.
Thanks in advance!!
Hopefully you can help me. I am trying to run a search based on certain criteria. I would like the user to be able to fill in as much (or as little) criteria into different fields as they desire. I have five such fields that the user can put their criteria into. I want my database to look at the criteria, skip any null values that might be associated with the search and then spit out values that meet all the criteria. I have tried to do this a couple of ways, but now I am trying something new. Right now when I hit my Search button, nothing happens. I am hoping you can tell me what I am doing wrong. I need the search to run when the button is pressed, and then I need the filter to also work. Can you check my code to see if it should work, and then let me know if I am missing something. I am pretty new to this whole VBA thing.
Here is the code:
Code:
Private Sub SearchBtn_Click()
Me.Filter = Lookup
Me.FilterOn = True
End Sub
Function Lookup() As String
multVar = 0 'This variable is to let the function know if there's more than one criteria
If Not IsNull(PartNumberLookup) Then
FilterString = "[PartNumber] Like '*" & PartNumberLookup & "*'"
multVar = multVar + 1
End If
If Not IsNull(DescriptionLookUp) Then
If multVar = 0 Then
FilterString = "[Description] Like '*" & DescriptionLookUp & "*'"
Else
FilterString = FilterStrong & "AND [Description] Like '*" & DescriptionLookUp & "*'"
End If
multVar = multVar + 1
End If
If Not IsNull(FirstUsedOnLookup) Then
If multVar = 0 Then
FilterString = "[FirstUsedOn] Like '*" & FirstUsedOnLookup & "*'"
Else
FilterString = FilterStrong & "AND [FirstUsedOn] Like '*" & FirstUsedOnLookup & "*'"
End If
multVar = multVar + 1
End If
If Not IsNull(OldPartNumberLookup) Then
If multVar = 0 Then
FilterString = "[OldPartNumber] Like '*" & OldPartNumberLookup & "*'"
Else
FilterString = FilterStrong & "AND [OldPartNumber] Like '*" & OldPartNumberLookup & "*'"
End If
multVar = multVar + 1
End If
If Not IsNull(WrongPartNumberLookup) Then
If multVar = 0 Then
FilterString = "[WrongPartNumber] Like '*" & WrongPartNumberLookup & "*'"
Else
FilterString = FilterStrong & "AND [WrongPartNumber] Like '*" & WrongPartNumberLookup & "*'"
End If
multVar = multVar + 1
End If
End Function
Any help is greatly appreciated. I am about to pull my hair out with this. Hopefully what I am trying to do makes sense.
Thanks in advance!!