Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Pass-Through with a Temp Table 2

Status
Not open for further replies.

cghoga

Programmer
Jun 26, 2003
614
US
Hello,

I have a SQL procedure that creates a temp table initially and then runs several updates to columns within the table.

At the end I query the table for the result set.

I have used a pass through before but never quite like this.

Here is where I am at this point...

I ran the same SQL script in the Access pass-through that I can successfully run in SQL and it did not error out, but my insert statement (VBA) did not insert any records. There was not any data in the table in Access.

How do I get Access to recognize the temp table so that I can insert the values into a table within Access to be viewable in a form or report?

Any and all help is greatly appreciated.

Thanks!
 
Thanks pwise, please see below...

Dim myDb As DAO.Database
Dim myQd As DAO.QueryDef
Dim connectstr(1) As String
Dim sObjStr As String

DoCmd.SetWarnings False

DoCmd.DeleteObject acQuery, "BaseQuery"

DoCmd.RunSQL "delete from TestTable"

sObjStr = "TestQuery"

'define connections to ODBC tables
connectstr(1) = "ODBC;DSN=Corpdb05;UID=hoganc;Trusted_Connection=Yes"

'Setup the base script properties
Set myDb = CurrentDb
Set myQd = myDb.CreateQueryDef("BaseQuery")
myQd.ODBCTimeout = 1000
myQd.Connect = connectstr(1)
myQd.ReturnsRecords = True

myQd.SQL = myDb.QueryDefs(sObjStr).SQL

myQd.Connect = connectstr(1)
DoCmd.RunSQL "INSERT INTO TestTable ( employer, company, account ) SELECT employer, company, account FROM BaseQuery"

myQd.Close

DoCmd.SetWarnings True
 
And here is an example of the SQL in the pass through...

Select employer, company, account
into #Test
from Table1

Select employer, company, account
from #Test

This is very basic I know. This is just the first step. Subsequent steps will require updates to the temp table. I wanted to build the infrastructure upfront and get it going before I tackled the whole process.

Thanks again for your help....
 
when you run this qurey


Code:
Select employer, company, account
into #Test
from Table1

Select employer, company, account 
from #Test

it returns records ??


I tried it and i got a message box

[qoute]

pass through query with returns records set to true did not return any records did not return any records

[/quote ]
 
It does not return records.

If I comment out the '--into #Test' and the last row and just do a simple select statement it populates the recordset and my table.

It's like it's not recognizing the final select from the temp table.
 
A temp table only stays in scope on the same connection. It looks to me like a 2nd connection is being started to retrieve from the temp table. Stay with the 1st connection.
 
Are you saying this cannot be done or is there a way to stay with the 1st connection?

I tried removing the second instance of 'myQd.Connect = connectstr(1)' and it did not help.

Thanks for the suggestions.
 
I have made use of temp tables through an ADO connection and I think you can through DAO, but I don't know if it can be done using Macros like DoCmd.RunSQL. Somebody that uses DAO a lot could show you how to do it avoiding Macros. I could give you an example using ADO but that introduces additional learning.
 
try just this
Code:
dim mydb as database
set mydb=currentdb
currentdb.execute "INSERT INTO TestTable ( employer, company, account )Select employer, company, account from Table1  IN '' [ODBC;DSN=Corpdb05;UID=hoganc;Trusted_Connection=Yes];"
 
I understand. I am willing to learn if you coud get me started down the path.

Thanks for your help.
 
Here is an example of using ADO to create a connection, create a #temp table, add a record to the table, and then read the record.


Function TestTempTable()
'-- Reference Library
'-- Microsoft ActiveX data objects 2.6 library needed for ADO
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset, connString As String
connString = "provider=SQLOLEDB.1;" & _
"User ID=sa;Initial Catalog=Northwind;" & _
"Data Source=shark;" & _
"Persist Security Info=False"
cn.ConnectionString = connString
cn.Open connString

'-- create the temporary table
Dim mykey As Integer, sql1 As String, textData As String
textData = "Some description data"
mykey = 10
sql1 = "insert into #myTempTable values(" & mykey & ",'" & textData & "')"
cn.Execute "create table #myTempTable (akey int, muchWords ntext) "
Debug.Print sql1
cn.Execute sql1
rs.Open "Select * from #myTempTable", cn, adOpenStatic, adLockOptimistic
Debug.Print "first field = "; rs(0); " second field = "; rs(1)

''cn.Execute "drop table #myTemptable"
cn.Close
Set cn = Nothing

End Function

 
Brilliant! Thanks for your time and help.

cghoga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top