Multi-field searches in Access with blank fields - ms-access

I have a table in access (simplified) with fields of sex, first name, last name, and phone number.
Sex First Last Phone
F Alice Smith
M Bob 111-111-1111
F Smithe 111-111-1112
M Charlie Smith
F Eve Drop 111-111-1113
I have created a form along with a query to search for the records, using the criteria of
Is Null Or Like "*" & [Forms]![Search]![Criteria] & "*"
where Search is the name of my form, and Criteria is the name of the individual entries on my form.
Right now, if I search for Sex F and the last name Smith, and leave the first and last name fields blank, I want to be able to see the records for "Alice Smith" and "Smithe". But instead, I only get the record of "Alice Smith". Furthermore, if I search for Sex F and phone number 111, I get results for Alice, Smithe, and Eve. Similarly, if I just search for phone number alone, I will get all five records.
I'm assuming that I'm doing something wrong here, but I can't tell what it is. Based on the conditional logic of or, the results should be either Null or 111 (and similar for the other fields), so I guess it is behaving as expected. I've tried xor however, and I get the same result (and even if it does work the way I'm thinking it would, it would seem bad since a blank entry would be interpreted as null instead of "search every entry". I've tried using iif, which should be the correct way to do this, but it appears to not work when placed in the criteria field. Am I missing something here?

Null is the special value (with the meaning not-known) and it is NOT the same as a blank text field, which has the value "" (empty string).
So adjust your criteria to accept the value "".

For multi-field search use criteria for each field like this:
WHERE
([Sex] Like "*" & [Forms]![Search]![CriteriaSex] & "*"
OR Nz([Forms]![Search]![CriteriaSex],"")="")
AND ([First] Like "*" & [Forms]![Search]![CriteriaFirst] & "*"
OR Nz([Forms]![Search]![CriteriaLast],"")="")
AND ([Last] Like "*" & [Forms]![Search]![CriteriaLast] & "*"
OR Nz([Forms]![Search]![CriteriaLast],"")="")
AND ([Phone] Like "*" & [Forms]![Search]![CriteriaPhone] & "*"
OR Nz([Forms]![Search]![CriteriaPhone],"")="")
In this case every condition will be TRUE if corresponding criteria field is empty.

Another way to build this query would be to build a string based upon which fields are populated.
q = "SELECT * FROM table "
w = ""
IF(nz([Forms]![Search]![Criteria1],"")<> "") THEN
w = "WHERE [table]![field] like '*" & [Forms]![Search]![Criteria1] & "*'"
END IF
IF(nz([Forms]![Search]![Criteria2],"")<> "") THEN
IF (w="") THEN
w=" WHERE "
ELSE
w=w & " AND "
END
w = w & " [table]![field] like '*" & [Forms]![Search]![Criteria2] & "*'"
END IF
IF(nz([Forms]![Search]![Criteria3],"")<> "") THEN
IF (w="") THEN
w=" WHERE "
ELSE
w=w & " AND "
END
w = w & " [table]![field] like '*" & [Forms]![Search]![Criteria3] & "*'"
END IF
q = q & w

Related

Select with 2 conditions

can you please help me to fix the following code:
The code is trying to get data according to the 2 conditions:
Select data when a checkbox and a value form a drop down list is checked
Select data when only a checkbox is checked ( this one is working fine so I am not going to list it)
If I select multiple check boxes the values are shown properly but when I try to add the second condition no values are shown.
Date looks like this DD/MM/YY
I am using vb and mysql(DB)
If c1check AndAlso ComboBox2.SelectedItem > 0 Then
searchQuery = "Select USERS_NUMBER FROM DB WHERE EXPIRATION_DATE LIKE '%" _
& String.Join("%'and EXPIRATION_DATE LIKE " + "'%", myList) _
& + "%' AND RIGHT(EXPIRATION_DATE, 3) = '%" & Trim(ComboBox2.SelectedItem.ToString) & "'"

Concat Related Function Returns Error

I am attempting to use Allen Browne's ConcatRelated() function, but I am getting an error of:
Error 3061: Too few parameters. Expected 1
Below is the syntax I input into my query ->
ConcatRelated("Product","[_ProdInfo]","OrderNumber = " & [OrderNumber])
What should I change so that this does not produce the error and displays the results I am after?
Further explanation:
Field Name is Product
Table Name Is _ProdInfo
The field to match on is OrderNumber and it is a short text type
As ConcatRelated() link describe:
If the foreign key field is Text (not Number), include quote marks as
delimiters, e.g.:
"[ForeignKeyFieldName] = """ & [PrimaryKeyFieldName] & """"
And since your OrderNumber is a text field, add the needed quotes:
ConcatRelated("Product", "[_ProdInfo]", "OrderNumber = """ & [OrderNumber] & """)
Or with single quotes:
ConcatRelated("Product", "[_ProdInfo]", "OrderNumber = '" & [OrderNumber] & "'")

Change SSS Report Condition based on report Parameter

I spent some time looking around at other questions on the board, but didn't find anything similar so here it goes:
I have a report that runs up against the limits of CRM 2011 online in regards to the max number of characters in a Fetch Query. I would like to have a drop box that has an option for "All Contacts" or "Select up to Five Contacts" to avoid that issue.
What I would like to do is remove the condition from the Fetch statement if the "All Contacts" option is selected, and include the condition only if the select up to five is chosen.
How would I go about doing this?
Let me know if you have any further questions.
Nick
You can build your SQL using custom code because the SQL itself can be a string expression that you build manually. Let's say your SQL looks like this at the moment:
SELECT ThisField, ThatField
FROM Contacts
WHERE ContactId IN #Contacts
Let's change that to a string expression that builds the SQL:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& "WHERE ContactId IN #Contacts "
Now we need to pull the #Contacts parameter apart and only select by ContactId when there are five or less contacts selected:
Function ContactsSQL(ByVal parameter As Parameter) AS String
Dim Result As String
Result = ""
If parameter.IsMultiValue Then
If parameter.Count <= 5 Then
Result = "WHERE ContactId IN ("
For i As integer = 0 To parameter.Count-1
Result = Result + CStr(parameter.Value(i)) + ", "
Next
Result = Left(Result, Result.Length - 2) + ") "
End If
End If
Return Result
End Function
This function builds the SQL WHERE clause to select the contacts only if five or less have been selected, otherwise it passes back an empty string. Now we use this function in our SQL expression:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& Code.ContactsSQL(Parameters!Contacts)
Note that you are passing the Contacts parameter object here, not the Value property. We access the Value property in the custom code function.

Multifield Search, Incorporating age range criteria?

I'm trying to add a multifield search into a form, but I ran into a jam. I have it figured out for my 4 text fields. Here's the code I use utilizing wildcards so that it searches all records if it's left blank
Like "*" & [Forms]![Patient Tracking]![FNSearch] & "*"
But now, I'm trying to add an age range feature. It's stored as a simple numerical field. I just need to create a way where you can enter an age range (lowest in textbox named minage, highest in maxage textbox) and have it also be ignored if left blank.
Any idea how to create this criteria?
this is the entire code of the query in sql:
SELECT [Patient Tracking].[First Name], [Patient Tracking].[Last Name], [Patient Tracking].City, [Patient Tracking].Sex, [Patient Tracking].Age
FROM [Patient Tracking]
WHERE ((([Patient Tracking].[First Name]) Like "*" & Forms![Patient Tracking]!FNSearch & "*") And (([Patient Tracking].[Last Name]) Like "*" & Forms![Patient Tracking]!lnsearch & "*") And (([Patient Tracking].City) Like "*" & Forms![Patient Tracking]!citysearch & "*") And (([Patient Tracking].Sex) Like "*" & Forms![Patient Tracking]!sexsearch & "*"))
I found a simpler method using the NZ Function,
Between Nz([Forms]![Patient Tracking]![minage],0) And Nz([Forms]![Patient Tracking]![maxage],10000) Or Is Null
thanks for the help

combobox rowsource based on forms record source (distinct) field

I have a form record source set to a elaborate SQL select statement. That is working fine. If it helps to know, the form layout is Tabular. Here is an example of the data:
order carrier billto employee
1 smgd horm chrnic
2 axxm sele chrnic
3 smgd horm redned
4 mcta cron greand
5 mcta cron greand
Its basically unbilled order entries. I want a combo box to show distinct employee names (chrnic, redned, greand) based on the current records showing. I will be coding it to filter the form. Seems simple, but I am having trouble
Things I have tried:
Tried setting rowsource to me.recordsource, but get an
It appears that I would need to parse & edit that string
I copied the complex query & put as combo box record source & that worked to filter the form, so I know that filter logic is correct. I just want it to be dynamic so we only have to change SQL statement in one place if needed
"I have a form record source set to a elaborate SQL select statement."
Save that query as a named QueryDef. I will pretend you chose qryRecordSource as the name.
"I want a combo box to show distinct employee names ... based on the current records"
For the combo box row source use ...
SELECT DISTINCT employee
FROM qryRecordSource
ORDER BY 1;
And then to filter the form based on the combo selection, add a command button, cmdApplyFilter, and use this in its click event procedure ...
Me.Filter = "[employee] = '" & Me.YourComboName.Value & "'"
Me.FilterOn = True
If the employee names can include an apostrophe, use this for the Filter expression ...
Me.Filter = "[employee] = '" & _
Replace(Me.YourComboName.Value, "'", "''") & "'"
If you want to include a combo row to clear the filter, use a UNION query as the combo row source ...
SELECT "*** ALL ***" AS employee
FROM Dual
UNION
SELECT employee
FROM qryRecordSource
ORDER BY 1;
... where Dual is any table or query which returns just one row. Then in the command button click event you can do ...
If Me.YourComboName.Value = "*** ALL ***" Or _
IsNull(Me.YourComboName.Value) Then
Me.Filter = vbNullString
Me.FilterOn = False
Else
Me.Filter = "[employee] = '" & Me.YourComboName.Value & "'"
Me.FilterOn = True
End If
And actually you wouldn't even need the command button. You could set the Filter from the combo's AfterUpdate event.