473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export Access Data to Oracle

I scoured the groups for some hands on code to perform the menial task
of exporting table data from an Access 2000 database to Oracle
database (in this case, it was oracle 8i but i'm assuming this will
work for 9i and even 10g )

No one had what I needed, so I wrote it myself.
I Rule.

This code isn't going for efficiency, and isn't trying to be dynamic.
It doesn't create the table structure in Oracle, that's up to you. (I
had to do it myself by importing the table into a new SQL Server
database, then generating SQLServer script for the table structure,
and editing the script so it was Oracle compliant)
The structure is assumed to exist in Oracle already, with the table
names and field names IDENTICAL to Access.
Also, a note that varchar2 fields in oracle have a maximum of 4000
characters, so any TEXT files in Access that are longer than 4000
character will be truncated by my code.

I created a new form in Access, with 3 text boxes (server, user,
password) and a button (named cmdExport).
Place this code into the form's code, and the cmdExport_Click method
is there.
watch out for line-wrapping, this is a newsgroup post.

the only trick to this code is the tblNames() string array in
CopyAccessDataT oOracle() function. This array needs the names of your
tables, in the order they need to be truncated (they are then
populated in reverse of that order). Read the comments in the code,
and pay attention.

Email me if you have any questions or suggestions.

~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~
Option Compare Database

Private Sub cmdExport_Click ()
Dim user As String
Dim pass As String
Dim server As String
Dim strReturnedErro rMessage As String
Me.txtUserName. SetFocus
user = Me.txtUserName. Text
Me.txtUserPassw ord.SetFocus
pass = Me.txtUserPassw ord.Text
Me.txtServerNam e.SetFocus
server = Me.txtServerNam e.Text
Me.cmdExport.Se tFocus
If Not CopyAccessDataT oOracle(server, user, pass,
strReturnedErro rMessage) Then
MsgBox "Failed to copy data to Oracle." & vbCrLf &
strReturnedErro rMessage, vbCritical, "Error Exporting"
Else
Debug.Print "Exported all tables successfully"
End If
End Sub

'Entry point to copying
'connect to oracle server using user/pass
'delete all data in oracle tables listed in tblNames variable
'insert all data from access tables with same names into oracle tables
'note order in tables has to respect any constraints in oracle
Private Function CopyAccessDataT oOracle(ByVal sServer As String, ByVal
sUser As String, ByVal sPassWord As String, ByRef strErrorMessage As
String) As Boolean
On Error GoTo CopyAccessDataT oOracle_Error
Dim conn As ADODB.Connectio n
Dim cmd As ADODB.Command

Dim i As Integer
Dim strReturnedErro r As String

Dim tblNames(0 To 15) As String
'tables are in the drop order
'from 0 to 15, the data must be deleted
'then, data needs to be populated from 15 to 0
'** This order needs to respect all foreign key constraints **
tblNames(0) = "tbkwbridge "
tblNames(1) = "tblKeyword ID"
tblNames(2) = "tblSection 3"
tblNames(3) = "tblSection 4"
tblNames(4) = "tblSection 5"
tblNames(5) = "tblSection 7"
tblNames(6) = "tblSection 2"
tblNames(7) = "tblSection 8"
tblNames(8) = "tblSection 6"
tblNames(9) = "luFileForm at"
tblNames(10) = "tblSection 9"
tblNames(11) = "tblSection 10"
tblNames(12) = "tblSection 1"
tblNames(13) = "luRegionTy pe"
tblNames(14) = "luDataType "
tblNames(15) = "tblMetadataInf o"

Set conn = New ADODB.Connectio n
conn.Connection String = "Provider=MSDAO RA.1;Data Source=" &
sServer & ";User ID=" & sUser & "; Password=" & sPassWord & ";"
conn.Open

If conn.STATE <> 1 Then
strErrorMessage = "Failed to connect to database using
connection string: " & "Provider=MSDAO RA.1;Data Source=" & sServer &
";User ID=" & sUser & "; Password=" & sPassWord & ";"
CopyAccessDataT oOracle = False
Exit Function
End If

'step through the tables and delete all data in oracle
For i = 0 To 15
strReturnedErro r = ""
If Not DeleteDataFromO racleTable(tblN ames(i), conn,
strReturnedErro r) Then
MsgBox strReturnedErro r, vbCritical, "Error Deleting Data
In " & tblNames(i)
End If
Debug.Print "Deleted data in table " & tblNames(i)
Next i

'Not sure if this is needed
Set cmd = New ADODB.Command
cmd.ActiveConne ction = conn
cmd.CommandText = "commit"
cmd.Execute
DoEvents

'step through the tables and insert data from the access tables
into oracle
For i = 15 To 0 Step -1
strReturnedErro r = ""
Debug.Print "Exporting table " & tblNames(i) & " to Oracle."
If Not ExportDataToOra cleTable(tblNam es(i), conn,
strReturnedErro r) Then
MsgBox strReturnedErro r, vbCritical, "Error Copying Into "
& tblNames(i)
End If
Debug.Print "Exported."
Next i

'Not sure if this is needed
cmd.CommandText = "commit"
cmd.Execute
DoEvents

conn.Close

CopyAccessDataT oOracle_Exit:
CopyAccessDataT oOracle = True
strErrorMessage = ""
GoTo CopyAccessDataT oOracle_Cleanup

CopyAccessDataT oOracle_Cleanup :
If Not cmd Is Nothing Then Set cmd = Nothing
If Not conn Is Nothing Then Set conn = Nothing
Exit Function

CopyAccessDataT oOracle_Error:
strErrorMessage = Err.Description
CopyAccessDataT oOracle = False
Err.Clear
GoTo CopyAccessDataT oOracle_Cleanup
End Function
'Deletes all data in table strTableName
'in the database connected to by conn
Private Function DeleteDataFromO racleTable(ByVa l strTableName As
String, conn As ADODB.Connectio n, ByRef strErrorMessage ) As Boolean
On Error GoTo DeleteDataFromO racleTable_Erro r
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConne ction = conn
cmd.CommandText = "DELETE FROM " & UCase(strTableN ame)
cmd.Execute

DeleteDataFromO racleTable_Exit :
DeleteDataFromO racleTable = True
strErrorMessage = ""
GoTo DeleteDataFromO racleTable_Clea nup

DeleteDataFromO racleTable_Clea nup:
If Not cmd Is Nothing Then Set cmd = Nothing
Exit Function

DeleteDataFromO racleTable_Erro r:
DeleteDataFromO racleTable = False
strErrorMessage = "Error occured deleting data from table " &
strTableName & vbCrLf & Err.Description
Err.Clear
GoTo DeleteDataFromO racleTable_Clea nup
End Function
'Push the data in table strTableName into the same table (including
column names) on connection conn
Private Function ExportDataToOra cleTable(ByVal strTableName As String,
conn As ADODB.Connectio n, ByRef strErrorMessage ) As Boolean
On Error GoTo ExportDataToOra cleTable_Error

Dim cmd As ADODB.Command

Dim strTableInsert As String
Dim i As Integer

Set cmd = New ADODB.Command
cmd.ActiveConne ction = conn

Set tableRs = CurrentDb.OpenR ecordset("selec t * from " &
strTableName)

tableRs.MoveFir st
While Not tableRs.EOF
strTableInsert = "("
For i = 0 To tableRs.Fields. Count - 1

'Originally, i had a select case on the
tableRs.Fields( i).Type
'but Access returns strange types for some columns (i.e.
varchar for date fields)
'and this works.
If IsNull(tableRs. Fields(i).Value ) Then
strTableInsert = strTableInsert & "Null"
Else
If (IsNumeric(tabl eRs.Fields(i).V alue)) Then
strTableInsert = strTableInsert &
tableRs.Fields( i).Value
Else
If IsDate(tableRs. Fields(i).Value ) Then
'use CDate() to take care of situations
involoving "August 1998" which VB reports as
'a date, but Oracle can not to_date() that
format
strTableInsert = strTableInsert & "to_date('"
& CDate(tableRs.F ields(i).Value) & "', 'mm/dd/yyyy')"
Else
strTableInsert = strTableInsert & "'" &
MakeSafeVarchar (tableRs.Fields (i).Value) & "'"
End If
End If
End If

If i <> tableRs.Fields. Count - 1 Then
strTableInsert = strTableInsert & ", "
End If
Next i
DoEvents
strTableInsert =
GetInsertStatem entColumnListFr omTable(strTabl eName) & strTableInsert &
")"

'Print the final Insert Statement
'Debug.Print strTableInsert

cmd.CommandText = strTableInsert
cmd.Execute

tableRs.MoveNex t
Wend

ExportDataToOra cleTable_Exit:
ExportDataToOra cleTable = True
strErrorMessage = ""
GoTo ExportDataToOra cleTable_Cleanu p

ExportDataToOra cleTable_Cleanu p:
If Not cmd Is Nothing Then Set cmd = Nothing
If Not tableRs Is Nothing Then Set tableRs = Nothing
Exit Function

ExportDataToOra cleTable_Error:
ExportDataToOra cleTable = False
strErrorMessage = Err.Description
Err.Clear
GoTo ExportDataToOra cleTable_Cleanu p
End Function
'Makes the varchar safe to use in an Insert statement
'replaces single quotes ' with escape character ''
Private Function MakeSafeVarchar (ByVal strStringIn As String) As
String
Dim i As Integer
Dim swap(1, 2) As String
Dim strOut As String
strOut = strStringIn
swap(0, 0) = "'"
swap(0, 1) = "''"

For i = 0 To UBound(swap)
strOut = Replace(strOut, swap(i, 0), swap(i, 1))
Next i
'If, for some reason, the varchar goes beyond 4000 characters
(which is the maximum for Oracle)
'truncate.
'This isn't the best solution, but the Oracle database gets much
more complex if the CLOB
'data type is needed to replace for extra-long varchar fields
MakeSafeVarchar = Left(strOut, 4000)
End Function
'Constructs an insert statement for a table definintion
'this assumes that oracle has the exact same table and field names as
access
'note, double quotes " are used to wrap column names, because access
can have spaces
'and strange characters in the column names
Private Function GetInsertStatem entColumnListFr omTable(ByVal
strTableName As String) As String
Dim i As Integer
Dim strColList As String
strColList = "INSERT INTO " &
UCase(CurrentDb .TableDefs(strT ableName).Name) & " ("
For i = 0 To CurrentDb.Table Defs(strTableNa me).Fields.Coun t - 1
strColList = strColList & """" &
CurrentDb.Table Defs(strTableNa me).Fields(i).N ame & """"
If i <> CurrentDb.Table Defs(strTableNa me).Fields.Coun t - 1
Then
strColList = strColList & ", "
End If
Next i
strColList = strColList & ") Values "
GetInsertStatem entColumnListFr omTable = strColList
End Function
Nov 13 '05 #1
1 9196
An**********@gm ail.com (Andrew Arace) wrote in
news:26******** *************** ***@posting.goo gle.com:
I scoured the groups for some hands on code to perform the menial task
of exporting table data from an Access 2000 database to Oracle
database (in this case, it was oracle 8i but i'm assuming this will
work for 9i and even 10g )

No one had what I needed, so I wrote it myself.
I Rule.


What I did was use the Oracle data exchange tool to make my Oracle schema
from the Access database. Then, go back into Access, link the Oracle
tables, and write some code kind of like this:

public sub LoadDataToOracl e
dim db as dao.database
dim t as dao.tabledef
dim sql as string

set db = dao.currentdb

for each t in db.tabledefs
'notice how Oracle's tables are linked in via ODBC... <user>_tablenam e...
if t.connect = "" then 'it's a local table...
sql = "insert into <user>_" & _
t.name & " select * from " & t.name

DoCmd.runsql sql
end if
next t
end sub

At the time, the Oracle data pumper had problems loading all the data from
Access to Oracle.

This all works fine, assuming your Access table names aren't stupid (i.e.,
they have spaces in them and other non-SQLish characters in them.
Otherwise, you need to mangle the local tablenames in t.name to match the
Oracle table's equivalent.

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5128
by: Colin Graham | last post by:
I have a little problem i was wondering if anyone could help. I have created a table in Access and it has the following format. UniqueID FieldResult 100 1,3,5,7,23,7,8,9,12,4 103 12,6,,6,4,8,12,10,19 My problem is that i need to export the table into excel and then in excel the uniqueID should become a column and each value in the FieldResult section should become a column. e.g.
2
396
by: thomas goodwin | last post by:
I have an Access2002 table with one field, x, with the value 777. It is defined as data type=Numeric, field size=long integer. Using Access's TableExport/ODBC to Oracle9i(creating a new table), I get the Oracle message-1401 "Inserted value too large for column". Defining the Access table as Number with field size=Decimal solves the problem. I actually have hundreds of these Number/LongInteger variables in Access tables. How can I export...
0
1394
by: premmehrotra | last post by:
I am using Microsoft Access 2000 and Oracle 9.2.0.5 on Windows 2000. When I export a table from Access to Oracle using ODBC I get error: ORA 972 identifier too long I think the error is because one or more columns in Access table are longer than 30 characters. My question is how one can overcome this problem. I also see several column names which have /, ? or spaces in name
3
6961
by: Aliza Klein | last post by:
I have a client who wants to export his Access purchase order data (from the system I am designing) directly into Quickbooks. I know I can create a CSV or similar that Quickbooks can then import - but does anyone know if Quickbooks can be referenced directly (like an instance of Excel etc.) and "talked" to? TIA Aliza -- -----------------------------------
1
3454
by: shaguna.dhall | last post by:
I need to migrate Unicode data from MS Access to Oracle. Have tried the following: -Exported the Access tables to csv/txt, but these files were generated in ANSI encoding, and the Unicode data got converted to ASCII characters. -ODBC export directly to the Oracle tables doesn't work either. -However, when the table is exported to excel, Unicode encoding is retained.
5
5771
by: cdun2 | last post by:
Hello, I'm a non_VBA coder who has been asked to update the following Function: ************************** Function EMAILER_REV_BY_ACCTCODE_MACRO() On Error GoTo RA_EMAILER_Err DoCmd.SetWarnings False
4
3319
by: xwings | last post by:
I want to export my data to "Notepad" Or "Excel" Below is my Access Database Data; http://img340.imageshack.us/img340/3842/pictureju0.jpg How can i done that with VB6? or isit possible that i output my data to "EXCEL"
0
1267
by: beebelbrox | last post by:
Greetings all. Once more I must dip into the font of your collective wisdom and request help: I have been given the task of taking an exsisting Access Query and exporting it to excel. There are hitches. The query has two groups of records "A" and "B". The excel spread sheet needs to display Group "A" followed by the subtotals for select fields. Next a little white space then Group "B", again followed by sub totals. With Grand totals...
2
9344
by: Susan Littlefie | last post by:
I want to know an easy way to export Access data to Quickbooks.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.