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,362
Office Version
365
Platform
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. :)
 

Forum statistics

Threads
1,081,834
Messages
5,361,594
Members
400,639
Latest member
fleyd

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top