请高手帮请帮忙看我做的一个报表程序:
Private Sub Command6_Click()
On Error GoTo Excet
Dim dtmMonth As String
Dim StartDate As String
Dim i As Long
StartDate = Format(Me.DtpStart, "yyyy-mm-dd HH:mm:ss")
dtmMonth = Format(Me.DtpEND, "yyyy-mm-dd HH:mm:ss")
‘引用ADO控件
Dim Sql As String
Dim cnADO As New ADODB.Connection
Dim rsADO As New ADODB.Recordset
‘查询控制设置
Sql = "SELECT VALUE,DATETIME,TAG,FROM FIX" & _
"WHERE ( DATETIME>={ts ‘" & StartDate & "‘}" & _
"AND DATETIME<={ts‘" & dtmMonth & "‘} )" & _
"AND (TAG IN(‘TEST‘,‘STEAM_PRESS_SPEED‘))" & _
"AND INTERWAL=‘00:01:00‘"
‘连接并读取数据库
Set cnADO = New ADODB.Connection
Set rsADO = New ADODB.Recordset
cnADO.ConnectionString = "Provider = Microsoft OLE DB Provider for ODBC Drivers;DSN=FIX Dynamics Historical Data;UID=;PWD="
cnADO.Open "FIX Dynamics Historical Data"
rsADO.CursorLocation = adUseClient
rsADO.Open Sql, cnADO, adOpenDynamic, adLockUnspecified, -1 ‘查询数据
‘判断数据有无
If rsADO.RecordCount <= 0 Then
MsgBox "无数据!" ‘ ", vbOKOnly + vbInformation, "信息..."
‘Set cnADO = Nothing
‘Set rsADO = Nothing
Exit Sub
End If
Dim ExcelApp As Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet
‘Set ExcelApp = New Excel.Application
‘xlApp.DisplayAlerts = False
‘xlApp.Visible = False
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(System.ProjectPath & "\APP\Excel.xls")
Set ExcelSheet = xlBook.Worksheets(1)
For i = 1 To rsADO.RecordCount
With ExcelSheet
.range("B5") = rsADO!DATERTIME & ""
If rsADO!Value & "" = "" Then
.range("C5") = "无数据"
Else
.range("C5") = rsADO!Value & ""
End If
End With
ExcelBook.Save
ExcelBook.Close
With Me.WebBrowser1
.Navigate System.ProjectPath & "\APP\Excel.xls"
.AddressBar = False
End With
Next i
Set ExcelApp = Nothing
Set ExcelBook = Nothing
Set ExcelSheet = Nothing
Set cnADO = Nothing
Set rsADO = Nothing
Exit Sub
Excet:
Set ExcelApp = Nothing
Set ExcelBook = Nothing
Set ExcelSheet = Nothing
Set cnADO = Nothing
Set rsADO = Nothing
Resume
End Sub
单句调试的时候每次运行到:rsADO.Open Sql, cnADO, adOpenDynamic, adLockUnspecified, -1 ‘查询数据 就跳回错误Excet:了,请高手看看是什么原因找成的,谢谢!!