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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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
 
Upvote 0
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.
 
Upvote 0
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. :)
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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
Back
Top