tags:

views:

148

answers:

4

I'm writing a vba code in Excel to access an Access database.

I'm using this provider

"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & DBFile & ";"

and here is my SQL string

SQlSrc = "SELECT YEAR FROM Data ORDER BY YEAR ASC"

SQlSrc = SQlSrc & ";SELECT STN FROM STN_List WHERE include = TRUE"

When I open each recordset individualy it works (just first line or second) but when I make as a single statement as above I get an error "Characters found after end of SQL statement"

Does anybody have idea if it's a problem with Access 2007?

+1  A: 

It looks like Access and/or the provider doesn't accept multiple SQL statements for opening a single recordset.

That said, I'm not sure if this is standard for all OLEDB providers or just the one you're using.

hythlodayr
Access has never supported more than one statment
Remou
+1  A: 

IMO MS Access does not support multiple SELECT statements in a single Command. You may have to split this into individual commands.

Raj More
A: 

That's unbelievable!.

So I have another question. How can I make a module that handles several sql statements without open and close the connection all the time?

My goal is to make a module that will work for any Access connection with any number of SQL statements.

Do you have any suggestion?

VBA_Code_Ex
What's unbelievable? That Jet/ACE doesn't accept multiple SQL statements? It's the way Jet/ACE has always been and your ignorance of that fact does not make it strange or unusual. In fact, if you took the time to understand Jet/ACE, you'd realize that it *can't* support multiple SQL statements because Jet/ACE is not a server database engine.
David-W-Fenton
@David W. Fenton: "it can't support multiple SQL statements because Jet/ACE is not a server database engine" -- please explain further, or provide a citation for this. Using ADODB Recordset's UpdateBatch works for the Access Database Engine (hint: it uses optimistic file locking). I fail to see why can't the engine support this functionality natively, so I suspect its just the usual story i.e. putting investment into SQL Server instead.
onedaywhen
+1  A: 

You can reuse the connection.

Dim cn As Object
Dim rs As Object

cn="Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & DBFile & ";"

SQlSrc = "SELECT YEAR FROM Data ORDER BY YEAR ASC"
rs.Open SQlSrc, cn

''Do stuff

SQlSrc = "SELECT STN FROM STN_List WHERE include = TRUE"
rs.Open SQlSrc, cn

cn.Execute "UPDATE Table SET Column=2"
Remou
Umm, not quite. The second invocation in your example is appending the second SQL query to the first one, bringing us back to where we started. It should just be a straight assignment, and lose the semicolon.
Dewayne Christensen
Remou