P
PhillWall
Hi - New to Access here. I am looking at taking load off a Main DB Server by
moving certain functionality into Access. To do this, the first step is I
need to do is load data - via ODBC into a table.
I have the connection working and I have a passthrough query that does
exactly what I want. However it requires a parameter and I cant see a way of
executing via VBA a parameterised querydef directly into a table (Into a
record set - sure and then I can write the code to take each of the fields
and insert them manually row by row into the table but there a huge number
of fields and Im not convinced there isnt a better way of coding this)
I also have ADO and DAO code to run the SQL and load the recordset.
but again that means all this extra code to load the fields from the record
set and insert into the local table.
I dont think I can use INSERT INTO because the main query is running on an
external mySQL database not in the JET engine locally.
I can use VBA to change the querydef SQL and then run it using
DoCmd.OpenQuery "passthrough1", acViewNormal, acReadOnly.
That will actually do exactly what I want but in my readings I gather
frequent changing of Query SQLs will "bloat" the database So that means
after running these (there will be a large number of these - loading several
hundred rows or so each query) I would have to run a compact of the database.
That doesnt seem elegant either.
I cant use the execute method of querydef - which would allow for parameters
- because its a select query - not an update query.
I dont think Import will work because I dont want the whole table (which is
gigabytes in size )
I suspect Im not thinking "Access" because surely something so easy to do
manually should be as easy to do in VBA
SO WHAT I WANT TO DO is run an ODBC query against an external database and
load the results direectly into a table. (IF there is data in the table I
want to wipe that data - I can run a truncate on it easily enough but I
mention that incase that can be incorporated)
Whats the most efficent way to code this (Im more concerned with keeping the
code short and readable as the client will try to maintain this and the guy
who will do that is not a programmer - so Im asking for shorter simpler code
rather than the fastest executing code... - this process currently takes a
staff member ALL day and the db server is overloaded anyway so a sec or so
wont make a difference)
Here is my VBA ADO code that loads the recordset (this is just a proof of
concept thing so its a way simpler query than the real one)
SQLcmd = "select * from agent a where agent_id between " & ParmBeginDate &
" and " & ParmEndDate & " ;"
'Declare and Instantiate Connection to the server
Dim svrcnn As ADODB.Connection
Set svrcnn = New ADODB.Connection
'Open the connection
svrcnn.Open "Driver={MySQL ODBC 5.1
Driver};SERVER=192.168.X.XXX;UID=xxxxx;PWD=xxxxxxx;DATABASE=xxxxxx;PORT=3306"
MsgBox "Open the Record Set"
Dim svrrst As ADODB.Recordset
Set svrrst = New ADODB.Recordset
svrrst.Open SQLcmd, svrcnn
MsgBox "Print the contents"
Debug.Print "Test Debug Print"
Debug.Print svrrst.GetString
MsgBox "Close and Destroy the record set"
svrrst.Close
Set svrrst = Nothing
'Close the connection
svrcnn.Close
but as I say I want to load direct into a table. doCmd the Querydef does it
but I cant see how to pass the paramters.
Any suggestions on the best access way to do this appreciated
Phill
moving certain functionality into Access. To do this, the first step is I
need to do is load data - via ODBC into a table.
I have the connection working and I have a passthrough query that does
exactly what I want. However it requires a parameter and I cant see a way of
executing via VBA a parameterised querydef directly into a table (Into a
record set - sure and then I can write the code to take each of the fields
and insert them manually row by row into the table but there a huge number
of fields and Im not convinced there isnt a better way of coding this)
I also have ADO and DAO code to run the SQL and load the recordset.
but again that means all this extra code to load the fields from the record
set and insert into the local table.
I dont think I can use INSERT INTO because the main query is running on an
external mySQL database not in the JET engine locally.
I can use VBA to change the querydef SQL and then run it using
DoCmd.OpenQuery "passthrough1", acViewNormal, acReadOnly.
That will actually do exactly what I want but in my readings I gather
frequent changing of Query SQLs will "bloat" the database So that means
after running these (there will be a large number of these - loading several
hundred rows or so each query) I would have to run a compact of the database.
That doesnt seem elegant either.
I cant use the execute method of querydef - which would allow for parameters
- because its a select query - not an update query.
I dont think Import will work because I dont want the whole table (which is
gigabytes in size )
I suspect Im not thinking "Access" because surely something so easy to do
manually should be as easy to do in VBA
SO WHAT I WANT TO DO is run an ODBC query against an external database and
load the results direectly into a table. (IF there is data in the table I
want to wipe that data - I can run a truncate on it easily enough but I
mention that incase that can be incorporated)
Whats the most efficent way to code this (Im more concerned with keeping the
code short and readable as the client will try to maintain this and the guy
who will do that is not a programmer - so Im asking for shorter simpler code
rather than the fastest executing code... - this process currently takes a
staff member ALL day and the db server is overloaded anyway so a sec or so
wont make a difference)
Here is my VBA ADO code that loads the recordset (this is just a proof of
concept thing so its a way simpler query than the real one)
SQLcmd = "select * from agent a where agent_id between " & ParmBeginDate &
" and " & ParmEndDate & " ;"
'Declare and Instantiate Connection to the server
Dim svrcnn As ADODB.Connection
Set svrcnn = New ADODB.Connection
'Open the connection
svrcnn.Open "Driver={MySQL ODBC 5.1
Driver};SERVER=192.168.X.XXX;UID=xxxxx;PWD=xxxxxxx;DATABASE=xxxxxx;PORT=3306"
MsgBox "Open the Record Set"
Dim svrrst As ADODB.Recordset
Set svrrst = New ADODB.Recordset
svrrst.Open SQLcmd, svrcnn
MsgBox "Print the contents"
Debug.Print "Test Debug Print"
Debug.Print svrrst.GetString
MsgBox "Close and Destroy the record set"
svrrst.Close
Set svrrst = Nothing
'Close the connection
svrcnn.Close
but as I say I want to load direct into a table. doCmd the Querydef does it
but I cant see how to pass the paramters.
Any suggestions on the best access way to do this appreciated
Phill