Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run query, automate using VBA Macro and Excel, make "loading" feature while reconciling?
    primarykey
    data
    text
    <p>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:</p> <p><strong>1) It is querying - if the value is NULL, how may I tell it to print out "DATA NOT AVAILABLE"<br> 2) How can I clear up the old output from pipeline A, when I am querying pipeline B?<br> 3) My dates are saved as strings in Oracle - how can I convert that to date?<br> Thank you!</strong></p> <p>Here is what I have so far:</p> <pre><code>Option Explicit Option Base 1 Dim cnnObject As ADODB.Connection Dim rsObject As ADODB.Recordset Dim strGPOTSConnectionString As String Dim startDate As Date Dim endDate As Date Dim strPipelineName As String Dim strQuery As String Sub ClickButton2() Debug.Print ("Button has been clicked") Dim Pipeline As String Dim DateStart As Date Dim DateEnd As Date 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) Pipeline = Range("B1").Value DateStart = Range("B2").Value DateEnd = Range("B3").Value strQuery = "select pipelineflow.lciid lciid, ldate, volume, capacity, status, " &amp; _ "pipeline, station, stationname, drn, state, county, owneroperator, companycode, " &amp; _ "pointcode, pottypeind, flowdirection, pointname, facilitytype, pointlocator, " &amp; _ "pidgridcode from pipelineflow, pipelineproperties " &amp; _ "where pipelineflow.lciid = piplineproperties.lciid " &amp; _ "and pipelineflow.audit_active = 1 " &amp; _ "and pipelineproperties.audit_active =1 " &amp; _ "and pipelineflow.ldate &gt;= '" &amp; Format(DateStart, "dd-MMM-yyyy") &amp; "' and pipelineflow.ldate &lt; '" &amp; Format(DateEnd, "dd-MMM-yyyy") &amp; "' " &amp; _ "and pipelineflow.ldate &gt;= '" &amp; DateStart &amp; "' and pipelineflow.ldate &lt; '" &amp; DateEnd &amp; "' " &amp; _ "and pipelineproperties.pipeline = '" &amp; Pipeline &amp; "' " 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=hhh; PWD=hhhh; UID=hhh" 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COYour code is a little hard to read. You need to remove the declarations you aren't using (startDate, endDate, strPipelineName), and you should indent things correctly. What error are you getting with the "DateEnd = Range("B3").Value" statement? Is there anything in that cell? Is it a valid date value? Also, you assign each of these values twice. It looks like you copied the code from the answer to your previous question without seeing the duplication. Those were just examples of two different ways to do it, from user input or from cells.
      singulars
    2. COWhen you are building the query string, you are duplicating the line with ldate, once with the Format() function and once without. Also, is that a real password in your connection string? You need to remove that from your example code. You should create a System DSN and simply refer to that as your connection string instead of all of those details.
      singulars
    3. COAfter setting strQuery you are calling PullZaiNetData but the declaration further down is PullZaiNetData2. Not sure if that's a typo here or an error in your program. Also, Call is unnecessary
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload