G
Guest
Hi Community...Please help me with a VBA...I have a search form with 4
unbound fields and an unbound listbox. The user inputs data into one or more
of the fields and the search results are shown in the list box. But there is
a problem. Please help me rectify the VBA to solve it.
When I do a search on the first field (number - Main_Pt_ID) by itself, it
works OK....When I do a search on the last field (Test - Name Field - Lname)
by itself, it works OK....But when I do a search on the second field (number
- Arthur_ID) by itself, nothing happens. Same with the third Field (Date -
DOB)....But the 2nd and 3rd fields work beautifully if used in conjunction
with the First field only....Please help me fix this glitch:
Dim sSearch As String
Dim sql As String
Dim strSQL() As String
If IsNull(Me.sMain_Pt_ID) = True And IsNull(Me.sArthur_ID) = True And
IsNull(Me.sDOB) = True And IsNull(sLname) = True Then
MsgBox "You must enter a search value", vbOKOnly
Exit Sub
End If
'/build the search string
If IsNull(sMain_Pt_ID) = False Then
sSearch = "[Main_Pt_ID]=" & Me.sMain_Pt_ID
End If
If IsNull(sArthur_ID) = False Then
If IsNull(sSearch) = False Then
sSearch = sSearch & " Or [Arthur_ID]=" & Me.sArthur_ID
Else
sSearch = "[Arthur_ID]=" & Me.sArthur_ID
End If
End If
If Not IsNull(Me.sDOB) Then
If IsNull(sSearch) = False Then
sSearch = sSearch & " Or [DOB]= #" & Me.sDOB & "#"
Else
sSearch = "[DOB]= #" & Me.sDOB & "#"
End If
End If
'Dim strSQL() As String
If Len(Nz(Me.sLname, "")) > 0 Then
strSQL() = Split(Me.sLname & ",,", ",")
strSQL(0) = Trim(strSQL(0))
strSQL(1) = Trim(strSQL(1))
If Len(strSQL(0)) > 0 Then
strSQL(0) = "([LName] = '" & strSQL(0) & "' "
If Len(strSQL(1)) > 0 Then
strSQL(0) = strSQL(0) & _
" AND [Fname] LIKE '" & strSQL(1) & "*' " ' <- Change * to %
End If
strSQL(0) = strSQL(0) & ") "
If Len(Nz(sSearch, "")) = 0 Then
sSearch = strSQL(0)
Else
sSearch = sSearch & " OR " & strSQL(0)
End If
End If
End If
'MsgBox sSearch
sql = "SELECT Main_Pt_ID, Arthur_ID, FName, LName, DOB, Pt_Type,
Head_Fam_member_ID FROM Tbl_Main_Pt WHERE " & sSearch
Me.ListFind.RowSource = sql
unbound fields and an unbound listbox. The user inputs data into one or more
of the fields and the search results are shown in the list box. But there is
a problem. Please help me rectify the VBA to solve it.
When I do a search on the first field (number - Main_Pt_ID) by itself, it
works OK....When I do a search on the last field (Test - Name Field - Lname)
by itself, it works OK....But when I do a search on the second field (number
- Arthur_ID) by itself, nothing happens. Same with the third Field (Date -
DOB)....But the 2nd and 3rd fields work beautifully if used in conjunction
with the First field only....Please help me fix this glitch:
Dim sSearch As String
Dim sql As String
Dim strSQL() As String
If IsNull(Me.sMain_Pt_ID) = True And IsNull(Me.sArthur_ID) = True And
IsNull(Me.sDOB) = True And IsNull(sLname) = True Then
MsgBox "You must enter a search value", vbOKOnly
Exit Sub
End If
'/build the search string
If IsNull(sMain_Pt_ID) = False Then
sSearch = "[Main_Pt_ID]=" & Me.sMain_Pt_ID
End If
If IsNull(sArthur_ID) = False Then
If IsNull(sSearch) = False Then
sSearch = sSearch & " Or [Arthur_ID]=" & Me.sArthur_ID
Else
sSearch = "[Arthur_ID]=" & Me.sArthur_ID
End If
End If
If Not IsNull(Me.sDOB) Then
If IsNull(sSearch) = False Then
sSearch = sSearch & " Or [DOB]= #" & Me.sDOB & "#"
Else
sSearch = "[DOB]= #" & Me.sDOB & "#"
End If
End If
'Dim strSQL() As String
If Len(Nz(Me.sLname, "")) > 0 Then
strSQL() = Split(Me.sLname & ",,", ",")
strSQL(0) = Trim(strSQL(0))
strSQL(1) = Trim(strSQL(1))
If Len(strSQL(0)) > 0 Then
strSQL(0) = "([LName] = '" & strSQL(0) & "' "
If Len(strSQL(1)) > 0 Then
strSQL(0) = strSQL(0) & _
" AND [Fname] LIKE '" & strSQL(1) & "*' " ' <- Change * to %
End If
strSQL(0) = strSQL(0) & ") "
If Len(Nz(sSearch, "")) = 0 Then
sSearch = strSQL(0)
Else
sSearch = sSearch & " OR " & strSQL(0)
End If
End If
End If
'MsgBox sSearch
sql = "SELECT Main_Pt_ID, Arthur_ID, FName, LName, DOB, Pt_Type,
Head_Fam_member_ID FROM Tbl_Main_Pt WHERE " & sSearch
Me.ListFind.RowSource = sql