PC Review


Reply
Thread Tools Rate Thread

Make Table from external DB Connection

 
 
Dave
Guest
Posts: n/a
 
      27th Aug 2004
Hi,

I am trying to insert records into an access table
returned from an ADO connection to an external oracle
database. I am able to succeed this by inserting records
into the access db while reading the records from oracle.
This works fine, but I don't think it is the best way of
doing it. I also have to structure the tables in access
and change external queries accordingly. I ultimately want
to run a query on the fly that would create a table in
access with the results retured. Currently, I use the
below, any help would be appreciated.
********************************************************

Private Sub cmd_connect_Click()

'**********************************************************
*********************
'This Module will create 2 ADO Connections, one will
connect to the Oracle Instance,
'the other will reference a connection to the current
database.
'While oracle ado con loops through the records returned
from the query, the current
'ADO con will write the data to a table in the current
Access Database
'**********************************************************
*********************
Call clear_form

If Me.cmb_queries <> "" Then

'Declare Variables
Dim oracle_db As New ADODB.Connection
Dim oracle_rs As New ADODB.Recordset
Dim current_db As New ADODB.Connection
Dim current_rs As New ADODB.Recordset
Dim y As Integer, n As Integer, z As Integer 'Counter
Variables
Dim str_access_table As String
Dim strsql As String

Set current_db = CurrentProject.Connection

'Get Required Parameters for selected query
current_rs.Open "Select query_code, access_ref_table from
sql_scripts where query_name = '" & Me.cmb_queries & "'",
current_db
strsql = current_rs("query_code")
str_access_table = current_rs("access_ref_table")
current_rs.Close

DoCmd.SetWarnings False
Me.lbl1.ForeColor = 0
DoCmd.RepaintObject
DoCmd.RunSQL "Delete * from " & str_access_table & ";"
DoCmd.SetWarnings True
Me.opt1.Value = 1
DoCmd.RepaintObject

'Oracle Connection Parameters
Me.lbl2.ForeColor = 0
DoCmd.RepaintObject
oracle_db.Provider = "MSDAORA.1"
oracle_db.Open "oar_db", "userid", "psw"
Me.opt2.Value = 1
DoCmd.RepaintObject

Me.lbl3.ForeColor = 0
DoCmd.RepaintObject
oracle_rs.Open (strsql), oracle_db, adOpenStatic
Me.opt3.Value = 1
DoCmd.RepaintObject

'Current Access DB Connection Parameters
current_rs.Open (str_access_table), current_db,
adOpenDynamic, adLockOptimistic

'n = 0

oracle_rs.MoveFirst

Me.lbl4.ForeColor = 0
DoCmd.RepaintObject

Do Until oracle_rs.EOF
'y = n + 1

current_rs.AddNew

For Each x In oracle_rs.Fields
current_rs(x.Name) = x.Value
Next

'Me.txt_counter = y
'DoCmd.RepaintObject
'n = y

oracle_rs.MoveNext

Loop

current_rs.Update

Me.opt4.Value = 1
DoCmd.RepaintObject

'Close ado connections and kill references
Me.lbl5.ForeColor = 0
DoCmd.RepaintObject

oracle_rs.Close
oracle_db.Close
Set oracle_rs = Nothing
Set oracle_db = Nothing

current_rs.Close

'Display record count
strsql = "Select count(1) as record_count from " &
str_access_table & ";"
current_rs.Open (strsql), current_db

z = current_rs("record_count")
current_rs.Close

'Me.lbloutput.Visible = 1
'Me.lbl_destination = str_access_table
'Me.lbl_destination.Visible = 1
'DoCmd.RepaintObject

Set current_rs = Nothing
Set current_db = Nothing

Me.opt5.Value = 1
DoCmd.RepaintObject

MsgBox z & " records have been copied to table " &
str_access_table, vbInformation

cmb_queries = ""
Call clear_form

Else
MsgBox "Please Select a Query from the Pull Down List",
vbInformation
End If

End Sub
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QnJpYW4=?=
Guest
Posts: n/a
 
      17th Oct 2004
Not sure if I understand fully, but if the name of the external
connection/table does not change, can you simply run a make-table query
against that connection one time to get the temporary table formatted
correctly? I often do this, and then set up an Append query to populate the
table when needed, preceded and followed by a Delete query to flush the
contents before and after use, while leaving the table structure intact for
the next Append/Delete cycle. I'm usually using ODBC links, however, instead
of ADO, so I am not sure if my comments are germane.


"Dave" wrote:

> Hi,
>
> I am trying to insert records into an access table
> returned from an ADO connection to an external oracle
> database. I am able to succeed this by inserting records
> into the access db while reading the records from oracle.
> This works fine, but I don't think it is the best way of
> doing it. I also have to structure the tables in access
> and change external queries accordingly. I ultimately want
> to run a query on the fly that would create a table in
> access with the results retured. Currently, I use the
> below, any help would be appreciated.
> ********************************************************
>
> Private Sub cmd_connect_Click()
>
> '**********************************************************
> *********************
> 'This Module will create 2 ADO Connections, one will
> connect to the Oracle Instance,
> 'the other will reference a connection to the current
> database.
> 'While oracle ado con loops through the records returned
> from the query, the current
> 'ADO con will write the data to a table in the current
> Access Database
> '**********************************************************
> *********************
> Call clear_form
>
> If Me.cmb_queries <> "" Then
>
> 'Declare Variables
> Dim oracle_db As New ADODB.Connection
> Dim oracle_rs As New ADODB.Recordset
> Dim current_db As New ADODB.Connection
> Dim current_rs As New ADODB.Recordset
> Dim y As Integer, n As Integer, z As Integer 'Counter
> Variables
> Dim str_access_table As String
> Dim strsql As String
>
> Set current_db = CurrentProject.Connection
>
> 'Get Required Parameters for selected query
> current_rs.Open "Select query_code, access_ref_table from
> sql_scripts where query_name = '" & Me.cmb_queries & "'",
> current_db
> strsql = current_rs("query_code")
> str_access_table = current_rs("access_ref_table")
> current_rs.Close
>
> DoCmd.SetWarnings False
> Me.lbl1.ForeColor = 0
> DoCmd.RepaintObject
> DoCmd.RunSQL "Delete * from " & str_access_table & ";"
> DoCmd.SetWarnings True
> Me.opt1.Value = 1
> DoCmd.RepaintObject
>
> 'Oracle Connection Parameters
> Me.lbl2.ForeColor = 0
> DoCmd.RepaintObject
> oracle_db.Provider = "MSDAORA.1"
> oracle_db.Open "oar_db", "userid", "psw"
> Me.opt2.Value = 1
> DoCmd.RepaintObject
>
> Me.lbl3.ForeColor = 0
> DoCmd.RepaintObject
> oracle_rs.Open (strsql), oracle_db, adOpenStatic
> Me.opt3.Value = 1
> DoCmd.RepaintObject
>
> 'Current Access DB Connection Parameters
> current_rs.Open (str_access_table), current_db,
> adOpenDynamic, adLockOptimistic
>
> 'n = 0
>
> oracle_rs.MoveFirst
>
> Me.lbl4.ForeColor = 0
> DoCmd.RepaintObject
>
> Do Until oracle_rs.EOF
> 'y = n + 1
>
> current_rs.AddNew
>
> For Each x In oracle_rs.Fields
> current_rs(x.Name) = x.Value
> Next
>
> 'Me.txt_counter = y
> 'DoCmd.RepaintObject
> 'n = y
>
> oracle_rs.MoveNext
>
> Loop
>
> current_rs.Update
>
> Me.opt4.Value = 1
> DoCmd.RepaintObject
>
> 'Close ado connections and kill references
> Me.lbl5.ForeColor = 0
> DoCmd.RepaintObject
>
> oracle_rs.Close
> oracle_db.Close
> Set oracle_rs = Nothing
> Set oracle_db = Nothing
>
> current_rs.Close
>
> 'Display record count
> strsql = "Select count(1) as record_count from " &
> str_access_table & ";"
> current_rs.Open (strsql), current_db
>
> z = current_rs("record_count")
> current_rs.Close
>
> 'Me.lbloutput.Visible = 1
> 'Me.lbl_destination = str_access_table
> 'Me.lbl_destination.Visible = 1
> 'DoCmd.RepaintObject
>
> Set current_rs = Nothing
> Set current_db = Nothing
>
> Me.opt5.Value = 1
> DoCmd.RepaintObject
>
> MsgBox z & " records have been copied to table " &
> str_access_table, vbInformation
>
> cmb_queries = ""
> Call clear_form
>
> Else
> MsgBox "Please Select a Query from the Pull Down List",
> vbInformation
> End If
>
> End Sub
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how make table name in make-table query an input? Ian Elliott Microsoft Access Queries 3 27th Aug 2009 04:41 AM
Is it Possible to make a table from a query without a Make Table Q =?Utf-8?B?V2lsZGx5SGFycnk=?= Microsoft Access 9 6th Nov 2007 07:44 AM
Make Table Query-Running a make table query before you open a for =?Utf-8?B?dGtpcmNob2ZmMTU=?= Microsoft Access Macros 4 13th Apr 2007 11:50 PM
To have month and year as part of the table name in CREATE TABLE or MAKE TABLE Query ichew@scs.com.sg Microsoft Access Getting Started 1 18th Apr 2006 06:38 AM
Create Table Primary Key after Make Table Query And Update Table =?Utf-8?B?Uk5VU1pAT0tEUFM=?= Microsoft Access Queries 1 3rd May 2005 08:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.