Wrox Programmer Forums
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2007, 03:19 PM
Registered User
 
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default @#$%& SQL syntax

Newbie to VB with ACCESS. I am trying to build a SQL statement using 2 controls from a form. Keep getting a syntax error. I need another set of eyes to help find the problem. Below is my statement so far:
     strSql = "SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = " & Val([Forms]![frmStartHere]![cmbBldgNo])) & _
                ") And ((EQUIP.EQUIP_NO) = '" & [Forms]![frmStartHere]![cmbEquipNo] & "')" & _
                " ORDER BY EQUIP.BLDG_NO"

equip.bldg_no is a number field. equip.equip_no is a text field. Both controls are combo fields on the form. This statement is being supplied to open a recordset (ADO).

Thanks for your time.

 
Old March 6th, 2007, 03:26 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

...whats the error you are getting?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 6th, 2007, 03:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there...

really don't know what language are you programming, but the ) in the VAL function since to be useless (1 open, 2 close?). Apart from that the sql looks ok to me... the syntax error is in the line of code or in the sql???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
 
Old March 6th, 2007, 03:32 PM
Registered User
 
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Error was just the "Compile Error, Syntax error"
I then changed my statement to the following:
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = Val([Forms]![frmStartHere]![cmbBldgNo])" & _
                " And EQUIP.EQUIP_NO) = [Forms]![frmStartHere]![cmbEquipNo]" & _
                " ORDER BY EQUIP.BLDG_NO"

Now I am getting: No value given for one or more parameters
 
Old March 6th, 2007, 03:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

i don't know why you change your code, but the last one is definitive wrong...

use this:
Code:
strSql = "SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = " & Val([Forms]![frmStartHere]![cmbBldgNo]) & _
                ") And ((EQUIP.EQUIP_NO) = '" & [Forms]![frmStartHere]![cmbEquipNo] & "')" & _
                " ORDER BY EQUIP.BLDG_NO"
HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
 
Old March 6th, 2007, 03:46 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In the above example and in your original post, you have Syntax errors in your where clause.

Origianl Post:
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = " & Val([Forms]![frmStartHere]![cmbBldgNo])) & _
                ") And ((EQUIP.EQUIP_NO) = '" & [Forms]![frmStartHere]![cmbEquipNo] & "')" & _
                " ORDER BY EQUIP.BLDG_NO"

Should be: (removed ) that proceeded the And keword line 3
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = " & Val([Forms]![frmStartHere]![cmbBldgNo])) & _
                " And ((EQUIP.EQUIP_NO) = '" & [Forms]![frmStartHere]![cmbEquipNo] & "')" & _
                " ORDER BY EQUIP.BLDG_NO"



And in your second post change:
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = Val([Forms]![frmStartHere]![cmbBldgNo])" & _
                " And EQUIP.EQUIP_NO) = [Forms]![frmStartHere]![cmbEquipNo]" & _
                " ORDER BY EQUIP.BLDG_NO"

to this:
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES" & _
                " FROM EQUIP WHERE ((EQUIP.BLDG_NO) = Val([Forms]![frmStartHere]![cmbBldgNo]))" & _
                " And EQUIP.EQUIP_NO = [Forms]![frmStartHere]![cmbEquipNo]" & _
                " ORDER BY EQUIP.BLDG_NO"

Add a ) after cmbBldgNo and remove the ) that follows EQUIP.EQUIP_NO

Gonazalo, it looks like she is working with VBA.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 6th, 2007, 03:48 PM
Registered User
 
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I turned on debug to step thru the procedure. When the recordset.open strSql executed, forced the error: "no values given for one or more parameters"

Error displayed with and without the val function. I will dink around some more.

Thanks for the tips.
 
Old March 6th, 2007, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

that error meant that one of the field is not typed correctly...

verify every field name... to this quickly..:

turn on debuging..
step into the line that build the sql
get what's inside the sql variable
try that query in access and it will tell you wich field is wrong.

good luck...


HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
 
Old March 6th, 2007, 04:00 PM
Registered User
 
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using VBA within some Access forms. Using procedures defined in form events to find records and display a form. Most data requires 2 column to be used in a query. You would think I could have started with a simpler project.
 
Old March 6th, 2007, 04:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Pam,

One of the things that you have done (which all beginners do) is to fail to keep clear the distinction between literal text—a string—and items that represent values.

This is especially tricky when you are creating strings which themselves distinguish between literal and representative.

First let me take your code, and reformat it for reading ease:
Code:
"SELECT EQUIP.BLDG_NO, EQUIP.EQUIP_NO, EQUIP.EQCODE, EQUIP.LOCAT, EQUIP.SERVES " & _
"FROM   EQUIP " & _
"WHERE  ((EQUIP.BLDG_NO) = Val([Forms]![frmStartHere]![cmbBldgNo])" & _
" AND   EQUIP.EQUIP_NO) = [Forms]![frmStartHere]![cmbEquipNo] " & _
"ORDER BY EQUIP.BLDG_NO"
Specifying the table and the field (TBL.FLD) is only necessary when you have more than 1 table in the FROM clause. So this can be simplified
Code:
"SELECT BLDG_NO, EQUIP_NO, EQCODE, LOCAT, SERVES " & _
"FROM   EQUIP " & _
"WHERE  ((BLDG_NO) = Val([Forms]![frmStartHere]![cmbBldgNo])" & _
" AND   EQUIP_NO) = [Forms]![frmStartHere]![cmbEquipNo] " & _
"ORDER BY BLDG_NO"
Next, Access is absolutely insane about adding parentheses in SQL, many of which are unnecessary. The folowing red parens do nothing for the SQL, and make it harder to read
Code:
WHERE  ((BLDG_NO) =
Taking those out, in the folowing
Code:
"WHERE  (BLDG_NO = Val([Forms]![frmStartHere]![cmbBldgNo])"
the SQL parser tries to find a record where BLDG is compared to an impossible value. See, the value on the form needs to be added to this SQL string you are building. To do that, you need to break out of being literal, and get VBA to provide the value found on the form (add the red parts):
Code:
"WHERE  (BLDG_NO = " & Val([Forms]![frmStartHere]![cmbBldgNo]) & " . . . "
If [Forms]![frmStartHere]![cmbBldgNo] has " 002 " as a value, the resultant string will be
Code:
"WHERE  (BLDG_NO = 2 . . . "
Gotta go. Hopefully this will get you on your way. I'll check back later.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Crystal Report & VB6 & SQL Server wlho VB How-To 2 June 5th, 2013 01:58 AM
Need Help with SQL syntax DeannaF829 VB Databases Basics 2 April 30th, 2007 12:12 PM
SQL-syntax grstad SQL Language 9 January 30th, 2007 02:40 PM
sql & join tables & find a field in multiple table trangd Beginning PHP 2 January 29th, 2004 07:18 PM
SQL Syntax jeffg22 SQL Language 6 July 28th, 2003 06:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.