473,398 Members | 2,368 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,398 software developers and data experts.

Make query works stand alone but not in SQL

49
Writing some VBA to call a make table query. It the make query is pulling data from a MS 2008 R2 SQL database then creating the data locally for processing. It works, but not when I call it out of the code below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CopyData_Click()
  2.  
  3. Dim dbo_Machine_a As QueryDef
  4. Dim dbo_Machine_Hello As QueryDef
  5. Dim dbo_Machine_World As QueryDef
  6.  
  7. Select Case MachineNameCombo
  8.  
  9.     Case "a"
  10.             DoCmd.SetWarnings False
  11.             DBEngine(0)(0).Execute "dbo_Machine_a"
  12.             DoCmd.SetWarnings True
  13.  
  14.  
  15.     Case "Hello"
  16.             DoCmd.SetWarnings False
  17.             DBEngine(0)(0).Execute "dbo_Machine_Hello"
  18.             DoCmd.SetWarnings True
  19.  
  20.     Case "World"
  21.  
  22.             DoCmd.SetWarnings False
  23.             DBEngine(0)(0).Execute "dbo_Machine_World"
  24.             DoCmd.SetWarnings True
  25.  
  26. End Select
  27.  
  28. End Sub
  29.  
I get the error: Run time error '3065' Can not execute a select query. From what I read on line about a 3065 I should be able to run a make query, just not a "select"
Dec 28 '11 #1

✓ answered by Stewart Ross

Please replace DoCmd.RunSQL with DoCmd.OpenQuery - the RunSQL method would require the SQL itself it turns out, not the name of the query.

OpenQuery will run action queries just as if you were running them from the normal query environment, with the exception that with SetWarnings off you will not have to confirm the replacement of the table.

Taking one of the Case statements as an example, you should end up with:

Expand|Select|Wrap|Line Numbers
  1. Case "World"
  2.    DoCmd.SetWarnings False
  3.    DoCmd.OpenQuery "dbo_Machine_World2"
  4.    DoCmd.SetWarnings True
  5.  
-Stewart

5 2599
Stewart Ross
2,545 Expert Mod 2GB
I see a number of problems with your approach. Firstly, the Execute method of the database object is intended to work with SQL strings (or the name of a query), not querydefs. Secondly, if you have included all of the code for your copydata_click sub, you have not set the locally-defined querydefs at all (that is, you have not associated the querydef variables with whatever actual queries you want to execute).

If you are using DAO querydefs you can use the querydef's Execute method to execute the query (after setting the querydef to associate it with the correct query, of course).

It appears to me to be much simpler if you delete your querydef local variables defined in lines 3 to 5 and simply provide the name for each query within the Execute statement in each case branch:

Expand|Select|Wrap|Line Numbers
  1. CurrentDB.Execute "Your Query Name Goes Here"
If you use Execute you can dispense with the setwarnings statements, which are not required in this case. However, in the case of a make-table query where a table of the same name already exists Execute will fail with a run-time error. This is a different behaviour than would occur where the Setwarnings statements are really needed, which is with DoCmd.RunSQL:

Expand|Select|Wrap|Line Numbers
  1. SetWarnings False
  2. DoCmd.RunSQL "Your Query Name Goes Here"
  3. SetWarnings True
Make-table queries run by DoCmd.RunSQL will overwrite any existing version of the table concerned. If warnings are set off (as above) this is done without the user being asked to confirm the replacement.

-Stewart
Dec 28 '11 #2
AndyB2
49
Did not work, Here is the code after editing:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub CopyData_Click()
  3.  
  4. Select Case MachineNameCombo
  5.  
  6.     Case "a"
  7.             DoCmd.SetWarnings False
  8.             DoCmd.RunSQL "dbo_Machine_a2"
  9.             DoCmd.SetWarnings True
  10.  
  11.     Case "Hello"
  12.             DoCmd.SetWarnings False
  13.             DoCmd.RunSQL ("dbo_Machine_Hello2")
  14.             DoCmd.SetWarnings True
  15.  
  16.     Case "World"
  17.             DoCmd.SetWarnings False
  18.             DoCmd.RunSQL "dbo_Machine_World2"
  19.             DoCmd.SetWarnings True
  20.  
  21. End Select
  22.  
  23. End Sub
  24.  
I'm getting a run time error 3129 now "Invalid SQL Statement; expected 'DELETE', 'INSERT', ect...

I added the DoCmd. for the setwarning as the code was erroring out without it. I also changed the name of the make query by adding the "2" at the end. The name of the dbo file was the same as the make query name.
Dec 31 '11 #3
Stewart Ross
2,545 Expert Mod 2GB
Please post the SQL for at least one of the queries (dbo_Machine_a2 etc). DoCmd.RunSQL, like the Execute method, is expecting to run an action query (one which contains statements beginning INSERT, DELETE etc) - and what you have posted suggests that a SELECT statement is being supplied instead.

Sorry I missed out the DoCmd off the Setwarnings statements last time.

-Stewart
Dec 31 '11 #4
AndyB2
49
I'm using MS Access as the front end. There is no "code" what I'm tring to execute is a make table query created with MS Access standard query options.

Went into Access and viewed in SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT dbo_Machine_Hello.Simulation_Examples_Functions_Ramp1_VALUE, dbo_Machine_Hello.Machine, dbo_Machine_Hello.Timestamp1, dbo_Machine_Hello.Quality, dbo_Machine_Hello.[Employee#], dbo_Machine_Hello.Production 
  2. INTO MachineData
  3. FROM dbo_Machine_Hello;
Dec 31 '11 #5
Stewart Ross
2,545 Expert Mod 2GB
Please replace DoCmd.RunSQL with DoCmd.OpenQuery - the RunSQL method would require the SQL itself it turns out, not the name of the query.

OpenQuery will run action queries just as if you were running them from the normal query environment, with the exception that with SetWarnings off you will not have to confirm the replacement of the table.

Taking one of the Case statements as an example, you should end up with:

Expand|Select|Wrap|Line Numbers
  1. Case "World"
  2.    DoCmd.SetWarnings False
  3.    DoCmd.OpenQuery "dbo_Machine_World2"
  4.    DoCmd.SetWarnings True
  5.  
-Stewart
Dec 31 '11 #6

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

Similar topics

121
by: David Pendrey | last post by:
I was wondering if it is at all posible to write a stand alone .EXE program in Visual Studio .NET. Hopefully in VB.NET but if not another language would be ok. Thanks for the assistance
3
by: Todd D. Levy | last post by:
What do I need to get (from Microsoft I assume) in order to distribute stand alone Access applications to people who do not (and will not) have Access installed on their systems? I have heard...
6
by: Ryan Bounds | last post by:
Hi All We have upgraded from Access2000 to Access2003 with all service packs. The problem that we have is: When a user try's to filter a client the database crashes and close's Access2003,...
4
by: Jim Witt | last post by:
Is it possible to compile an assembly to be a stand-alone app independant of installing the .NET framework, or would that make an enormous .exe size? Thanks!
7
by: Ulrich Wisser | last post by:
Hi, I would like to stop the postmaster every night and run vacuum pg_dump reindex in the stand alone backend.
2
by: jim-on-linux | last post by:
py help, The file below will run as a stand alone file. It works fine as it is. But, when I call it from another module it locks my computer, The off switch is the only salvation. This...
2
by: karthi84 | last post by:
hi, i have created a web project in visual studio 2003 in a networked system and it worked fine. when i tried to access that project through a browser from another system in the same...
7
by: driplet | last post by:
I am using MS VC++ 2005 Express Edition to make some windows application programs. I found that the exe files I built on one computer cannot be run on the other. Could any body please tell me how to...
1
by: =?Utf-8?B?RGViSA==?= | last post by:
Hello, I am not connecting to an office network anymore so I changed the "setting" to take off the network and use the computer as a stand alone. After rebooting, I cannot log on AT ALL!! "System...
1
by: josh001 | last post by:
Hey, I have a program that needs wxpython, shelle and a dat file... But I have no idea how to make it into a stand alone program. I want to be able to put it on a disc and give it to my friends...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.