views:

1260

answers:

1

Hi, I am building a reconciliation tool via VBA that automates queries from my oracle database and a worksheet. When I run the query I want the user to input what ITEM (in this case pipeline) to query (the worksheet has many items) and the end/start dates. I am having trouble figuring out the following:

1) It is querying - if the value returned is NULL, how may I tell it to print out "DATA NOT AVAILABLE"
2) How can I clear up the old sql statement/ data output from pipeline A, when I am querying pipeline B?
3) My dates are saved as strings in Oracle - how can I convert that to date?
Thank you!

Here is what I have so far:

Option Explicit
Option Base 1

Dim cnnObject As ADODB.Connection
Dim rsObject As ADODB.Recordset
Dim strGPOTSConnectionString As String

Dim Pipeline As String
Dim DateStart As Date
Dim DateEnd As Date
Dim strQuery As String

Sub ClickButton2()

Debug.Print ("Button has been clicked")
Pipeline = InputBox("Enter PipeLine", "My Application", "Default Value")
DateStart = InputBox("Enter Start Date", "My Application", DateTime.Date)
DateEnd = InputBox("Enter End Date", "My Application", DateTime.Date + 1)

Range("B1").Value = Pipeline
Range("B2").Value = DateStart
Range("B3").Value = DateEnd

strQuery = "select pipelineflow.lciid lciid, ldate, volume, capacity, status, " & _
"pipeline, station, stationname, drn, state, county, owneroperator, companycode, " & _
"pointcode, pointtypeind, flowdirection, pointname, facilitytype, pointlocator, " & _
"pidgridcode from pipelineflow, pipelineproperties " & _
"where pipelineflow.lciid = pipelineproperties.lciid " & _
"and pipelineflow.audit_active = 1 " & _
"and pipelineproperties.audit_active =1 " & _
"and pipelineflow.ldate >= '" & Format(DateStart, "dd-MMM-yyyy") & "' and pipelineflow.ldate < '" & Format(DateEnd, "dd-MMM-yyyy") & "' " & _
"and pipelineproperties.pipeline = '" & Pipeline & "' "


    Call PullZaiNetData(strQuery)

    Call TieOut

End Sub

Sub PullZaiNetData2(ByVal strQry As String)

    Set cnnObject = New ADODB.Connection
    Set rsObject = New ADODB.Recordset

    strGPOTSConnectionString = "DRIVER={Microsoft ODBC for Oracle}; SERVER=XYZ; PWD=XYZ; UID=XYZ"


    cnnObject.Open strGPOTSConnectionString

    rsObject.Open strQry, cnnObject, adOpenStatic
    Worksheets("ZaiNet Data").Cells(1, 1).CopyFromRecordset rsObject


    rsObject.Close
    cnnObject.Close

    Set rsObject = Nothing
    Set cnnObject = Nothing

End Sub

Sub TieOut()

End Sub

In addition if I wanted to change things around, how might I let the user input dates and pipeline into a cell instead of being prompted? I know that I should do something like setting:

startDate = Worksheets("Instructions").Cells(5, 4).Value

But would this be possible?

+1  A: 
  1. To replace your nulls you can use the Oracle's NVL function. e.g. "SELECT NVL(pipelineflow.lciid, 'DATA NOT AVAILABLE')..."

  2. To clear out the old data you could use Worksheets("ZaiNet Data").Cells.Clear

  3. To convert your date strings to dates use Oracle's TO_DATE function, e.g. "SELECT TO_DATE(ldate, 'dd-mon-yyyy')...", where dd-mon-yyyy is the date format of the date string in the database.

Your code to read the date from a worksheet will work as long as the value in the cell can be cast to a date. You may want to use worksheet validation to make sure users can only enter a valid date.

Graham Miller