用EXCEL做报表,报表连接的是SQL数据库中的表,在工作表中可以用DTPicker控件吗?让用户能够输入查询日期和时间
Sub data()Dim lcConnectionString, lcCommandText As String
Dim loADODBConnection As ADODB.Connection
Dim loADODBRecordset As ADODB.Recordset
Dim i As Integer
Dim time_1 As Variant
Dim time_2 As Variant
time_1 = DTPicker1.Value & " " & DTPicker2.Hour & ":" & DTPicker2.Minute & ":" & DTPicker2.Second
time_2 = DTPicker3.Value & " " & DTPicker4.Hour & ":" & DTPicker4.Minute & ":" & DTPicker4.Second
lcConnectionString = "Driver={SQL Server}; " & _
"Server= (local);" & _
"Database=建筑设备节能 ;" & _
"Uid=sa;" & _
"Pwd=304"
If DTPicker1.Value > DTPicker3.Value Then
MsgBox "你输入的截止日期不小于起始日期,请重新输入!", , "警告"
Else
If DTPicker2.Value > DTPicker4.Value And DTPicker1.Value = DTPicker3.Value Then
MsgBox "你输入的截止时间不小于起始时间,请重新输入!", , "警告"
Else
lcCommandText = "select * from RoomData where DateTime between ' " & time_1 & " ' and ' " & time_2 & " '"
Set loADODBConnection = CreateObject("ADODB.Connection")
Set loADODBRecordset = CreateObject("ADODB.Recordset")
loADODBConnection.Open lcConnectionString
loADODBRecordset.Open lcCommandText, loADODBConnection, 3, 1, 1
Dim r, f As Integer
r = 15
For f = 0 To loADODBRecordset.Fields.Count - 1
sheets(1).Cells(r, f + 1) = loADODBRecordset.Fields(f).Name
Next
While Not loADODBRecordset.EOF
r = r + 1
For f = 0 To loADODBRecordset.Fields.Count - 1
sheets(1).Cells(r, f + 1) = loADODBRecordset.Fields(f).Value
Next
loADODBRecordset.MoveNext
Wend
loADODBConnection.Close
sheets(1).Cells.EntireColumn.AutoFit
End Sub
老提示变量未定义,我是菜鸟,希望得到大家帮助?谢谢
首先,你得说清楚是那句报的变量未定义的错误。
其次,报的错误很明确,就是变量未定义。 sheets(1)没定义 有没有引用ADO? DTPicker 有问题吗?我是用VBA做的,DTPicker控件就画在sheet(1)上 没有用ADO吧?我也不懂,刚开始学。是ODBC连接数据源。 变量未定义显示在DTPicker1的地方 报表制作推荐使用
http://topic.csdn.net/u/20101024/15/975b970e-3e8c-44a0-886c-97e8f23dd6c4.html?43607
补充:VB , 数据库(包含打印,安装,报表)