473,626 Members | 3,285 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query works, but SQL statment doesn't when pasted into a procedure

Hello and Thanks in advance for any help. I am using Access 2000.

I have a data entry form that opens a main form (using the On Click
event of the combo box [cboDESeledct]) with tab controls and 1 subform
on the last tab control. The main form is opened using a where clause
to restrict the contact type. The subform is built on a separate
table that lists other contact types that the contact could be a
member of. Example a volunteer could also be a donor or a vendor,
etc. I would like to open the form where the contact type in either
the main form or the subform is the same.

I can get correct results using a query, but when I paste the SQL
statement into the procedure I receive an error "Syntax error. in
query expression...

Here is the code (the strLinkCriteria is all on one line):

Private Sub cboDESelect_Cli ck()
On Error GoTo Err_cboDESelect _Click

Dim stDocName As String

Dim stLinkCriteria As String

stDocName = "frmContact s"
stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect]))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=[forms]![frmDataEntryMen u]![cboDESelect]));"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

I tried to remove the parens and join the parts of the statement with
"&" (as per other posts on the subject) but received an error of
Object Required.

Again, Thanks for any and all help. Liz
Nov 13 '05 #1
6 1680
AS [member of] in your statement returns a Syntax error for me.

em******@bellso uth.net (Liz Malcolm) wrote in message news:<ae******* *************** ****@posting.go ogle.com>...
Hello and Thanks in advance for any help. I am using Access 2000.

I have a data entry form that opens a main form (using the On Click
event of the combo box [cboDESeledct]) with tab controls and 1 subform
on the last tab control. The main form is opened using a where clause
to restrict the contact type. The subform is built on a separate
table that lists other contact types that the contact could be a
member of. Example a volunteer could also be a donor or a vendor,
etc. I would like to open the form where the contact type in either
the main form or the subform is the same.

I can get correct results using a query, but when I paste the SQL
statement into the procedure I receive an error "Syntax error. in
query expression...

Here is the code (the strLinkCriteria is all on one line):

Private Sub cboDESelect_Cli ck()
On Error GoTo Err_cboDESelect _Click

Dim stDocName As String

Dim stLinkCriteria As String

stDocName = "frmContact s"
stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect]))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=[forms]![frmDataEntryMen u]![cboDESelect]));"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

I tried to remove the parens and join the parts of the statement with
"&" (as per other posts on the subject) but received an error of
Object Required.

Again, Thanks for any and all help. Liz

Nov 13 '05 #2
Hi Malcolm,

Try this as your SQL string - watch for word wrap!

stLinkCriteria = "SELECT tblContacts.*, " & _
"tblContacts.ID _contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] " & _
"FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON " & _
"tblContacts.ID _contact = tblMulti_Contac t_type.ID_conta ct " & _
"WHERE ((tblContacts.I D_contact_type) ='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & _
"')) OR (((tblMulti_Con tact_type.ID_co ntact_type)='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & "'));"

The above would take the value of your combo boxes and use the value
as criteria.

Hope this helps.

Cheers,

em******@bellso uth.net (Liz Malcolm) wrote in message news:<ae******* *************** ****@posting.go ogle.com>...
Hello and Thanks in advance for any help. I am using Access 2000.

I have a data entry form that opens a main form (using the On Click
event of the combo box [cboDESeledct]) with tab controls and 1 subform
on the last tab control. The main form is opened using a where clause
to restrict the contact type. The subform is built on a separate
table that lists other contact types that the contact could be a
member of. Example a volunteer could also be a donor or a vendor,
etc. I would like to open the form where the contact type in either
the main form or the subform is the same.

I can get correct results using a query, but when I paste the SQL
statement into the procedure I receive an error "Syntax error. in
query expression...

Here is the code (the strLinkCriteria is all on one line):

Private Sub cboDESelect_Cli ck()
On Error GoTo Err_cboDESelect _Click

Dim stDocName As String

Dim stLinkCriteria As String

stDocName = "frmContact s"
stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect]))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=[forms]![frmDataEntryMen u]![cboDESelect]));"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

I tried to remove the parens and join the parts of the statement with
"&" (as per other posts on the subject) but received an error of
Object Required.

Again, Thanks for any and all help. Liz

Nov 13 '05 #3
Hello Again

I used the sample suggested, but it won't go past the OR statement.
If I strip out just the OR statement I get an "Enter Parameter Value"
for tblMulti_Contac t_type.ID_Conta ct_type. This is the 2nd table that
has 3 fields; ID, ID_Contact_type as a lookup field and contact ID.
What could I be missing or doing wrong?

Thanks, Liz
ph****@globalne t.co.uk (Shuffs) wrote in message news:<54******* *************** ****@posting.go ogle.com>...
Hi Malcolm,

Try this as your SQL string - watch for word wrap!

stLinkCriteria = "SELECT tblContacts.*, " & _
"tblContacts.ID _contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] " & _
"FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON " & _
"tblContacts.ID _contact = tblMulti_Contac t_type.ID_conta ct " & _
"WHERE ((tblContacts.I D_contact_type) ='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & _
"')) OR (((tblMulti_Con tact_type.ID_co ntact_type)='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & "'));"

The above would take the value of your combo boxes and use the value
as criteria.

Hope this helps.

Cheers,

em******@bellso uth.net (Liz Malcolm) wrote in message news:<ae******* *************** ****@posting.go ogle.com>...
Hello and Thanks in advance for any help. I am using Access 2000.

I have a data entry form that opens a main form (using the On Click
event of the combo box [cboDESeledct]) with tab controls and 1 subform
on the last tab control. The main form is opened using a where clause
to restrict the contact type. The subform is built on a separate
table that lists other contact types that the contact could be a
member of. Example a volunteer could also be a donor or a vendor,
etc. I would like to open the form where the contact type in either
the main form or the subform is the same.

I can get correct results using a query, but when I paste the SQL
statement into the procedure I receive an error "Syntax error. in
query expression...

Here is the code (the strLinkCriteria is all on one line):

Private Sub cboDESelect_Cli ck()
On Error GoTo Err_cboDESelect _Click

Dim stDocName As String

Dim stLinkCriteria As String

stDocName = "frmContact s"
stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect]))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=[forms]![frmDataEntryMen u]![cboDESelect]));"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

I tried to remove the parens and join the parts of the statement with
"&" (as per other posts on the subject) but received an error of
Object Required.

Again, Thanks for any and all help. Liz

Nov 13 '05 #4
Liz Malcolm wrote:
Hello Again


I've experienced exactly the same problem. For example, I write a query
in the query builder and it works fine. But when I take the SQL from
the SQL view and paste it into a procedure, carefully making sure the
double quotes are double quoted where necessary, the procedure doesn't work.

I found the problem to lie in the use of the query by form. That's
where you have
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect],
ie, you want the tblContacts.ID_ contact_type to be the value of whatever
is in the form frmDataEntryMen u in the control cboDESelect.

What I do now is make sure I have a variable for anything that I use the
query by form method for in an Access query.

So here's what I would do with what you supplied on your initial post -
I assumed the value of cboDESelect was a text versus a number. note the
addition and treatment of stValue. This works all the time for me:

Dim stDocName As String
Dim stLinkCriteria As String

dim stValue as string

stDocName = "frmContact s"

stValue = forms!frmDataEn tryMenu.cboDESe lect

stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=""" & stValue & """ ))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=""" & stValue & """ ));"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

--
Tim
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #5
Tim,

The values are numbers (autonumbers to be exact) so I replaced the
triple double quotes with a single double quote. But I still get a
Syntax error. in query expression.... OR (((tblMu'.

The first WHERE clause works by itself.

The second WHERE clause by itself gives the "Enter Parameter Value"

I followed another suggestion to have the hidden field in the combox
box displayed on the form but that didn't solve the problem (and it
isn't user friendly). The second criteria is from a subform linked by
contact ID to the main form. Not all contacts have a record in this
subform, only those that have two contact types, ex: Volunteer and
Donor, etc. I tried to throw in a Not IsNull but that gave me all
records. I tried to put in the column number but that doesn't work
either.

I tried Allen Brownes suggestion to debug.print the criteria and put
that into a new query which works and gives me the records I'm looking
for. I've been reading everything I could find in the NG for the past
3 days, but cannot seem to find my mistake.

Hoping someone can help with this.

Again Thanks Liz
em******@bellso uth.net (Liz Malcolm) wrote in message news:<ae******* *************** ****@posting.go ogle.com>...
Hello Again

I used the sample suggested, but it won't go past the OR statement.
If I strip out just the OR statement I get an "Enter Parameter Value"
for tblMulti_Contac t_type.ID_Conta ct_type. This is the 2nd table that
has 3 fields; ID, ID_Contact_type as a lookup field and contact ID.
What could I be missing or doing wrong?

Thanks, Liz
ph****@globalne t.co.uk (Shuffs) wrote in message news:<54******* *************** ****@posting.go ogle.com>...
Hi Malcolm,

Try this as your SQL string - watch for word wrap!

stLinkCriteria = "SELECT tblContacts.*, " & _
"tblContacts.ID _contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] " & _
"FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON " & _
"tblContacts.ID _contact = tblMulti_Contac t_type.ID_conta ct " & _
"WHERE ((tblContacts.I D_contact_type) ='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & _
"')) OR (((tblMulti_Con tact_type.ID_co ntact_type)='" & _
[forms]![frmDataEntryMen u]![cboDESelect] & "'));"

The above would take the value of your combo boxes and use the value
as criteria.

Hope this helps.

Cheers,

em******@bellso uth.net (Liz Malcolm) wrote in message news:<ae******* *************** ****@posting.go ogle.com>...
Hello and Thanks in advance for any help. I am using Access 2000.

I have a data entry form that opens a main form (using the On Click
event of the combo box [cboDESeledct]) with tab controls and 1 subform
on the last tab control. The main form is opened using a where clause
to restrict the contact type. The subform is built on a separate
table that lists other contact types that the contact could be a
member of. Example a volunteer could also be a donor or a vendor,
etc. I would like to open the form where the contact type in either
the main form or the subform is the same.

I can get correct results using a query, but when I paste the SQL
statement into the procedure I receive an error "Syntax error. in
query expression...

Here is the code (the strLinkCriteria is all on one line):

Private Sub cboDESelect_Cli ck()
On Error GoTo Err_cboDESelect _Click

Dim stDocName As String

Dim stLinkCriteria As String

stDocName = "frmContact s"
stLinkCriteria = "SELECT tblContacts.*,
tblContacts.ID_ contact_type, tblMulti_Contac t_type.ID_conta ct_type AS
[member of] FROM tblContacts LEFT JOIN tblMulti_Contac t_type ON
tblContacts.ID_ contact = tblMulti_Contac t_type.ID_conta ct WHERE
(((tblContacts. ID_contact_type )=[forms]![frmDataEntryMen u]![cboDESelect]))
OR (((tblMulti_Con tact_type.ID_co ntact_type)=[forms]![frmDataEntryMen u]![cboDESelect]));"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cboDESelec t_Click:
Exit Sub

Err_cboDESelect _Click:
MsgBox Err.Description
Resume Exit_cboDESelec t_Click

End Sub

I tried to remove the parens and join the parts of the statement with
"&" (as per other posts on the subject) but received an error of
Object Required.

Again, Thanks for any and all help. Liz

Nov 13 '05 #6
Liz Malcolm wrote:
The values are numbers (autonumbers to be exact) so I replaced the
triple double quotes with a single double quote. But I still get a
Syntax error. in query expression.... OR (((tblMu'.


Did you dim the variable as an integer or other appropriste number format?
--
Tim
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto
Nov 13 '05 #7

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

Similar topics

29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
3
2831
by: Bob | last post by:
I'm currently updating one of our web sites and have encountered a strange problem. The page that is giving me the problem is written in ASP and hits a SQL 2K DB. When I click submit I have 4 procs that need to be executed. I always get a sql server timeout error. I opened up Profiler and traced the events. The page hangs on the last proc called which is basically a select * where id = @id. If I run the same proc that hangs in query...
9
3122
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
10
3730
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty well and used a query plan that made sense. Now, I know what you're all thinking... stored procedures have to optimize for variable parameters, etc. Here's what I've tried to fix the issue: 1. Recompiled the stored procedure 2. Created a new,...
8
6451
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable .mdb. The query below query runs correctly and without error, but any attempt to save it causes Access to crash without a message, leaving the .ldb file. Opening the DB reveals it saved a blank "query1". I've upgraded to Jet SP 8, and I'm running...
6
29931
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access newsgroups, the access support centre, I can seem to find no similar situation. I am not using any references, or VBA at all in the first place. I am trying to set up a simple (or so I thought) query to work with the text of two tables. ...
7
2574
by: Dave Hopper | last post by:
Hi I posted a question recently regarding problems I am having getting a value from a list box to use in a query. I got a lot of help, for which I thank you and it's nearly working! But I need a little more help on one more point. This is what i've got. I have code that hunts for updated appointments in a public folder based on the order that happens to be open (code fires on an on open event) This works fine and updates my
2
3150
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong) ..Fields.Append .CreateField(strTmpIDFld, dbLong) ..Fields.Append .CreateField(strTmpDateFld, dbDate) ..Fields.Append .CreateField(strTmpDataFld, dbDouble)
8
6398
by: Ragbrai | last post by:
Howdy All, I have a query that is used for filtering results to be used in a combo box. The query needs to test fields from both a table and then unbound text boxes on the form that also contains the combo box. The fields I am working with are date fields that are formated as Short Date. I have written an IIF statement and placed it within the Criteria section of the Date field that I am trying to filter by. The current IIF statement works...
0
8262
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6122
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.