SQL statement doesn't work in VBA.

D

Dan

When I run the following, it gave me " A RunSQL action requires an argument
consisting of an SQL statement". Can anyone help?

Dim SQL as String
SQL = "SELECT [Contact List].[RFQ Vendor], [Contact List].[Email Sent],
[Contact List].Email, [Contact List].SupplierCCEmail, RFQ_Master.* " & _
"FROM RFQ_Master INNER JOIN [Contact List] ON RFQ_Master.[RFQ Vendor] =
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL SQL
 
C

Chris O'C via AccessMonster.com

Runsql requires an action query or a data definition query, like "update
tablename", "insert into", "create table" etc. You're using a select query,
which only gathers records in tables for display. It doesn't do anything to
those records or the tables.

Select queries can be used with querydefs in vba code.

Chris
Microsoft MVP

When I run the following, it gave me " A RunSQL action requires an argument
consisting of an SQL statement". Can anyone help?

Dim SQL as String
SQL = "SELECT [Contact List].[RFQ Vendor], [Contact List].[Email Sent],
[Contact List].Email, [Contact List].SupplierCCEmail, RFQ_Master.* " & _
"FROM RFQ_Master INNER JOIN [Contact List] ON RFQ_Master.[RFQ Vendor] =
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL SQL
 
P

Paolo

Hi Dan,
I'm pretty sure SQL is an access reserved word. Change the name of your
variable. e.g.

Dim str_SQL as String
str_SQL = "SELECT [Contact List].[RFQ Vendor], [Contact List].[Email Sent],
[Contact List].Email, [Contact List].SupplierCCEmail, RFQ_Master.* " & _
"FROM RFQ_Master INNER JOIN [Contact List] ON RFQ_Master.[RFQ Vendor] =
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL (str_SQL)

HTH Paolo
 
C

Chris O'C via AccessMonster.com

And don't use sql as a variable. It's a reserved word.

Chris
Microsoft MVP

Runsql requires an action query or a data definition query, like "update
tablename", "insert into", "create table" etc. You're using a select query,
which only gathers records in tables for display. It doesn't do anything to
those records or the tables.

Select queries can be used with querydefs in vba code.

Chris
Microsoft MVP
When I run the following, it gave me " A RunSQL action requires an argument
consisting of an SQL statement". Can anyone help?
[quoted text clipped - 5 lines]
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL SQL
 
P

Paolo

I didn't notice that the statement was just a select query. As Chris said
runsql requires an action query or a data definition query

Paolo said:
Hi Dan,
I'm pretty sure SQL is an access reserved word. Change the name of your
variable. e.g.

Dim str_SQL as String
str_SQL = "SELECT [Contact List].[RFQ Vendor], [Contact List].[Email Sent],
[Contact List].Email, [Contact List].SupplierCCEmail, RFQ_Master.* " & _
"FROM RFQ_Master INNER JOIN [Contact List] ON RFQ_Master.[RFQ Vendor] =
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL (str_SQL)

HTH Paolo

Dan said:
When I run the following, it gave me " A RunSQL action requires an argument
consisting of an SQL statement". Can anyone help?

Dim SQL as String
SQL = "SELECT [Contact List].[RFQ Vendor], [Contact List].[Email Sent],
[Contact List].Email, [Contact List].SupplierCCEmail, RFQ_Master.* " & _
"FROM RFQ_Master INNER JOIN [Contact List] ON RFQ_Master.[RFQ Vendor] =
[Contact List].[RFQ Vendor];"
DoCmd.RunSQL SQL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SQL -- VBA 3
Using Date fields in SQL 2
SQL - VBA once again 10
SQL Query in VBA 9
VBA query trouble 4
VBA -- SQL 9
VSTO C# add in Outlook.Appointment error 0
Stop Query 4

Top