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
Related
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) & "'"
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
I have tried to select all records from a table from date1 to date2, Example Jun 28, 2014 to Jan 05, 2015, for display. Basically, sort out selected records based on date criteria. Big thanks if anyone could point out my mistake.
What I tried to do here is allowed user to select specific dates from calendar to view the records
I have gone through all answers, but still couldn't find similar solution to mine. I think, there may be some error in my syntax.
sqlDateRangeSearch = "Select * from BatteryDataTable where ((BatteryDateChanged) <= ""*" & Me.FromDTPicker.Value & "*"")" & " and ((BatteryDateChanged) <= " & """*" & Me.ToDTPicker.Value & "*""));"
Me.RecordSource = sqlDateRangeSearch
I noticed you have wildcard characters in the search criteria for the query. If using dates, you'll want to avoid those. Also, you don't need quotes for dates in access queries. If you're dynamically creating SQL I'd use something like:
sqlDateRangeSearch = "Select * from BatteryDataTable where (BatteryDateChanged <= #" & Me.FromDTPicker.Value & "#)" & " and (BatteryDateChanged <= #" & Me.ToDTPicker.Value & "#));"
Just a side note, your comparison operators are the same. I think you want it to say something like (remember the "#" symbols):
...WHERE (Field1 >= #Date1# AND Field1 < #Date2#);
Hope that helps!
I am trying to get the following result in a query.
SELECT * FROM rosterTbl
WHERE (IIF( ISNULL([Forms]![ReportsGUI]![cmbCounsellor]), rosterTbl.CounsellorID<>null,rosterTbl.CounsellorID=[Forms]![ReportsGUI]![cmbCounsellor]
I know the above query is incorrect but I need the result: If cmbCounsellor is Null Then all counsellors Else specific counsellor.
Perhaps something like this will work:
="SELECT * FROM rosterTbl WHERE CounsellorID " & IIf(IsNull([Forms]![ReportsGUI]![cmbCounsellor], "IS NOT NULL", "= " & [Forms]![ReportsGUI]![cmbCounsellor])
edit re: comments
In this particular case the WHERE condition will be used to control the records to be included in a report, so the compete SELECT statement is not really required. Instead, the "WHERE" clause can be passed to the report via the WhereCondition argument of DoCmd.OpenReport, something like this:
DoCmd.OpenReport "MyReport", acViewPreview, , "CounsellorID " & IIf(IsNull([Forms]![ReportsGUI]![cmbCounsellor], "IS NOT NULL", "= " & [Forms]![ReportsGUI]![cmbCounsellor])
This is the way to do this within query:
SELECT * from rosterTbl
WHERE [Forms]![ReportsGUI]![cmbCounsellor] IS NULL
OR
rosterTbl.CounsellorID
= ([Forms]![ReportsGUI]![cmbCounsellor])
I created three table in MSAccess (PCH Info, Attorney zip-county, Medical-zip code-county). Each table have similar field of zip code, city and county. My Goal is to search for either of the zip code, city and county in the 3 table and return result meeting either one or 2 of the search criteria.
I ve already created 3 search field and build my query using UNION on the three table. But my problem is if I search specifically for a data it returns the results with other results which doesnt meet the criteria. See code below
SELECT [PCH Info].City, [PCH Info].[Zip Code], [PCH Info].County, "PCH Info"
FROM [PCH Info]
WHERE (([PCH Info].City) Like Forms!Search.qcity & "*") And (([PCH Info].[Zip Code]) Like Forms!Search.qcode & "*") And (([PCH Info].County) Like Forms!Search.qcounty & "*") OR (([PCH Info].[Network Member]) = Yes)
UNION
SELECT [Medical-Zip Code-County].City, [Medical-Zip Code-County].[ZIP Code], [Medical-Zip Code-County].County , "Medical-Zip Code-County"
FROM [Medical-Zip Code-County]
WHERE (([Medical-Zip Code-County].City) Like Forms!Search.qcity & "*") And (([Medical-Zip Code-County].[ZIP Code]) Like Forms!Search.qcode & "*") And (([Medical-Zip Code-County].County) Like Forms!Search.qcounty & "*")
UNION
SELECT [Attorney-Zip-County].City, [Attorney-Zip-County].[ZIP Code], [Attorney-Zip-County].[Country/Region], "Attorney-Zip-County"
FROM [Attorney-Zip-County]
WHERE (([Attorney-Zip-County].City) Like Forms!Search.qcity & "*") And (([Attorney-Zip-County].[ZIP Code]) Like Forms!Search.qcode & "*") And (([Attorney-Zip-County].[Country/Region]) Like Forms!Search.qcounty & "*");
My major challenge comes from the first query where PCH Info.Network Member = Yes, if i do a search to meet this criteria, it still give me results which does meet the criteria. What could I be doing wrong or is the UNION query the problem? thank you.
How about:
SELECT City, [ZIP Code], County, Source, [Network Member]
FROM (
SELECT p.City, p.[Zip Code], p.County,
"PCH Info" As Source, p.[Network Member]
FROM [PCH Info] p
UNION ALL
SELECT m.City, m.[ZIP Code], m.County ,
"Medical-Zip Code-County" As Source, False As [Network Member]
FROM [Medical-Zip Code-County] m
UNION ALL
SELECT a.City, a.[ZIP Code], a.[Country/Region],
"Attorney-Zip-County" As Source, False As [Network Member]
FROM [Attorney-Zip-County] a) As r
WHERE (r.City Like Forms!Search!qcity & "*"
And r.[Zip Code] Like Forms!Search!qcode & "*"
And r.County Like Forms!Search!qcounty & "*")
OR r.[Network Member] = Yes