I need to add a basic record search feature on an Access 2007 form. I set up two ComboBoxes, the first to provide three different search options: search by reference, by date or by staff name. The AfterUpdate event on this ComboBox would then set the RowSource of the second ComboBox to show the user any available references, dates or staff names to allow them to pick the one they want. A Go button then opens the associated form with the record(s) which match their criteria.
All was going well at the start, working fine, but then a bug crept in. If I select to search by reference (long) or name (string) it works fine, but if I select to search by date, whilst the available dates are displayed correctly in the second combo, when I try to select one I get an error: the value entered isn't valid for this field. Is this because the control should only be used with a single data type, and my trying to use three different ones at different times is confusing things? I don't have any formats set in the control's properties.
Private Sub btnGo_Click()
'go clicked so find request being searched for and open form at that request(s)
If Not IsNull(Me.cboSearchOption) And Not IsNull(Me.cboSearchFor) Then
'search variables entered so go to specified request
Select Case [gblOption]
Case "Reference"
DoCmd.OpenForm "frmRequestForInformation", , , "reference = " & Me.cboSearchFor.Value
Case "Request date"
DoCmd.OpenForm "frmRequestForInformation", , , "date_request_received = #" & Me.cboSearchFor.Value & "#"
Case "Staff"
DoCmd.OpenForm "frmRequestForInformation", , , "signed_by = '" & Me.cboSearchFor.Value & "'"
End Select
Else
MsgBox "Please ensure you have entered search values in both fields", vbOKOnly, "Unable to search"
End If
End Sub
Note: gblOption contains the value of cboSearchOption and is set on its AfterUpdate event along with the RowSource for cboSearchFor.