Microsoft
Software
Hardware
Network
Question : Writing results of a VBA driven Pass-Trhough Query to an Access table
I need to store a recordset (size unknown), that is returned by a pass-trhrough query (sql string built at runtime), into a MS Access table. The query is sent into Oracle database. I will appreciate if the code for the whole cycle - from sending a query to Oracle database to storing the results into a table - is presented here.
FYI: I could only accomplish this task for the first record returned by a PT query with the rest not present in the recordset and the recordset itself being weird in the sense that stepping from the first record to the second retrieves an "ODBC call failed" error instead of saying there are no more records. RecordCount method also states that there is only one record retrieved. Running the same query in Oracle via SQL Navigator retrieves multiple records.
Answer : Writing results of a VBA driven Pass-Trhough Query to an Access table
try this code and change names accordingly. naturally if not done already, you will first need to set up the relevant ODBC connection and have your tnsnames.ora file configured correctly.
Function fnQuerySQLServer() As Boolean
'inside a code module, check tools->References and ensure that
'microsoft dao 3.6 Object library is checked
'if not you have to register that dll library
'the file is usually found under C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll
'-------------------------
----------
----------
----------
----------
----------
----------
----------
--
'backup your access database before running this function!!!!
'replace tablenames, query names, sql strings accordingly
'-------------------------
----------
----------
----------
----------
----------
----------
----------
--
Dim d As DAO.Database, qODBC As DAO.QueryDef
Dim sql As String, sqODBC As String, sConnectDAO As String
Dim sWhere As String
Set d = CurrentDb
DoCmd.SetWarnings False
DoCmd.Hourglass True
'whatever query you want to name it as
sqODBC = "qODBC_mySQLServer"
'warning: below removes whatever temp query and table you want to store your results
On Error Resume Next
DoCmd.DeleteObject acQuery, sqODBC
DoCmd.DeleteObject acTable, "myTempAccessTableThatStor
esResultSe
tFromSQLSe
rver"
On Error GoTo errH
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
'defining the pass thru query
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
'if you need to get your criteria from a form control etc it would be something like
sWhere = " WHERE CustID = " & Forms![frm_cust]![Cust_ID]
sql = "select * from whateverSQLServerView " & sWhere
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
'if it is a stored proc, an eg of a stored proc with two parameters , 1st numeric, 2nd a string parameter would be:
'sql = "exec whateverStoredProc 100, 'whateverStringParameter'"
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
Set qODBC = d.CreateQueryDef(sqODBC, sql)
'note the APP argument may not be required, play around with the connect string to see what works
sConnectDAO = "ODBC;DSN=whateverDSN;UID=
whateverUs
erID;APP=M
icrosoft Office 2003;DATABASE=whateverdata
base"
qODBC.Connect = sConnectDAO
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
'if your stored proc does not return records (for eg if it inserts/updates a table etc then below should be set to false
qODBC.ReturnsRecords = True
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
qODBC.Close
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
'now define another sql statement to execute your pass thru query
'-------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
sql = "select * into myTempAccessTableThatStore
sResultSet
FromSQLSer
ver from " & sqODBC
d.Execute sql
comExit:
Set d = Nothing
Set qODBC = Nothing
DoCmd.SetWarnings True
DoCmd.Hourglass False
Exit Function
errH:
Beep
MsgBox Err & " " & Err.Description
Resume comExit
End Function
Random Solutions
SQL 6.5: Kill xp_cmdshell process awaiting user input
ActiveX Error 336
Remove Spaces in String Variable
tunnelling with VNC over SSH...
Symantec Antivirus on ES 2003
Windows Server 2003 Setup Restart Loop
CGI Mailer script
Forwarding Multiple Domains, How to benefit SEO?
IIS Running, but Web Svcs wont start
Port Forwarding