473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

SQL in VBA for Access

31
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.
Jun 29 '07 #1
14 4445
LacrosseB0ss
113 100+
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
Jun 29 '07 #2
JKing
1,206 Expert 1GB
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.  
Jun 29 '07 #3
Flo100
31
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
Jun 29 '07 #4
Flo100
31
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
Jun 29 '07 #5
JKing
1,206 Expert 1GB
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.
Jun 29 '07 #6
Flo100
31
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?
Jul 2 '07 #7
Flo100
31
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
Jul 2 '07 #8
JKing
1,206 Expert 1GB
Did you replace qryMyQuery with the name of your query?
Jul 3 '07 #9
NeoPa
32,556 Expert Mod 16PB
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.
Jul 3 '07 #10
Flo100
31
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?
Jul 3 '07 #11
NeoPa
32,556 Expert Mod 16PB
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.
Jul 3 '07 #12
NeoPa
32,556 Expert Mod 16PB
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)
Jul 3 '07 #13
Flo100
31
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.
Jul 12 '07 #14
NeoPa
32,556 Expert Mod 16PB
Good for you. Pleased you got it sorted :)
Jul 12 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

63
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy...
13
by: bill | last post by:
I am trying to convince a client that dotNet is preferable to an Access project (ADP/ADE). This client currently has a large, pure Access MDB solution with 30+ users, which needs to be upgraded....
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
13
by: Simon Bailey | last post by:
I am a newcomer to databases and am not sure which DBMS to use. I have a very simplified knowledge of databases overall. I would very much appreciate a (simplifed) message explaining the advantages...
0
by: Frederick Noronha \(FN\) | last post by:
---------- Forwarded message ---------- Solutions to Everyday User Interface and Programming Problems O'Reilly Releases "Access Cookbook, Second Edition" Sebastopol, CA--Neither reference book...
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
64
by: John | last post by:
Hi What future does access have after the release of vs 2005/sql 2005? MS doesn't seem to have done anything major with access lately and presumably hoping that everyone migrates to vs/sql. ...
1
by: com | last post by:
Extreme Web Reports 2005 - Soft30.com The wizard scans the specified MS Access database and records information such as report names, parameters and subqueries. ......
17
by: Mell via AccessMonster.com | last post by:
Is there a way to find out where an application was created from? i.e. - work or home i.e. - if application sits on a (work) server/network, the IT people know the application is sitting...
37
by: jasmith | last post by:
How will Access fair in a year? Two years? .... The new version of Access seems to service non programmers as a wizard interface to quickly create databases via a fancy wizard. Furthermore, why...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.