Obvious SQL VBA question.

Mars

New Member
Joined
Jul 19, 2005
Messages
19
I new to working with SQL in Access macros, and have a problem with the following code.
Sub SQLtest()

Dim strSQL As String

strSQL = "SELECT AUTO_PREMTREND.[Fiscal_Year],AUTO_PREMTREN[CLEP],AUTO_PREMTREND.[Earned_Exp] " & _
"FROM AUTO_PREMTREND " & _
"WHERE AUTO_PREMTREND.[State_Cd]='AZ' " & _
"AND AUTO_PREMTREND.[Major_Peril_Cd]='BI'" & _
"ORDER BY AUTO_PREMTREND.[STATE_Cd];"


DoCmd.RunSQL strSQL
End Sub

When i run this, it returns an error msg that says "A RunSQL action requires an argument consisting of an SQL statement"

This is the associated query that runs fine.
SELECT AUTO_PREMTREND.Fiscal_Year, AUTO_PREMTREND.CLEP, AUTO_PREMTREND.Earned_Exp
FROM AUTO_PREMTREND
WHERE (((AUTO_PREMTREND.State_Cd)="AZ") AND ((AUTO_PREMTREND.Major_Peril_Cd)="bi"));

Is there an obvious mistake I'm making?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

pritcham

Board Regular
Joined
Oct 26, 2005
Messages
128
Hi

I think (but you may want to confirm) that the runSQL command is used for action queries (i.e. updates/inserts/deletes) and doesn't deal with selects so that may be your problem.

Hope that helps
Martin
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Mars

When creating SQL queries in VBA you need to be extra careful with spaces.

I think you should have a space after 'BI' here.

Code:
"AND AUTO_PREMTREND.[Major_Peril_Cd]='BI' " & _

I don't know if that will help, might just be a typo or something to do with how you copied the code into the message.

By the way what do you actually want to do, I think RunSQL is normally used with action queries, which I think Mark is pointing toward.

From help.
MS ACCESS VBA Help said:
The RunSQL method carries out the RunSQL action in Visual Basic in action queries. For more information on how the action and its arguments work, see the action topic.
 

Portia

New Member
Joined
Nov 21, 2005
Messages
10
Hi, Mars.

Trying changing this line: 'DoCmd.RunSQL strSQL'

To this: 'rst = CurrentDB.OpenRecordset strSQL'

You'll need a recordset object to store the results though if you're planning on doing something with the data (hence the 'rst').

Hopefully, that should help. :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,545
Messages
5,838,005
Members
430,525
Latest member
WHall

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top