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?
 

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
75,275
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. :)
 

Forum statistics

Threads
1,077,864
Messages
5,336,846
Members
399,105
Latest member
idleminutes

Some videos you may like

This Week's Hot Topics

Top