Hi everyone,
I'm new to MS Access and especially to VBA code. I'm trying to have my search button in a form find contact information based on what I type in the text boxes. However, my code (that a friend tried helping me with) keeps giving me a compile error when I try and run it. Is there something that looks wrong? Any help would be much appreciated.
Seems like my requery line is giving the compile error (syntax).
Main form name = fSearch
I'm new to MS Access and especially to VBA code. I'm trying to have my search button in a form find contact information based on what I type in the text boxes. However, my code (that a friend tried helping me with) keeps giving me a compile error when I try and run it. Is there something that looks wrong? Any help would be much appreciated.
Seems like my requery line is giving the compile error (syntax).
Main form name = fSearch
Code:
Option Compare Database
Private Sub cmdSearch_Click()
'Holds the Filtered Query
Dim DataSource As String
'Set Query based on text fields
DataSource = "SELECT * FROM tMediaContacts WHERE " & _
"[Publication] like '*" & Me.txtPublication & "*' AND" & _
"[Publication_Location] like '*" & Me.txtLocation & "*' AND " & _
"[Category_type] like '*" & Me.txtCategory & "*' AND " & _
"[First_Name] like '*" & Me.txtFirst & "*' AND " & _
"[Last_Name] like '*" & Me.txtLast & "*' AND " & _
"[Email] like '*" & Me.txtEmail & "*' AND " & _
"[Title] like '*" & Me.txtTitle & "*' AND " & _
"[City] like '*" & Me.txtCity & "*' AND " & _
"[State] like '*" & Me.txtState & "*'"
'Set Form Record Source to be the Query
Me.tMediaContacts_subform.Form.RecordSource = DataSource
'Bind that query to the form
Me.tMediaContacts_subform.Requery()
Exit Sub
End Sub
Private Sub cmdClear_Click()
'Clear all Text Boxes
txtPublication = txtLocation = txtCategory = txtFirst = txtLast = txtEmail = txtTitle = txtCity = txtState = ""
'Set the Record Source to be all records
Me.tMediaContacts_subform.Form.RecordSource = "SELECT * FROM tMediaContacts "
'Bind datasource to subform
Me.tMediaContacts_subform.Requery()
'Set the focus
txtPublication.SetFocus()
End Sub