473,799 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Code error in VBA

40 New Member
Hi,
I'm trying to display my query results onto an Access subform. I have written a very simple SQL statement to preform a query depending on a user's choice of a drop down box in an attempt to display the resultset on the subform.

Example:
If the user selects 10 from the combobox, then it does a query saying "SELECT * from tblD10;"

No matter what the SQL statement is, (i've tried simple ones such as this and more complex queries that I've written before and are known-working) I get the VBA error:
'Run-time error 2342
A RunSQL action requires an argument consisting of an SQL statement.'

I tried using both annotations of the 'all' object as you can see in the code I have: "SELECT *" and "SELECT tblD10.*"

Below is the short snippet of code that gives the error in VB. I'm not sure if having the 'DoCmd.RunSQL' in an 'if statment' matters or not... I wouldn't imagine so.

I'm using Office 2003 w/ SP2 on XP machine...

Any insight much appreciated.
Thanks in advance.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSearchDept_AfterUpdate()
  2. Dim rs As Recordset
  3. Dim db As Database
  4. Dim frm2 As SubForm
  5. Set frm2 = Forms!frmSearch!subSearch
  6. Dim strSQL10 As String
  7. Dim strSQL11 As String
  8. Set db = CurrentDb
  9.  
  10.  
  11. strSQL10 = "SELECT * FROM tblD10;"
  12. strSQL11 = "SELECT tblD11.* FROM tblD11;"
  13.  
  14.  
  15.  
  16. If cboSearchDept = 10 Then
  17. DoCmd.RunSQL strSQL10
  18. End If
  19.  
  20. If cboSearchDept = 11 Then
  21. DoCmd.RunSQL strSQL11
  22. End If
  23.  
  24. Set rs = frm2
  25. Forms!frmSearch!subSearch.Requery
  26.  
  27. End Sub
  28.  
Aug 9 '07 #1
8 3191
MikeTheBike
639 Recognized Expert Contributor
Hi,
I'm trying to display my query results onto an Access subform. I have written a very simple SQL statement to preform a query depending on a user's choice of a drop down box in an attempt to display the resultset on the subform.

Example:
If the user selects 10 from the combobox, then it does a query saying "SELECT * from tblD10;"

No matter what the SQL statement is, (i've tried simple ones such as this and more complex queries that I've written before and are known-working) I get the VBA error:
'Run-time error 2342
A RunSQL action requires an argument consisting of an SQL statement.'

I tried using both annotations of the 'all' object as you can see in the code I have: "SELECT *" and "SELECT tblD10.*"

Below is the short snippet of code that gives the error in VB. I'm not sure if having the 'DoCmd.RunSQL' in an 'if statment' matters or not... I wouldn't imagine so.

I'm using Office 2003 w/ SP2 on XP machine...

Any insight much appreciated.
Thanks in advance.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSearchDept_AfterUpdate()
  2. Dim rs As Recordset
  3. Dim db As Database
  4. Dim frm2 As SubForm
  5. Set frm2 = Forms!frmSearch!subSearch
  6. Dim strSQL10 As String
  7. Dim strSQL11 As String
  8. Set db = CurrentDb
  9.  
  10.  
  11. strSQL10 = "SELECT * FROM tblD10;"
  12. strSQL11 = "SELECT tblD11.* FROM tblD11;"
  13.  
  14.  
  15.  
  16. If cboSearchDept = 10 Then
  17. DoCmd.RunSQL strSQL10
  18. End If
  19.  
  20. If cboSearchDept = 11 Then
  21. DoCmd.RunSQL strSQL11
  22. End If
  23.  
  24. Set rs = frm2
  25. Forms!frmSearch!subSearch.Requery
  26.  
  27. End Sub
  28.  
Hi

I believe DoCmd.RunSQL requires, as the error states, an ACTION query (that does not return records). You are supplying a select query ??

To display records in a form you need to set the form RecordSource = "SQL Statement" (SELECT) or create a recordset and assign the fields to the form controls in code. Is that what you want?

MTB
Aug 9 '07 #2
wizardRahl
40 New Member
Hi
I believe DoCmd.RunSQL requires, as the error states, an ACTION query (that does not return records). You are supplying a select query ??

To display records in a form you need to set the form RecordSource = "SQL Statement" (SELECT) or create a recordset and assign the fields to the form controls in code. Is that what you want?
MTB
Are you implying that a SELECT statement isn't
Yes, I would like to display the results of a query on a subform. The problem with putting it as the record source is that I have to define a query. The query will change depending on what the user selects in a combobox on the parent form. Acutally, the query will stay the same, just changing the FROM table.

Example:
User selects 11 from combobox (cboSearchDept)
I would like to display results from
SELECT * FROM tblD11;

User selects 12 from combobox (cboSearchDept)
I would like to display results from
SELECT * FROM tblD12;

and so on and so forth...

I can create a fixed query (ex. qryDept10) and it will display the results that would be expected. I just need to figure out how to get the SELECT statement to work when I choose from cboSearchDept.



I have thought about just writing one long SQL statement and saving it as a query, but I don't think I can write IF statements in SQL view when designing a query in Access. It forces you to begin with a SQL keyword (SELECT, INSERT etc)

Thanks for prompt reply!
Aug 9 '07 #3
wizardRahl
40 New Member
That first sentence is supposed to say:
"Are you implying that the SELECT statement isn't an ACTION query?"

I would have to agree with you. However, the error says that the 'DoCmd.RunSQL' action requires an argument consisting of a SQL statement, not an action query. This tells me that the statement that I have written isn't recognized by VBA as a valid SQL statement.

Any other insight appreciated.
Sorry for the double post.
Aug 10 '07 #4
MikeTheBike
639 Recognized Expert Contributor
That first sentence is supposed to say:
"Are you implying that the SELECT statement isn't an ACTION query?"

I would have to agree with you. However, the error says that the 'DoCmd.RunSQL' action requires an argument consisting of a SQL statement, not an action query. This tells me that the statement that I have written isn't recognized by VBA as a valid SQL statement.

Any other insight appreciated.
Sorry for the double post.
Hi

Well, we agree that SELECT isn't an action query, however RunSQL does require a valid action query SQL, as the eror message suggest.

Perhaps the solution to your problem would be something like this code in the After Update event of the combo box
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSearchDept_AfterUpdate()
  2.     Me.RecordSource = "SELECT * FROM " & cboSearchDept
  3.     Me.Requery
  4. End Sub
The bound column of the cboSearchDept combo would list all the table names to select from.

This also assumes that all the fields are the same in all the tables !

???

MTB
Aug 13 '07 #5
wizardRahl
40 New Member
Hi

Well, we agree that SELECT isn't an action query, however RunSQL does require a valid action query SQL, as the eror message suggest.

Perhaps the solution to your problem would be something like this code in the After Update event of the combo box
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSearchDept_AfterUpdate()
  2.     Me.RecordSource = "SELECT * FROM " & cboSearchDept
  3.     Me.Requery
  4. End Sub
The bound column of the cboSearchDept combo would list all the table names to select from.

This also assumes that all the fields are the same in all the tables !

???

MTB

Thanks again for the reply!

It didn't like the RecordSource statement that you provided. It or any variation of it produced the same error: "Syntax error in FROM clause".

I may have found a different way. I'll just write a query for each SELECT statement and tell it:

If 'cboSearchDept' = 10 Then
DoCmd.OpenQuery qryD10
End If

All I need now is to figure out how to get the results to display on the subform.
The subform is named 'subSearch'. When I update the combo, I see a quick message on status bar (bottom left) saying "Calculating... " , so it seems that it is actually running the query. Just gotta get it to display to resultset from the query.

I've taken the liberty of uploading the the db file. If you think you'd like to look at it, please feel free to do so. There is no sensitive data, only names and the structure of the db.

http://5.download-3.files-upload.com...3/13-30/TM.mdb

Any further insight much appreciated.
Aug 13 '07 #6
MikeTheBike
639 Recognized Expert Contributor
Thanks again for the reply!

It didn't like the RecordSource statement that you provided. It or any variation of it produced the same error: "Syntax error in FROM clause".

I may have found a different way. I'll just write a query for each SELECT statement and tell it:

If 'cboSearchDept' = 10 Then
DoCmd.OpenQuery qryD10
End If

All I need now is to figure out how to get the results to display on the subform.
The subform is named 'subSearch'. When I update the combo, I see a quick message on status bar (bottom left) saying "Calculating... " , so it seems that it is actually running the query. Just gotta get it to display to resultset from the query.

I've taken the liberty of uploading the the db file. If you think you'd like to look at it, please feel free to do so. There is no sensitive data, only names and the structure of the db.

http://5.download-3.files-upload.com...3/13-30/TM.mdb

Any further insight much appreciated.
Hi

If you want to have the new table data to show in the sub form then you will have to change the RecoedSource on the sub form and requery it.

As I mentioned before, if you have the names on the table (or query if it is a sub set or multiple table query) in the combo box (as a Value List!) in the bound column, then you can use this

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSearchDept_AfterUpdate()
  2.     Me.SubFormControlName.Form.RecordSource = "SELECT * FROM " & cboSearchDept
  3.     Me.SubFormControlName.Requery
  4. End Sub
where SubFormControlN ame is the name of the control containing the subform.

As mention the tables need the same fields.


MTB
Aug 14 '07 #7
wizardRahl
40 New Member
Access will not allow me to put table names in the bound column. It does not allow text to be entered. I can select the ValueList to be the RowSource of all the table names, so that the combo is the selection that I want to be there. I'm thinking this is what you meant.

Also, after I do the said steps above, I get the error:
"Method or data member not found." and it has cboSearchDept selected.

I've triplechecked the spelling in both places (code and control properties) and both are correct. I'm not sure why VB thinks that the object doesn't exist.

Here's the updated file:
http://8.download-1.files-upload.com...5/13-20/TM.mdb

It's the little things like this that are frustrating. I know that it's a little change that will make the app work like it should.

Any further insight appreciated.
Thanks in advance.
Aug 15 '07 #8
MikeTheBike
639 Recognized Expert Contributor
Access will not allow me to put table names in the bound column. It does not allow text to be entered. I can select the ValueList to be the RowSource of all the table names, so that the combo is the selection that I want to be there. I'm thinking this is what you meant.

Also, after I do the said steps above, I get the error:
"Method or data member not found." and it has cboSearchDept selected.

I've triplechecked the spelling in both places (code and control properties) and both are correct. I'm not sure why VB thinks that the object doesn't exist.

Here's the updated file:
http://8.download-1.files-upload.com...5/13-20/TM.mdb

It's the little things like this that are frustrating. I know that it's a little change that will make the app work like it should.

Any further insight appreciated.
Thanks in advance.
Hi

In addition to my previous code, try this (suitable Modified to you table/query names) in the main form load event

Expand|Select|Wrap|Line Numbers
  1. With  cboSearchDept 
  2.     .RowSourceType = "Value List"
  3.     .BoundColumn = 1
  4.     .ColumnCount = 2
  5.     .ColumnWidths = "0cm;2.54cm"
  6.     .RowSource = "qryD10;Query D10 Data;qryD11;Query D11 Data;qryD12;Query D12 Data"
  7. End With
and see if that works, assuming qryD10 etc. are stored queries, althought table names will also work.

I'm sorry but IT policy does not permit me to down load anything, so cannot look at file.

MTB
Aug 16 '07 #9

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

Similar topics

13
4487
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
17
18224
by: wana | last post by:
I was reading through original source code of ping for some insight and was confused by unusual code. Entire listing available at: http://www.ping127001.com/pingpage/ping.html #include #include #include #include #include #include #include #include #include #include #include #include
18
4611
by: __frank__ | last post by:
The following code use a macro and a label. I would to change it and use instead a more readable function and avoid the label. The macro DAQmxFailed checks for the return code of the various functions: DAQmxCreateTask() DAQmxStartTask() DAQmxReadAnalogScalarF64()
4
11364
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error LNK2020: unresolved token (0A000007) memset LINK : error LNK2020: unresolved token (0A000008) free LINK : error LNK2020: unresolved token (0A00000A) atexit LINK : error LNK2020: unresolved token (0A000028) wcscpy LINK : error LNK2020: unresolved...
2
5533
by: mike_li | last post by:
On Window 2000 Professional Server DB2 UDB Level: DB2 code release "SQL07029" with level identifie "030A0105" and informational tokens "DB2 v7.1.0.98", "n040510" and "WR21337". In the db2diag.log, ---------------------------------------------------- 2005-12-20-10.05.43.278000 Instance:MC Node:000
10
2215
by: dbuchanan | last post by:
Hello, >From time to time my vb2005 form disappears and is replaced by the following errors. Rebuilding the application never helps. However the errors never affects the operation of my application that I notice, but it is very anoying. To get rid of the errors I must close the form, close the application and then reopen it.
9
1700
by: Jens Jensen | last post by:
Hello all, I need some design advice for my web service. I' have written a web service that exposes a function that takes some parameters and return an xml.
4
48781
by: Pool | last post by:
I tried to connect DB2 (Sitting in Unix server at my client location) using Db2 connect V8. I am getting the following error message. I tried all the possible options BUt the error is same.. See each type belpw Standard connection failed.
0
1630
by: walve_wei | last post by:
<1>use the D3D control panel, enable the debug DLL and maximum validation,for D3D control panel ,you need to install the directx sdk. <2>Start up the debug monitor (<MSVC install directory>\Common7\Tools \Bin\winnt\Dbmon.Exe). <3>If you use the tooltip in native code,then run the program,show the tooltip,then exit the program, the Dbmon.exe will report serious direct3d9 memleak in xp and vista os.but If I use the wpf tooltip in c#,it is...
1
4631
by: sanctus | last post by:
I have a Matlab code which works fine in windows. Now I installed Xubuntu (because this way I have admin rights(=root) which I don't have on windows) and want to use the same code. To be able to use the C-code you have to compile it from Matlab comand line with the comand mex myfile.c where before with mex -setup you define which compiler to use, the only choice I had was gcc (version 4.something). Now if I compile I get the following...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9067
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.