Hello all. I'm new to VBA/Oracle and I need your help.
I have the following codes that extract data from Oracle. It does the trick in bringing data from Oracle but would like to limit the output using a worksheet within the current spreadsheet.
Need to filled worksheet "Sample Data" both from Oracle table and Worksheet Data.
Example:
BH_FA WBS DWG_NO Prty P_Date Addr Lvl
MT03564896 CA01 CA01-0S-671017-07-I7 880 20090102 D01 I7
where BH_FA, DWG_NO & SHT Rev will come from Oracle table and WBS Prty P_Date Addr will come from spreasheet called "Data".
Worksheet Data have this
WBS Prty P_Date M_Grp Addr. MTO_LVL
CA01 880 20090102 11 D01 I7
CA01 880 20090102 14 D01 I7
Oracle field must by equal to Worksheet table field.
[Data$]WBS = [BOM_HDR]BH_DL_CD
[Data$]Addr. = [BOM_HDR]BH_ADDR_CD
[Data$]MTO_LVL = [BOM_HDR]BH_MTO_LVL_CD
Thanks in advance.
Regards,
John
I have the following codes that extract data from Oracle. It does the trick in bringing data from Oracle but would like to limit the output using a worksheet within the current spreadsheet.
Need to filled worksheet "Sample Data" both from Oracle table and Worksheet Data.
Example:
BH_FA WBS DWG_NO Prty P_Date Addr Lvl
MT03564896 CA01 CA01-0S-671017-07-I7 880 20090102 D01 I7
where BH_FA, DWG_NO & SHT Rev will come from Oracle table and WBS Prty P_Date Addr will come from spreasheet called "Data".
Worksheet Data have this
WBS Prty P_Date M_Grp Addr. MTO_LVL
CA01 880 20090102 11 D01 I7
CA01 880 20090102 14 D01 I7
Oracle field must by equal to Worksheet table field.
[Data$]WBS = [BOM_HDR]BH_DL_CD
[Data$]Addr. = [BOM_HDR]BH_ADDR_CD
[Data$]MTO_LVL = [BOM_HDR]BH_MTO_LVL_CD
Code:
Sub OracleExcel()
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim OraDynaSet As Object
Dim objSession As Object
Dim objDataBase As Object
Dim i As Integer
'Create a reference to my database
Database_Name = "XXXX" ' Enter your database name here
User_ID = "User" ' enter your user ID here
Password = "Password" ' Enter your password here
'Create a reference to the OO4O dll
Set objSession = CreateObject("OracleInProcServer.XOraSession")
Set objDataBase = objSession.OpenDatabase(Database_Name, User_ID & "/" & Password, 0&)
SQLStr = "SELECT BH_SUB_PROJ, BH_FAST_ACCESS, BH_DL_CD, BH_DOC_NO, BH_SHT_NO, BH_DOC_REV_NO FROM BOM_HDR WHERE BH_MTO_LVL_CD = 'I7' and BH_ADDR_CD = 'D01' and BH_DL_CD = 'CA01'"
objDataBase.ExecuteSQL (SQLStr)
'Retrieve the results from Oracle
Set OraDynaSet = objDataBase.DBCreateDynaset(SQLStr, 0&)
If OraDynaSet.RecordCount > 0 Then
'There were records retrieved
OraDynaSet.MoveFirst
'Loop the recordset for returned rows
For i = 2 To OraDynaSet.RecordCount
'Put the results in column A, B, D, E and F
Sheets("Sample Data").Cells(i, 1) = OraDynaSet.Fields(0).Value
Sheets("Sample Data").Cells(i, 2) = OraDynaSet.Fields(1).Value
Sheets("Sample Data").Cells(i, 4) = OraDynaSet.Fields(3).Value
Sheets("Sample Data").Cells(i, 5) = OraDynaSet.Fields(4).Value
Sheets("Sample Data").Cells(i, 6) = OraDynaSet.Fields(5).Value
OraDynaSet.MoveNext
Next i
End If
Set OraDynaSet = Nothing
Set objSession = Nothing
objDataBase.Close
Set objDataBase = Nothing
End Sub
Regards,
John