473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Query by form problem

I'd appreciate any help I can get. I'm not sure what I'm doing wrong,
but....
I've searched these groups for some solutions but no luck.

I have an unbound form (frmSearch), with several unbound text boxes on
it and a command button bound to a macro which fires off a parameter
query based on the criteria/string that the user types into the text
boxes on frmSearch. My goal is to create a search form where the user
can search by any of the fields on the search form, hit the "Search"
button. I've set everything up according to Microsoft Article 304428,
but no luck.

I have it all set up, but after I run the query one time, the criteria
in the query changes. Below is the original code before I run the
query for the first time.

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] &
"*" Or (Entity.FullName) Like [Forms]![frmSearch]![FullName] Is Null)
AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*" Or
(Entity.Address) Like [Forms]![frmSearch]![Address] Is Null) AND
((Entity.State) Like [Forms]![frmSearch]![State] Or (Entity.State) Like
[Forms]![frmSearch]![State] Is Null) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*" Or (Entity.Zip) Like
[Forms]![frmSearch]![Zip] Is Null));

After I fire off the query the first time, the criteria in the query is
all changed. I'd paste it here but it'd take several screen scrolls to
get it all. Basically, Access places several "Like Forms!....."
criteria within each field, then creates additional fields for each Is
Null criteria. Below is a sample of the SQL Access creates:

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] &
"*") AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] &
"*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*") AND
((Entity.State) Like [Forms]![frmSearch]![State]) AND ((Entity.Zip)
Like "*" & [Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[FullName])
Like [Forms]![frmSearch]![FullName]) Is Null)) OR (((Entity.FullName)
Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.State)
Like [Forms]![frmSearch]![State]) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[Address])
bla bla bla

I'm just trying to create a simple query based on a form, with 1-5
criteria that the user types in. What am I doing wrong? I know it's
possible because others have done it and I've followed the tips from
others in this groups, but I can't get it for some reason.

Any help would be appreciated.

dskillingstad

Nov 13 '05 #1
3 1960
Yes, we regularly see people doing this kind of thing, but it is not very
efficient approach.

Here's another alternative. This search form consists of lots of unbound
controls in the Form Header, where the user can enter a value to match for
FullName, Address, and so on. It has a Search button, and clicking this
button build the SQL statement, and assigns it to the search form. If you
set up the search form to be a continuous form, you can then see all the
results in the detail section, and then double-click one to move your
original form to the found record.

The Click of the command button builds the WHERE clause from the non-blank
boxes like this:

Private Sub cmdSearch_Click()
dim strWhere As String 'The WHERE clause of the SQL statement.
Dim lngLen As Long 'Length of string.
Const strcStub = "SELECT FullName, Address, State, Zip FROM Entity WHERE
("
Const strcTail = ") ORDER BY FullName;"

If Not IsNull(Me.txtFindFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullName & """)
AND "
End If

If Not IsNull(Me.txtFindAddress) Then
strWhere = strWhere & "([Address] Like ""*" & Me.txtFindAddress &
"*"") AND "
End If

'etc for the other boxes.

lngLen = Len(strWhere) - 5 'without trailing " AND "
If lngLen <= 0 Then
MsgBox "No criteria"
Else
strWhere = Left$(strWhere, lngLen)
Me.RecordSource = strcStub & strWhere & strTail
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<ds***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I'd appreciate any help I can get. I'm not sure what I'm doing wrong,
but....
I've searched these groups for some solutions but no luck.

I have an unbound form (frmSearch), with several unbound text boxes on
it and a command button bound to a macro which fires off a parameter
query based on the criteria/string that the user types into the text
boxes on frmSearch. My goal is to create a search form where the user
can search by any of the fields on the search form, hit the "Search"
button. I've set everything up according to Microsoft Article 304428,
but no luck.

I have it all set up, but after I run the query one time, the criteria
in the query changes. Below is the original code before I run the
query for the first time.

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] &
"*" Or (Entity.FullName) Like [Forms]![frmSearch]![FullName] Is Null)
AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*" Or
(Entity.Address) Like [Forms]![frmSearch]![Address] Is Null) AND
((Entity.State) Like [Forms]![frmSearch]![State] Or (Entity.State) Like
[Forms]![frmSearch]![State] Is Null) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*" Or (Entity.Zip) Like
[Forms]![frmSearch]![Zip] Is Null));

After I fire off the query the first time, the criteria in the query is
all changed. I'd paste it here but it'd take several screen scrolls to
get it all. Basically, Access places several "Like Forms!....."
criteria within each field, then creates additional fields for each Is
Null criteria. Below is a sample of the SQL Access creates:

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] &
"*") AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] &
"*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*") AND
((Entity.State) Like [Forms]![frmSearch]![State]) AND ((Entity.Zip)
Like "*" & [Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[FullName])
Like [Forms]![frmSearch]![FullName]) Is Null)) OR (((Entity.FullName)
Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.State)
Like [Forms]![frmSearch]![State]) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[Address])
bla bla bla

I'm just trying to create a simple query based on a form, with 1-5
criteria that the user types in. What am I doing wrong? I know it's
possible because others have done it and I've followed the tips from
others in this groups, but I can't get it for some reason.

Any help would be appreciated.

dskillingstad

Nov 13 '05 #2
Thanks for the reponse. I changed the code to match my textbox names
and had several errors and could not get it to work.First there were
problems with the WHERE ("Const strcTail...) line, and then errors on
every "If Not IsNull(.....") line. By the way, I'm assuming (bad idea
on my part) that """ should be "*" and ""*" should be
"*"...right....wrong??

I'm not a programming person, but I can follow what you are doing, I am
just not proficient enough in coding to understand syntax etc.

Thanks for your help. Looks like I'll be creating multiple queries.

dskillingstad
Allen Browne wrote:
Yes, we regularly see people doing this kind of thing, but it is not very efficient approach.

Here's another alternative. This search form consists of lots of unbound controls in the Form Header, where the user can enter a value to match for FullName, Address, and so on. It has a Search button, and clicking this button build the SQL statement, and assigns it to the search form. If you set up the search form to be a continuous form, you can then see all the results in the detail section, and then double-click one to move your original form to the found record.

The Click of the command button builds the WHERE clause from the non-blank boxes like this:

Private Sub cmdSearch_Click()
dim strWhere As String 'The WHERE clause of the SQL statement.
Dim lngLen As Long 'Length of string.
Const strcStub = "SELECT FullName, Address, State, Zip FROM Entity WHERE ("
Const strcTail = ") ORDER BY FullName;"

If Not IsNull(Me.txtFindFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullName & """) AND "
End If

If Not IsNull(Me.txtFindAddress) Then
strWhere = strWhere & "([Address] Like ""*" & Me.txtFindAddress & "*"") AND "
End If

'etc for the other boxes.

lngLen = Len(strWhere) - 5 'without trailing " AND "
If lngLen <= 0 Then
MsgBox "No criteria"
Else
strWhere = Left$(strWhere, lngLen)
Me.RecordSource = strcStub & strWhere & strTail
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<ds***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I'd appreciate any help I can get. I'm not sure what I'm doing wrong, but....
I've searched these groups for some solutions but no luck.

I have an unbound form (frmSearch), with several unbound text boxes on it and a command button bound to a macro which fires off a parameter query based on the criteria/string that the user types into the text boxes on frmSearch. My goal is to create a search form where the user can search by any of the fields on the search form, hit the "Search" button. I've set everything up according to Microsoft Article 304428, but no luck.

I have it all set up, but after I run the query one time, the criteria in the query changes. Below is the original code before I run the
query for the first time.

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] & "*" Or (Entity.FullName) Like [Forms]![frmSearch]![FullName] Is Null) AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*" Or (Entity.Address) Like [Forms]![frmSearch]![Address] Is Null) AND
((Entity.State) Like [Forms]![frmSearch]![State] Or (Entity.State) Like [Forms]![frmSearch]![State] Is Null) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*" Or (Entity.Zip) Like
[Forms]![frmSearch]![Zip] Is Null));

After I fire off the query the first time, the criteria in the query is all changed. I'd paste it here but it'd take several screen scrolls to get it all. Basically, Access places several "Like Forms!....."
criteria within each field, then creates additional fields for each Is Null criteria. Below is a sample of the SQL Access creates:

SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
FROM Entity
WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND ((Entity.Zip)
Like "*" & [Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[FullName]) Like [Forms]![frmSearch]![FullName]) Is Null)) OR (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND ((Entity.Zip) Like "*" &
[Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[Address])
bla bla bla

I'm just trying to create a simple query based on a form, with 1-5
criteria that the user types in. What am I doing wrong? I know it's possible because others have done it and I've followed the tips from others in this groups, but I can't get it for some reason.

Any help would be appreciated.

dskillingstad


Nov 13 '05 #3
The constants are the stub (SELECT and FROM clauses etc) and the tail (ORDER
BY clause) of the SQL stament, which you need so you can patch the WHERE
clause between them. You can mock up a query, and swith it to SQL View (View
menu) to see an example of the statement you are trying to create.

The IsNull() has to wrap around the name of an unbound control (text box) on
your form.

The extra quotes are intended exactly as they are.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<ds***********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thanks for the reponse. I changed the code to match my textbox names
and had several errors and could not get it to work.First there were
problems with the WHERE ("Const strcTail...) line, and then errors on
every "If Not IsNull(.....") line. By the way, I'm assuming (bad idea
on my part) that """ should be "*" and ""*" should be
"*"...right....wrong??

I'm not a programming person, but I can follow what you are doing, I am
just not proficient enough in coding to understand syntax etc.

Thanks for your help. Looks like I'll be creating multiple queries.

dskillingstad
Allen Browne wrote:
Yes, we regularly see people doing this kind of thing, but it is not

very
efficient approach.

Here's another alternative. This search form consists of lots of

unbound
controls in the Form Header, where the user can enter a value to

match for
FullName, Address, and so on. It has a Search button, and clicking

this
button build the SQL statement, and assigns it to the search form. If

you
set up the search form to be a continuous form, you can then see all

the
results in the detail section, and then double-click one to move your

original form to the found record.

The Click of the command button builds the WHERE clause from the

non-blank
boxes like this:

Private Sub cmdSearch_Click()
dim strWhere As String 'The WHERE clause of the SQL statement.
Dim lngLen As Long 'Length of string.
Const strcStub = "SELECT FullName, Address, State, Zip FROM

Entity WHERE
("
Const strcTail = ") ORDER BY FullName;"

If Not IsNull(Me.txtFindFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullName

& """)
AND "
End If

If Not IsNull(Me.txtFindAddress) Then
strWhere = strWhere & "([Address] Like ""*" &

Me.txtFindAddress &
"*"") AND "
End If

'etc for the other boxes.

lngLen = Len(strWhere) - 5 'without trailing " AND "
If lngLen <= 0 Then
MsgBox "No criteria"
Else
strWhere = Left$(strWhere, lngLen)
Me.RecordSource = strcStub & strWhere & strTail
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<ds***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
> I'd appreciate any help I can get. I'm not sure what I'm doing wrong, > but....
> I've searched these groups for some solutions but no luck.
>
> I have an unbound form (frmSearch), with several unbound text boxes on > it and a command button bound to a macro which fires off a parameter > query based on the criteria/string that the user types into the text > boxes on frmSearch. My goal is to create a search form where the user > can search by any of the fields on the search form, hit the "Search" > button. I've set everything up according to Microsoft Article 304428, > but no luck.
>
> I have it all set up, but after I run the query one time, the criteria > in the query changes. Below is the original code before I run the
> query for the first time.
>
> SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
> FROM Entity
> WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] & > "*" Or (Entity.FullName) Like [Forms]![frmSearch]![FullName] Is Null) > AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*" Or > (Entity.Address) Like [Forms]![frmSearch]![Address] Is Null) AND
> ((Entity.State) Like [Forms]![frmSearch]![State] Or (Entity.State) Like > [Forms]![frmSearch]![State] Is Null) AND ((Entity.Zip) Like "*" &
> [Forms]![frmSearch]![Zip] & "*" Or (Entity.Zip) Like
> [Forms]![frmSearch]![Zip] Is Null));
>
> After I fire off the query the first time, the criteria in the query is > all changed. I'd paste it here but it'd take several screen scrolls to > get it all. Basically, Access places several "Like Forms!....."
> criteria within each field, then creates additional fields for each Is > Null criteria. Below is a sample of the SQL Access creates:
>
> SELECT Entity.FullName, Entity.Address, Entity.State, Entity.Zip
> FROM Entity
> WHERE (((Entity.FullName) Like "*" & [Forms]![frmSearch]![FullName] & > "*") AND ((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & > "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
> ((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
> (((Entity.Address) Like "*" & [Forms]![frmSearch]![Address] & "*") AND > ((Entity.State) Like [Forms]![frmSearch]![State]) AND ((Entity.Zip)
> Like "*" & [Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[FullName]) > Like [Forms]![frmSearch]![FullName]) Is Null)) OR (((Entity.FullName) > Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.State) > Like [Forms]![frmSearch]![State]) AND ((Entity.Zip) Like "*" &
> [Forms]![frmSearch]![Zip] & "*") AND ((([Entity].[Address])
> bla bla bla
>
> I'm just trying to create a simple query based on a form, with 1-5
> criteria that the user types in. What am I doing wrong? I know it's > possible because others have done it and I've followed the tips from > others in this groups, but I can't get it for some reason.
>
> Any help would be appreciated.
>
> dskillingstad

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value ($site) is derived from a db query. This works fine....
7
by: Mike | last post by:
I have to pass a date from a form to a report query. I have written a query that works fine when I execute from SQL view, But I dont know how to pass a value from the form to this query. SELECT...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
13
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
0
by: Stephen D Cook | last post by:
I have a form linked to a table. In the form I have a textbox, a command button, and a query subform. I dragged the query onto the form to create the query subform. The query has a field named...
10
by: Farmer | last post by:
Hello Folks, can anybody tell me how to do the following very simple thing: I have a table that has say departments, divisions and units of an organisation and what i am trying to do is find a...
4
by: Jimmy | last post by:
I have a form with a command button on it that is supposed to open up a report and use a query to populate it. DoCmd.OpenReport "rptMailingList", acViewPreview, "qryMailingListChristmas" ...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
9
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.