Connecting Tech Pros Worldwide Forums | Help | Site Map

SQL in VBA for Access

Newbie
 
Join Date: Jun 2007
Posts: 31
#1: Jun 29 '07
Hi All,

I have a simple query that I want to execute in VBA in Access. My form has a button and the following code exists in the click event of the button:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim strSQL As String
  3. Set db = CurrentDb
  4.  
  5. strSQL = "SELECT * FROM Project info Tbl;"
  6. db.Execute strSQL
  7. DoCmd.Close acForm, Me.Name
  8. Set db = Nothing

But it gives me a Run Time Error 3131: Syntax error in FROM Clause.

Can somebody help me on this. Very urgent. Many thanks.

LacrosseB0ss's Avatar
Member
 
Join Date: Oct 2006
Location: Brampton, ON, Canada
Posts: 116
#2: Jun 29 '07

re: SQL in VBA for Access


Your table name can't have spaces. SQL looks at the statement this way:

FROM - get ready for a table
Project - ok, this is the table I want
**standby for additional functions or where clause**

Info - look for the info function. WTF!?!?!? I can't find it. Throw an error.

My advice would be to change the table name to Project_Info_Table or something.

Hope this helps
- LB
JKing's Avatar
Moderator
 
Join Date: Jun 2007
Location: Niagara Falls, Ontario
Posts: 557
#3: Jun 29 '07

re: SQL in VBA for Access


Hi, another alternative to changing your table name is to enclose the table name in square brackets. Square brackets are used in sql to handle spaces in field and table names.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM [Project info Tbl];"
  2.  
Newbie
 
Join Date: Jun 2007
Posts: 31
#4: Jun 29 '07

re: SQL in VBA for Access


I changed the name of the table and used it in the following 2 ways:

First:

strSQL = "SELECT * FROM Project_info_Tbl;"
db.Execute strSQL

Error: Cannot execute SELECT Query

Second:

strSQL = "SELECT * FROM Project_info_Tbl;"
DoCmd.RunSQL strSQL
DoCmd.Close acForm, Me.Name
Set db = Nothing

Error: A RunSQL action requires an argument consisting of an SQL statement
Newbie
 
Join Date: Jun 2007
Posts: 31
#5: Jun 29 '07

re: SQL in VBA for Access


Quote:

Originally Posted by JKing

Hi, another alternative to changing your table name is to enclose the table name in square brackets. Square brackets are used in sql to handle spaces in field and table names.

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM [Project info Tbl];"
  2.  


I tried with the square brackets:

strSQL = "SELECT * FROM [Project info Tbl];"
DoCmd.RunSQL strSQL
DoCmd.Close acForm, Me.Name
Set db = Nothing

But, it gives me the following error....

Error: A RunSQL action requires an argument consisting of an SQL statement
JKing's Avatar
Moderator
 
Join Date: Jun 2007
Location: Niagara Falls, Ontario
Posts: 557
#6: Jun 29 '07

re: SQL in VBA for Access


RunSQL and Execute I believe can only be used with action queries.
There are two things you can do to acheive your results

1) Create and save a query that uses your SQL
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery("qryMyQuery")
  2. DoCmd.Close acForm, Me.Name
  3.  
2) Create and save a query that you can modify on the fly
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim strSQL As String
  4.  
  5. Set db = CurrentDb
  6. Set qdf = db.QueryDefs("qryMyQuery")
  7.  
  8. strSQL = "SELECT * FROM [Project Name tbl];"
  9. qdf.SQL = strSQL
  10.  
  11. DoCmd.OpenQuery("qryMyQuery")
  12. DoCmd.Close acForm, Me.Name
  13.  
  14. Set db = nothing
  15. Set qdf = nothing
  16.  
Let me know how you make out with this.
Newbie
 
Join Date: Jun 2007
Posts: 31
#7: Jul 2 '07

re: SQL in VBA for Access


Quote:

Originally Posted by JKing

RunSQL and Execute I believe can only be used with action queries.
There are two things you can do to acheive your results

1) Create and save a query that uses your SQL

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery("qryMyQuery")
  2. DoCmd.Close acForm, Me.Name
  3.  
2) Create and save a query that you can modify on the fly
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim strSQL As String
  4.  
  5. Set db = CurrentDb
  6. Set qdf = db.QueryDefs("qryMyQuery")
  7.  
  8. strSQL = "SELECT * FROM [Project Name tbl];"
  9. qdf.SQL = strSQL
  10.  
  11. DoCmd.OpenQuery("qryMyQuery")
  12. DoCmd.Close acForm, Me.Name
  13.  
  14. Set db = nothing
  15. Set qdf = nothing
  16.  
Let me know how you make out with this.


I tried it too. But it is giving me the following error:

"Run time error 3265 item not found in this collection".

I am using Access 2002 and the databse is Access 200 file format.
Does it have to do anything with the version of Access?
Newbie
 
Join Date: Jun 2007
Posts: 31
#8: Jul 2 '07

re: SQL in VBA for Access


Quote:

Originally Posted by Flo100

I tried it too. But it is giving me the following error:

"Run time error 3265 item not found in this collection".

I am using Access 2002 and the databse is Access 200 file format.
Does it have to do anything with the version of Access?


Correction: the database is in Access 2000 file format
JKing's Avatar
Moderator
 
Join Date: Jun 2007
Location: Niagara Falls, Ontario
Posts: 557
#9: Jul 3 '07

re: SQL in VBA for Access


Did you replace qryMyQuery with the name of your query?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#10: Jul 3 '07

re: SQL in VBA for Access


There are a lot of right answers in here (I loved LB's explanation - sweet), but I'm afraid the question is unclear. Bugs were found (quite validly) but trying to understand what you're trying to achieve is not so easy.
Your SQL (after fixes) is a SELECT query, which means it will provide information.
Where do you need this information?
In your code or on the screen for the operator?
This makes a world of difference to the question.
Newbie
 
Join Date: Jun 2007
Posts: 31
#11: Jul 3 '07

re: SQL in VBA for Access


Quote:

Originally Posted by NeoPa

There are a lot of right answers in here (I loved LB's explanation - sweet), but I'm afraid the question is unclear. Bugs were found (quite validly) but trying to understand what you're trying to achieve is not so easy.
Your SQL (after fixes) is a SELECT query, which means it will provide information.
Where do you need this information?
In your code or on the screen for the operator?
This makes a world of difference to the question.

I need the answer(result of my query) on the screen. My task is to write an event for a command button which creates a query and opens the result for the user in datasheet view.

I hope this is clear. Just one more question please. What are the diference between Access 200, 2002 and 2003?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#12: Jul 3 '07

re: SQL in VBA for Access


Quote:

Originally Posted by Flo100

I need the answer(result of my query) on the screen. My task is to write an event for a command button which creates a query and opens the result for the user in datasheet view.

In that case JKing is on the right lines.
Follow those instructions and let us know how you get on.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#13: Jul 3 '07

re: SQL in VBA for Access


Quote:

Originally Posted by Flo100

I hope this is clear. Just one more question please. What are the differences between Access 2000, 2002 and 2003?

Just a small question to throw in then (LOL)
Check out this link (Converting Access versions)
Newbie
 
Join Date: Jun 2007
Posts: 31
#14: Jul 12 '07

re: SQL in VBA for Access


Quote:

Originally Posted by NeoPa

Just a small question to throw in then (LOL)
Check out this link (Converting Access versions)


I got my task done by saving the query and opening it using

Expand|Select|Wrap|Line Numbers
  1. CurrentDb.QueryDefs("qryAllIndex").SQL = strSQL
  2. DoCmd.OpenQuery "qryAllIndex"
where "QueryAllIndex" is name of a query and strSQL is the string that contains the query.

Thank you everybody..for your help.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#15: Jul 12 '07

re: SQL in VBA for Access


Good for you. Pleased you got it sorted :)
Reply