我想根据时间段查询SQL数据库内的数据,下面是我编写的代码,万能的网友帮忙看下查询语句错在哪里导致一直查询拿不出来。控件2、控件3是WINCCdtp控件
Sub OnClick(ByVal Item)
Dim conn '定义类对象
Dim SCon '定义数据库连接字符串
Dim oRs1 '定义获取到的数据集
Dim oCom
Dim strSQL1
Dim m,i,j,k,DATE1,date2,date3,date4,date5
Dim startDate,endDate
Set date2=ScreenItems("控件2")
date4 = FormatDateTime(date2.Value,2)
Msgbox(date4)
startDate=date4 & " 00:00:00"
Set date3=ScreenItems("控件3")
date5 = FormatDateTime(date3.Value,2)
endDate= date5 & " 23:59:59"
'---------------------打开数据库 --------------------'
sCon= "Provider=SQLOLEDB; Integrated Security =SSPI;Persist Security Info=False; Initial Catalog=生产统计; Data Source=ZKCD\WINCC"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
Set oRs1 = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Msgbox(endDate)
'---------------------查询数据库 --------------------'
'strSQL1= "SELECT * FROM [生产数据]"
strSQL1 = "SELECT * FROM [生产数据] WHERE sj BETWEEN ”" & startDate & "” And ”" & endDate & "” ORDER BY sj "
Set oCom.ActiveConnection = conn
oCom.CommandText = strSQL1
Set oRs1 = oCom.Execute
m = oRs1.RecordCount
MsgBox("查询到表格共有" & m &"行数据")