View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike Knight Mike Knight is offline
external usenet poster
 
Posts: 5
Default Excel VBA Query Using Access Database Hangs on 2nd Execute

(Jamie Collins) wrote in message . com...
(Mike Knight) wrote ...

I have a MS Access 2003 Database named "AS400 Fields.mdb". This
database contains links to tables on an AS400.

In MS Excel 2003, I have VBA code that creates and executes queries
using the Access database, and returns the results to an Excel sheet.
There are many of these Excel query files and they all work at the
same time on different users' machines.

The first time the query is executed, results are returned to Excel in
usually less than 10 seconds. However, if the query is executed a
second time (without closing the Excel workbook), the code hangs at
the line surrounded by "=",s in the code below (this was tested with
message boxes). If the Excel file containing the VBA code is closed
and re-opened the code executes normally.

Private Sub Execute_Query()
Dim X As Integer
Dim DB1 As ADODB.Connection
Dim RS1 As ADODB.Recordset
Const ConnectionStr As String =
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= Myserver Location Goes here\AS400 Fields.mdb"
Set DB1 = New ADODB.Connection
DB1.Open ConnectionStr

Set RS1 = New ADODB.Recordset
'=============================
RS1.Open QryStr, DB1
'=============================
'Copy the data
ActiveCell.Offset(1, 0).Select
ActiveCell.CopyFromRecordset RS1
RS1.Close
DB1.Close
Set RS1 = Nothing
Set DB1 = Nothing
End Sub


I can't think of a specific problem. I think you must isolate the
problem a bit more. What is QryStr e.g. a SELECT query, a stored
procedure? Have you tried doing something else with the Connection
object e.g. use its Execute method to UPDATE or INSERT to a test
table, rather than return a row set? Have you tried running a
different query/stored proc to return a different result set? Have you
tried keeping the connection open for the second call? (this may not
be an option in production if you have many workbooks connecting to
the .mdb)

Jamie.

--


Jamie

Qrystr is a string variable containing my select statement.

I just tried the Execute method:
(Set RS1 = DB1.Execute(QryStr, , adCmdText))

Different queries using different AS400 Files (linked tables in Access
DB) have the same problems.

I will experiment with leaving the connection open.

Thanks for your reply.

Mike