Solved

SQL won't work

Posted on 2000-04-17
17
135 Views
Last Modified: 2010-05-18
I have a data control on a form which is used to link to an Access Db.

In the formload event of the form I have the following code:

Set dbs = OpenDatabase("c:\my documents\employees.mdb")

In the general declarations section:

Option Explicit
Dim dbs As Database
Dim rst As Recordset

And in a command button:

Private Sub Command1_Click()




Set rst = dbs.OpenRecordset("SELECT tbldept.DeptNumber, tbldept.DeptName, " _
& "tblemployees.EmpNumber, tblemployees.EmpName, tblemployees.EmpTitle, " _
& "FROM tbldept INNER JOIN tblemployees ON " _
& "tbldept.DeptNumber = tblemployees.EmpDeptNumber, " _
& "WHERE DeptNumber ='" & Text1.Text & "' ;")

I cannot get the SQL coded properly.  The error is 3141.

What is wrong with the SQL?

TIA,

jjjjjjj
0
Comment
Question by:jjjjjjj
  • 4
  • 3
  • 3
  • +5
17 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2724069
try putting parenthesis around the FROM clause:

FROM (tbldept INNER JOIN ... ON ....)
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 25 total points
ID: 2724073
also take out the comma before the WHERE
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2724077
sorry, before the FROM as well
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2724188
Definitively, the problem is caused by commas!
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2724255
Why is there a ; at the end of things?


M
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2724287
to tell that the statement is over
0
 

Expert Comment

by:gafftech
ID: 2724288
I would create the Query in Access and click on view\sql copy that and paste it in your code.

Also I would:
dim sSql as string

sSql = "Select * from Table1"
sSql = Sql & "Where Table1.ID = 1 "

Debug.Print Sql 'If you copy this 'string into the sql window in access 'and it runs you are good to go.

'Set rst = dbs.OpenRecordset sSqL

Check your Syntax before you even try and open the rs.

0
 
LVL 12

Expert Comment

by:mark2150
ID: 2724328
The statement is over when the string ends. Unless this is embedded in a stream of statements you don't need the ";" at the end. This may be causing the SQL engine to think that more SQL is forthcomming when it's not.

M
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Sage020999
ID: 2724657
To make it a little easier, create your SQL in Access design mode, view it in SQL mode and paste it in your code.
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2724740
All,

I have done the Access look and paste but it has not helped.  I believe that there is a syntax error in the code that I have written, but I cannot find it.

I will take the suggestions that AzraSound has made and see what happens.

Thanks for the great dialog from all!!

jjjjjjj
0
 

Expert Comment

by:Billy06
ID: 2724859
yeah, I agree with Mark.
You won't need ';'.
NO Commas before FROM and before WHERE.

last statement should be:

"where DeptNumber = '" & Text1.Text& "'"

ALSO, are you sure it's _

to continue?
Is it    " & _
 "where


Good Luck
Bill
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2724860
Why isn't working?

Is your query is this one:

Set rst = dbs.OpenRecordset("SELECT tbldept.DeptNumber, tbldept.DeptName, " _
& "tblemployees.EmpNumber, tblemployees.EmpName, tblemployees.EmpTitle " _
& "FROM tbldept INNER JOIN tblemployees ON " _
& "tbldept.DeptNumber = tblemployees.EmpDeptNumber " _
& "WHERE DeptNumber ='" & replace(Text1.Text,"'","''") & "'")
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2724870
Yes, it is, but in VB All of the data after the replace is not needed.  I have used the code that I posted in the past but never with the join spec.  I believe that this is where the problem lies, but can't figure it out.

jjjjjjj
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2724895
Is DeptNum numeric?

If so your query should be:
Set rst = dbs.OpenRecordset("SELECT tbldept.DeptNumber, tbldept.DeptName, tblemployees.EmpNumber, tblemployees.EmpName, tblemployees.EmpTitle FROM tbldept INNER JOIN tblemployees ON tbldept.DeptNumber = tblemployees.EmpDeptNumber WHERE DeptNumber =" & Text1.Text)

While your query won't work, put it all on a single line so it could be easier to seek for mistake!
0
 

Expert Comment

by:c4mar
ID: 2725434
go database manager, put all of your SQL statment and test it over there. You should get the result immedailly, and other thing is try not to break the SQL statement into different line. Cuase you may make some mistake when u try to break it into different line. Other method that u can try is go to access and create a queue for your SQL statement, then compare the SQL code inside the access and your old SQL statement. Ususally u are able to get some idea from the SQl code.
0
 

Expert Comment

by:gafftech
ID: 2726353


Using Access you will need ; at the end of your select statment.  Access SQl Also needs:

String = "text1"
AutoNumber = Text1
0
 
LVL 1

Author Comment

by:jjjjjjj
ID: 2727943
Thanks, AzraSound.  The comma's were the culprit.  The parentheses can either stay or remain, either way the SQL now works fine.

jjjjjjj
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now