Hello All.
I am new to VBA/Oracle and I've been task to write an application that will
extract data from Oracle and then bring them into Excel which I guess similar
to the problem of Sakol.
I manage to extract the data from Oracle and saved them into Excel but I
need to limit the output using a worksheet within the excel file.
Can you please help me how can I use the worksheet called "Data" as part of
the query and then write them to a different worksheet.
Below are my codes which I found in the net.
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 = "i021" ' Enter your database name here
User_ID = "ops$access" ' enter your user ID here
Password = "access" ' 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"
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
'End If
Next i
End If
Set OraDynaSet = Nothing
Set objSession = Nothing
objDataBase.Close
Set objDataBase = Nothing
End Sub
I have the following from worksheet data as an example.
WBS Priority P_Date M_Grp Addr. MTO_LVL
CA01 880 20090102 11 D01 I7
CA01 880 20090102 14 D01 I7
Need to filled worksheet Sample Data both from Oracle table and Worksheet
Data.
Sub Proj BH_FA WBS DWG_NO SHT Rev Priority P_Date M_Grp Addr. MTO_LVL
Fields WBS, Priority, P_Date, M_Grp, Addr., MTO_LVL will come from Worksheet
Data the rest from Oracle.
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
Hope you guys can help me.
Regards,
John