Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Compare Database
Option Explicit
Public Sub AddToWhere(FieldValue As Variant, FieldName As String, MyCriteria As String, ArgCount As Integer)
' Create criteria for WHERE clause.
If FieldValue <> "" Then
' Add "and" if other criterion exists.
If ArgCount > 0 Then
MyCriteria = MyCriteria & " and "
End If
' Append criterion to existing criteria.
' Enclose FieldValue and asterisk in quotation marks.
MyCriteria = (MyCriteria & FieldName & " Like " & Chr(39) & FieldValue & Chr(42) & Chr(39))
' Increase argument count.
ArgCount = ArgCount + 1
End If
End Sub
Option Compare Database
Option Explicit
Private Sub cmdView_Click()
'On Error GoTo Err_VIEW_Click
'------------------------------ Starting here ----------------------
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim ArgCount As Integer
Dim db As Database
Dim rs As Recordset
Dim intRecCount As Integer
Set db = CurrentDb
' Initialize SELECT statement.
MySQL = "SELECT * FROM [tblScouts] WHERE "
' Use values entered in text boxes in form header to create criteria for WHERE clause.
'text box name on form 'Field name in Table 'blank info ' number of times run Addt
AddToWhere [Find1], "[ScoutName]", MyCriteria, ArgCount
AddToWhere [Find2], "[CarWeight]", MyCriteria, ArgCount
' If no criterion specifed, return all records.
If MyCriteria = "" Then
MyCriteria = "True"
End If
' Create SELECT statement.
MyRecordSource = MySQL & MyCriteria
' Optional Order By clause
If Me![Find1] <> "" Then
MyRecordSource = MySQL & MyCriteria & " ORDER BY [ScoutName]"
ElseIf Me![Find2] <> "" Then
MyRecordSource = MySQL & MyCriteria & " ORDER BY [CarWeight]"
Else
MyRecordSource = MySQL & MyCriteria & " ORDER BY [ScoutName]"
End If
Set rs = db.OpenRecordset(MyRecordSource)
intRecCount = rs.RecordCount
rs.Close
If intRecCount > 0 Then
' display the subform
Me![SearchsubForm].Form.Visible = True
' set record source to Subform
Me![SearchsubForm].Form.RecordSource = MyRecordSource
Else
MsgBox "Sorry, No Match Found!"
If Me![SearchsubForm].Form.Visible = True Then
' hide the subform
Me![SearchsubForm].Form.Visible = False
End If
End If
Exit_VIEW_Click:
Exit Sub
Err_VIEW_Click:
MsgBox Error$
Resume Exit_VIEW_Click
End Sub
Private Sub Form_Load()
' hide the subform
Me![SearchsubForm].Form.Visible = False
End Sub