我正在通过VBA构建一个对帐工具,它可以自动执行来自我的oracle数据库和工作表的查询。当我运行查询时,我希望用户输入要查询的项目(在这种情况下为管道)(工作表中有许多项目)和结束/开始日期。我有麻烦搞清楚以下几点:如何运行查询,使用VBA宏和Excel自动化,在协调时进行“加载”功能?
1)它查询 - 如果该值为NULL,请问有什么可以告诉它打印出“数据不可用”
2)我怎样才能清理旧管道A的输出,当我查询管道B时?
3)我的日期在Oracle中保存为字符串 - 我如何将其转换为日期?
谢谢!
这是我到目前为止有:
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, " & _
"pipeline, station, stationname, drn, state, county, owneroperator, companycode, " & _
"pointcode, pottypeind, flowdirection, pointname, facilitytype, pointlocator, " & _
"pidgridcode from pipelineflow, pipelineproperties " & _
"where pipelineflow.lciid = piplineproperties.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 pipelineflow.ldate >= '" & DateStart & "' and pipelineflow.ldate < '" & DateEnd & "' " & _
"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=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
你的代码有点难以阅读。你需要删除你没有使用的声明(startDate,endDate,strPipelineName),并且你应该正确地缩进东西。 你用“DateEnd = Range(”B3“)得到了什么错误。那个牢房里有什么东西?这是一个有效的日期值吗? 此外,您分配这些值的每个值两次。它看起来像是将答案中的代码复制到上一个问题而不会看到重复。这些只是用户输入或单元格的两种不同方式的例子。 – Todd 2009-07-09 17:17:02
当您构建查询字符串时,您将使用ldate复制该行,一次使用Format()函数,一次不使用。 此外,你的连接字符串是否是一个真正的密码?你需要从示例代码中删除它。您应该创建一个系统DSN,并简单地将其称为连接字符串,而不是所有这些详细信息。 – Todd 2009-07-09 17:21:29