Source code dibawah ini merupkan code untuk meng-
konverikan suatu database msAccess ke Oracle.
1. Buatlah 1 Project(name=Convert.vbp)
2. Tambahkan 1 Form(name=FrmAccessToOracle)
3. Tambahkan 1 SSTab(Tabs=3,TabsPerRow=3)
a. Tab=0,caption=1.Asal Database
Tambahkan Label1(caption=Nama MsAccse-
ss Database),TextBox(name=TxMdbName),
CommandButton(name=CmdBrowse,Caption=
Browse),CheckBox(name=CheckPassword,cap-
tion=Password),TextBox(name=TxPassword,
PasswordChar=*),Label(name=LabelOpenMsA-
ccess,Capt=open MsAccess database. . . .)
CommandButton(name=CmdCancel1,Capt=Batal)
CommandButton(name=CmdNext1,Capt=Beriku-
tnya >),Commondialog.
b. Tab=1,capt=2.Target Database
Tambahkan Label(Capt=Database),TextBox(
name=TxDatabaseOracle),label(Capt=User
Name),TextBox(name=TxUserOracle),Label
(Capt=Password),TextBox(name=TxPasswordOr-
acle),CommandButton(name=CmdOpenConnection,
Open connection)Label(name=LabelOpenOracle,
Capt=Open connection Oracle Database.....)
CommandButton(name=CmdCancel2,Capt=Batal)
CommandButton(name=CmdBack2,capt=nya)CommandButton(name=CmdNext2,capt=Beri-
kutnya>)
c. Tab=2,capt=3.Pilih Tabel
Label(capt=Daftar Tabel)ListBox(name=ListA-
vailableTable)CommandButton(name=CmdAdd,Ca-
pt=Tambahkan>)CommandButton(name=CmdAddAll,
Capt=Tambahkan semua>>)CommandButton(name=
CmdRemove,capt=< Buang)commandButton(name=
CmdRemoveAll,capt=<< Buang Semua)Label(Capt=
Table Yang Dipilih)ListBox(name=ListSele-
ctedTable)Label(name=LabelProsesTbl,capt=
LabelProses Tabel)CommandButton(name=CmdC-
ancel3,capt=Batal)CommandButton(name=CmdP-
rev3,capt=< Kembali)CommandButton(name=Cm-
dConvert,capt=Convert)
Copy coding dibawah ini dan paste di Editor Form:
Option Explicit
Dim CnAccess, CnOracle As ADODB.Connection
Dim cat As ADOX.Catalog
Private Sub CheckPassword_Click()
If CheckPassword.Value = 0 Then
TxPassword.Enabled = False
Else
TxPassword.Enabled = True
End If
End Sub
Private Sub CmdAdd_Click()
Dim i As Byte
i = 0
Do
If ListAvailableTable.Selected(i) Then
ListSelectedTable.AddItem ListAvailableTable.List(i)
ListAvailableTable.RemoveItem (i)
End If
i = i + 1
Loop While i <= ListAvailableTable.ListCount - 1
End Sub
Private Sub CmdAddAll_Click()
Dim i As Byte
i = 0
Do
ListSelectedTable.AddItem ListAvailableTable.List(i)
i = i + 1
Loop While i <= ListAvailableTable.ListCount - 1
ListAvailableTable.Clear
End Sub
Private Sub CmdBack2_Click()
SSTab1.Tab = 0
End Sub
Private Sub CmdBrowse_Click()
CommonDialog1.CancelError = True
CommonDialog1.Filter = "*.mdb"
CommonDialog1.FileName = "*.mdb"
On Error GoTo Batal
CommonDialog1.ShowOpen
TxMdbName.Text = CommonDialog1.FileName
Batal:
End Sub
Private Sub CmdCancel1_Click()
Unload Me
End Sub
Private Sub CmdCancel2_Click()
Unload Me
End Sub
Private Sub CmdCancel3_Click()
Unload Me
End Sub
Private Sub CmdConvert_Click()
ExportTablesADO
LabelProsesTbl.Caption = ""
End Sub
Private Sub CmdNext1_Click()
Dim strCn As String
Dim NTbl, i As Byte
Dim X As ADOX.Table
On Error GoTo BatalBuka
LabelOpenMsAccess.Visible = True
Me.Refresh
strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strCn = strCn & TxMdbName.Text
strCn = strCn & ";Persist Security Info=False"
If CnAccess.State = 1 Then CnAccess.Close
CnAccess.Open strCn
cat.ActiveConnection = CnAccess
ListSelectedTable.Clear
ListAvailableTable.Clear
NTbl = cat.Tables.Count ' - 1
For Each X In cat.Tables
' If cat.Tables.Item(i).Type = "TABLE" Then
' ListAvailableTable.AddItem cat.Tables.Item(i).Name
' End If
If X.Type = "TABLE" Then
ListAvailableTable.AddItem X.Name
End If
Next
SSTab1.Tab = 1
LabelOpenMsAccess.Visible = False
Exit Sub
BatalBuka:
LabelOpenMsAccess.Visible = False
MsgBox "Tidak bisa connect dengan MsAccess"
End Sub
Private Sub CmdNext2_Click()
Dim strCn As String
LabelOpenOracle.Visible = True
Me.Refresh
strCn = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;" & _
"User ID=" & TxUserOracle.Text & ";" & _
"Password=" & TxPasswordOracle.Text & ";" & _
"Data Source=" & TxDatabaseOracle.Text
On Error GoTo BatalConnect
If CnOracle.State = 1 Then CnOracle.Close
CnOracle.Open strCn
LabelOpenOracle.Visible = False
SSTab1.Tab = 2
Exit Sub
BatalConnect:
LabelOpenOracle.Visible = False
MsgBox "Tidak bisa connect dengan Oracle"
End Sub
Private Sub ExportTablesADO()
Dim aRs, oRs As ADODB.Recordset
Dim TblName, TblAName, TblOName As String
Dim NTbl, NFld, NIdx, NICol As Byte
Dim i, j, k As Integer
Dim StrTbl, StrFld As String
Dim StrIdx As String
Dim idxField As String
Dim Nkey As Byte
Dim StrKey As String
Dim keyName, keyType As String
Dim keyRelatedTable As String
Dim keyField As String
Dim keyRelatedField As String
Set aRs = New ADODB.Recordset
Set oRs = New ADODB.Recordset
aRs.ActiveConnection = CnAccess
aRs.CursorLocation = adUseClient
aRs.CursorType = adOpenStatic
aRs.LockType = adLockReadOnly
On Error GoTo GagalExport
NTbl = ListSelectedTable.ListCount - 1
For i = 0 To NTbl
'Create Table
TblAName = "[" & ListSelectedTable.List(i) & "]"
TblOName = CekSpasi(CStr(ListSelectedTable.List(i)))
TblName = ListSelectedTable.List(i)
StrTbl = "CREATE TABLE " & TblOName & vbCrLf & " ("
aRs.Open "Select * From " & TblAName
NFld = aRs.Fields.Count - 1
LabelProsesTbl.Caption = TblName
Me.Refresh
For j = 0 To NFld
LabelProsesTbl.Caption = TblName & "/" & "Create table"
Me.Refresh
StrFld = CekSpasi(CStr(aRs.Fields(j).Name)) & " "
Select Case aRs.Fields(j).Type
Case 202 'Text
StrFld = StrFld & "VARCHAR2 " & "(" & aRs.Fields(j).DefinedSize & ")"
Case 203 'Memo
StrFld = StrFld & "CLOB"
Case 3 'Long
StrFld = StrFld & "NUMBER(11)"
Case 17 'Byte
StrFld = StrFld & "NUMBER(3)"
Case 2 'Integer
StrFld = StrFld & "NUMBER(5)"
Case 4 'Single
StrFld = StrFld & "REAL"
Case 5 'Double
StrFld = StrFld & "FLOAT"
Case 72 'Replication ID
StrFld = StrFld & "RAW(16)"
Case 7 'Date
StrFld = StrFld & "DATE"
Case 131 'Decimal
StrFld = StrFld & "NUMBER(18,0)"
Case 6 'Currency
StrFld = StrFld & "NUMBER(18,2)"
Case 11 'YesNo
StrFld = StrFld & "NUMBER(3)"
Case 205 'OLE
StrFld = StrFld & "BLOB"
Case 20 'Hyperlink
StrFld = StrFld & "CLOB"
Case Else
StrFld = StrFld & "VARCHAR2 " & "(" & aRs.Fields(j).DefinedSize & ")"
End Select
If j < NFld Then
StrFld = StrFld & ","
End If
StrTbl = StrTbl & StrFld
Next
'Add Index
StrIdx = ""
If cat.Tables(TblName).Indexes.Count > 0 Then
NIdx = cat.Tables(TblName).Indexes.Count - 1
TblName = cat.Tables(TblName).Name
For j = 0 To NIdx
If cat.Tables(TblName).Indexes.Item(j).PrimaryKey Then
LabelProsesTbl.Caption = TblName & "/" & "Add index"
Me.Refresh
StrIdx = "Primary Key ("
NICol = cat.Tables(TblName).Indexes.Item(j).Columns.Count - 1
For k = 0 To NICol
idxField = CekSpasi(CStr(cat.Tables(TblName).Indexes.Item(j).Columns.Item(k).Name))
StrIdx = StrIdx & idxField
If k < NICol Then
StrIdx = StrIdx & ","
Else
StrIdx = StrIdx & ")"
End If
Next
End If
Next
End If
If StrIdx <> "" Then
StrTbl = StrTbl & ", " & vbCrLf & StrIdx
End If
StrTbl = StrTbl & ") "
CnOracle.Execute StrTbl
StrTbl = ""
'Export data
LabelProsesTbl.Caption = TblName & "/" & "export data"
Me.Refresh
oRs.CursorLocation = adUseServer
oRs.LockType = adLockOptimistic
oRs.CursorType = adOpenDynamic
oRs.Open "Select * From " & TblOName, CnOracle
j = aRs.Fields.Count - 1
While Not aRs.EOF
oRs.AddNew
For k = 0 To j
oRs.Fields(k).Value = aRs.Fields(k).Value
Next
oRs.Update
aRs.MoveNext
Wend
oRs.Close
aRs.Close
Next i
'Add Check constraint
For i = 0 To NTbl
TblName = ListSelectedTable.List(i)
TblOName = CekSpasi(CStr(TblName))
NFld = cat.Tables(ListSelectedTable.List(i)).Columns.Count - 1
k = 0
For j = 0 To NFld
If cat.Tables(TblName).Columns(j).Properties.Item(8).Value <> "" Then
LabelProsesTbl.Caption = TblName & "/" & "Add CHECK constraint "
Me.Refresh
k = k + 1
StrTbl = "ALTER TABLE " & TblOName & " ADD CONSTRAINT "
StrTbl = StrTbl & TblOName & Format(k, "00") & vbCrLf & "CHECK ("
StrTbl = StrTbl & cat.Tables(TblName).Columns(j).Name & " "
StrTbl = StrTbl & cat.Tables(TblName).Columns(j).Properties.Item(8).Value
StrTbl = StrTbl & ") ENABLE Validate"
CnOracle.Execute StrTbl
End If
Next
Next i
'Add Foreign key
For i = 0 To NTbl
TblAName = "[" & ListSelectedTable.List(i) & "]"
TblOName = CekSpasi(CStr(ListSelectedTable.List(i)))
TblName = ListSelectedTable.List(i)
If cat.Tables(TblName).Keys.Count > 0 Then
Nkey = cat.Tables(TblName).Keys.Count - 1
LabelProsesTbl.Caption = TblName & "/" & "Add Foreign key constraint"
Me.Refresh
'TblOName = CekSpasi(CStr(cat.Tables(TblName).Name))
For j = 0 To Nkey
StrKey = ""
If cat.Tables(TblName).Keys.Item(j).Type = 2 Then
keyName = CekSpasi(CStr(cat.Tables(TblName).Keys.Item(j).Name))
keyRelatedTable = CekSpasi(CStr(cat.Tables(TblName).Keys.Item(j).RelatedTable))
StrKey = "ALTER TABLE " & TblOName & " ADD (FOREIGN KEY ("
NICol = cat.Tables(TblName).Keys.Item(j).Columns.Count - 1
For k = 0 To NICol
idxField = CekSpasi(CStr(cat.Tables(TblName).Keys.Item(j).Columns.Item(k).Name))
StrKey = StrKey & idxField
If k < NICol Then
StrKey = StrKey & ","
Else
StrKey = StrKey & ")"
End If
Next
StrKey = StrKey & " References " & keyRelatedTable & " ("
For k = 0 To NICol
keyRelatedField = CekSpasi(CStr(cat.Tables(TblName).Keys.Item(j).Columns.Item(k).RelatedColumn))
StrKey = StrKey & keyRelatedField
If k < NICol Then
StrKey = StrKey & ","
Else
StrKey = StrKey & ")"
End If
Next
StrKey = StrKey & ")"
CnOracle.Execute StrKey
End If
Next
End If
Next i
LabelProsesTbl.Caption = "Selesai"
Me.Refresh
MsgBox "Export ke oracle Selesai"
Exit Sub
GagalExport:
MsgBox "Export ke oracle gagal " & vbCrLf & _
Err.Description
End Sub
Private Sub CmdPrev3_Click()
SSTab1.Tab = 1
End Sub
Private Sub CmdRemove_Click()
Dim i As Byte
i = 0
Do
If ListSelectedTable.Selected(i) Then
ListAvailableTable.AddItem ListSelectedTable.List(i)
ListSelectedTable.RemoveItem (i)
End If
i = i + 1
Loop While i <= ListSelectedTable.ListCount - 1
End Sub
Private Sub CmdRemoveAll_Click()
Dim i As Byte
i = 0
Do
ListAvailableTable.AddItem ListSelectedTable.List(i)
i = i + 1
Loop While i <= ListSelectedTable.ListCount - 1
ListSelectedTable.Clear
End Sub
Private Sub Form_Load()
Dim i, NTbl As Integer
Set CnAccess = New ADODB.Connection
Set CnOracle = New ADODB.Connection
Set cat = New ADOX.Catalog
TxMdbName.Text = ""
CheckPassword.Value = 0
TxPassword.Enabled = False
SSTab1.Tab = 0
TxDatabaseOracle.Text = ""
TxUserOracle.Text = ""
TxPasswordOracle.Text = ""
LabelOpenMsAccess.Visible = False
LabelOpenOracle.Visible = False
LabelProsesTbl.Caption = ""
End Sub
Private Function CekSpasi(X As String) As String
CekSpasi = Replace(RTrim(X), " ", "_")
End Function