473,545 Members | 1,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] &
"*" Or (Entity.FullNam e) Like [Forms]![frmSearch]![FullName] Is Null)
AND ((Entity.Addres s) 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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] &
"*") AND ((Entity.Addres s) Like "*" & [Forms]![frmSearch]![Address] &
"*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Addre ss) 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.FullN ame)
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 1969
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.txtFi ndFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullN ame & """)
AND "
End If

If Not IsNull(Me.txtFi ndAddress) Then
strWhere = strWhere & "([Address] Like ""*" & Me.txtFindAddre ss &
"*"") 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.goo glegroups.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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] &
"*" Or (Entity.FullNam e) Like [Forms]![frmSearch]![FullName] Is Null)
AND ((Entity.Addres s) 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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] &
"*") AND ((Entity.Addres s) Like "*" & [Forms]![frmSearch]![Address] &
"*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Addre ss) 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.FullN ame)
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.txtFi ndFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullN ame & """) AND "
End If

If Not IsNull(Me.txtFi ndAddress) Then
strWhere = strWhere & "([Address] Like ""*" & Me.txtFindAddre ss & "*"") 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.goo glegroups.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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] & "*" Or (Entity.FullNam e) Like [Forms]![frmSearch]![FullName] Is Null) AND ((Entity.Addres s) 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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] & "*") AND ((Entity.Addres s) Like "*" & [Forms]![frmSearch]![Address] & "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
(((Entity.Addre ss) 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.FullN ame) 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******** *************@o 13g2000cwo.goog legroups.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.txtFi ndFullName) Then
strWhere = strWhere & "([FullName] = """ & Me.txtFindFullN ame

& """)
AND "
End If

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

Me.txtFindAddre ss &
"*"") 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.goo glegroups.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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] & > "*" Or (Entity.FullNam e) Like [Forms]![frmSearch]![FullName] Is Null) > AND ((Entity.Addres s) 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.FullN ame) Like "*" & [Forms]![frmSearch]![FullName] & > "*") AND ((Entity.Addres s) Like "*" & [Forms]![frmSearch]![Address] & > "*") AND ((Entity.State) Like [Forms]![frmSearch]![State]) AND
> ((Entity.Zip) Like "*" & [Forms]![frmSearch]![Zip] & "*")) OR
> (((Entity.Addre ss) 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.FullN ame) > 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
2868
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. Value selected is used as the 'where' clause of the 2nd query. If $site is a single word, the 2nd query works like a charm. If $site is more than one...
7
2420
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 Production.Production FROM Production WHERE (((Production.Date)=Forms!ReportForm!reportDate) And ((Production.ShiftName)="Shift1"));
1
2902
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 2000. The access file is in access 2000 format. I have a form that will hold the relevent parameters for the query/report that reports the statistics...
4
7733
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, but I can't update record in query or in form. I believe the problem is due to the source query. In source query, there is a filter to show the...
13
4209
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 solution here - until now. This one is driving me crazy. I am making my first attempt at creating a runtime application. I am using Access 2003...
0
1300
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 SerialNumber. In the criteria for the query, I have !! which links the query to the textbox in the main form. Code for the form: Private Sub...
10
5274
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 divison. One department has many divisions, and one division many units. I am think therefore a SELECT DISTINCT query on the division would make sense...
4
3812
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" qryMailingListChristmas sql: SELECT DISTINCTROW tblClients.BillingName, tblClients.AccountType, tblClients.BillingAddress, tblClients.Contact,...
22
31154
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 June, and will return all records in that month.
9
2758
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 from searching on the internet and books but the problem is when i run the command button "Export" it just only open the Blank Spreadsheet, no ...
0
7457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4941
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.