问题
I'm trying to create a pretty bare-bones search tool to pull records from a table ("ApplicationTable"). The user is presented with a form containing a number of textbox controls, each one corresponding to a field in the table (ApplicationID, FirstName, LastName, etc.) They enter data into whatever field(s) they wish, then click a button. The search should return each record for which a corresponding field contains the search parameter, and ignores any search field that is blank. Data is displayed in a report that pops up.
For example, if you enter "A17" into the ApplicationID control, it'll return all records for which the ApplicationID field contains "A17". If you enter "A17" into ApplicationID and "John" into FirstName, it'll return all records for which the ApplicationID field contains "A17" and the FirstName field contains "John". If you hit the search button with all fields blank, it just returns all records in the table.
Unfortunately, for the life of me, I cannot get the SQL right to make this thing powered by a simple query, and so I'm having to use vba. But I don't want to, because it's making a lot of things more complicated than it needs to be. (e.g., I'm trying to export the results to Excel, which is a pain since transferspreadsheet can't accept dynamic SQL.)
SO!
Here is the vba code I'm using currently:
Dim QueryStr As String
Dim AddAnd As Boolean
AddAnd = False
If Not IsNull([Forms]![ClientSearchForm]![ApplicationID]) Then
QueryStr = QueryStr & "((ApplicationTable.ApplicationID) Like '*' &
[Forms]![ClientSearchForm]![ApplicationID] & '*')"
AddAnd = True
End If
If Not IsNull([Forms]![ClientSearchForm]![NevadaApplicationID]) Then
If AddAnd = True Then
QueryStr = QueryStr & " AND "
End If
QueryStr = QueryStr & "((ApplicationTable.NevadaApplicationID) Like '*'
& [Forms]![ClientSearchForm]![NevadaApplicationID] & '*')"
AddAnd = True
End If
If Not IsNull([Forms]![ClientSearchForm]![FirstName]) Then
If AddAnd = True Then
QueryStr = QueryStr & " AND "
End If
QueryStr = QueryStr & "((ApplicationTable.ClaimantFirstName) Like '*' &
[Forms]![ClientSearchForm]![FirstName] & '*')"
AddAnd = True
End If
If Not IsNull([Forms]![ClientSearchForm]![LastName]) Then
If AddAnd = True Then
QueryStr = QueryStr & " AND "
End If
QueryStr = QueryStr & "((ApplicationTable.ClaimantLastName) Like '*' &
[Forms]![ClientSearchForm]![LastName] & '*')"
AddAnd = True
End If
If Not IsNull([Forms]![ClientSearchForm]![AssignedStaff]) Then
If AddAnd = True Then
QueryStr = QueryStr & " AND "
End If
QueryStr = QueryStr & "((ApplicationTable.AssignedStaff) Like '*' &
[Forms]![ClientSearchForm]![AssignedStaff] & '*')"
AddAnd = True
End If
If Not IsNull([Forms]![ClientSearchForm]![VictimDescription]) Then
If AddAnd = True Then
QueryStr = QueryStr & " AND "
End If
QueryStr = QueryStr & "((ApplicationTable.VictimDescription) Like '*' &
[Forms]![ClientSearchForm]![VictimDescription] & '*')"
AddAnd = True
End If
DoCmd.OpenReport "ClientSearchQuery", acViewReport, , QueryStr
"ClientSearchQuery" is the core query I'm using, without any filters on it:
SELECT ApplicationTable.PrimaryAppID, ApplicationTable.ApplicationID, ApplicationTable.NevadaApplicationID,
ApplicationTable.EarliestReceivedDate, ApplicationTable.ClaimantFirstName, ApplicationTable.ClaimantLastName,
ApplicationTable.AssignedStaff, ApplicationTable.ContactDate, ApplicationTable.VictimDescription
FROM ApplicationTable;
Help? I assume I need to use iifs to get this thing going with just SQL, but I couldn't get the syntax right, and it would either throw an error or return the wrong results.
回答1:
Boolean logic to the rescue!
Make your static query like this, and you won't need any VBA.
SELECT stuff
FROM ApplicationTable
WHERE (ApplicationID Like '*' & [Forms]![ClientSearchForm]![ApplicationID] & '*'
OR [Forms]![ClientSearchForm]![ApplicationID] IS NULL)
AND (NevadaApplicationID Like '*' & [Forms]![ClientSearchForm]![NevadaApplicationID] & '*'
OR [Forms]![ClientSearchForm]![NevadaApplicationID] IS NULL)
AND ...
For each of the AND clauses, if the respective search control is NULL, then the clause is always TRUE.
Note: for a working VBA solution, see http://allenbrowne.com/ser-62.html
来源:https://stackoverflow.com/questions/46715023/trying-to-tailor-a-query-for-search-functionality-without-vba