关于vba的问题,请指教,万分感激
在rsview32里做报表,用vba语句,调用acess数据库,但在运行时提示错误代码80040e07,日期语法错误!
程序如下:
Sub BS_GetInfo()
Dim BS_cn As ADODB.Connection
Dim BS_rs As ADODB.Recordset
Dim BS_strCmd As String
Dim BS_strCmd1 As String
Dim BS_strCmd2 As String
Dim BS_strCmd3 As String
Dim BS_strCmd4 As String
Dim BS_strCmd5 As String
Dim BS_strCmd6 As String
Dim BS_strCmd7 As String
Dim BS_strCmd8 As String
Dim BS_strCmd9 As String
Dim BS_strCmd10 As String
Dim BS_strCmd11 As String
Dim BS_strCmd12 As String
Dim BS_strCmd13 As String
Dim BS_strCmd14 As String
Dim BS_strCmd15 As String
Dim BS_strCmd16 As String
Dim BS_strCmd17 As String
Dim BS_strCmd18 As String
Dim BS_strCmd19 As String
Dim BS_strCmd20 As String
Dim BS_strCmd21 As String
Dim BS_strCmd22 As String
Dim BS_strCmd23 As String
Dim BS_strCmd24 As String
Dim BS_strCmd25 As String
Dim BS_strCmd26 As String
Dim BS_strCmd27 As String
Dim BS_strCmd28 As String
Dim BS_strCmd29 As String
Dim BS_strCmd30 As String
Dim BS_strCmd31 As String
Dim BS_strCmd32 As String
Dim BS_strCmd33 As String
Dim BS_strCmd34 As String
Dim BS_strCmd35 As String
Dim BS_strCmd36 As String
Dim BS_strCmd37 As String
Dim BS_strCmd38 As String
Dim BS_strCmd39 As String
Dim BS_strCmd40 As String
Dim BS_strCmd41 As String
Dim BS_strCmd42 As String
Dim BS_strCmd43 As String
Dim BS_strTag As String
Dim BS_I As Long
Dim BS_x As String
Set BS_cn = New ADODB.Connection
Set BS_rs = New ADODB.Recordset
BS_cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source=d:\BS_TABLE\BS_sheet.mdb;"
BS_x = gTagDb("BS\strtime").Value
With BS_rs
.ActiveConnection = BS_cn
BS_strCmd = BS_CreateSel("BS\11", CDate(BS_x))
.Open BS_strCmd, , adOpenStatic, adLockReadOnly
End With
BS_rs.MoveFirst
BS_I = 0
While Not BS_rs.EOF
BS_I = BS_I + 1
BS_strTag = "BS\table1_val" & BS_I
gTagDb(BS_strTag).Value = BS_rs!Val
BS_rs.MoveNext
Wend
BS_rs.Close
BS_cn.Close
……………………………………..
……………………………………….( 中间部分省略,跟上面第一段一样)
End Sub
Private Function BS_CreateSel(BS_strName As String, BS_dTime As Date) As String
Dim BS_dTimeahad As Date, BS_dTimelate As Date
BS_dTimeahad = DateAdd("s", -3, BS_dTime)
BS_dTimelate = DateAdd("s", 3, BS_dTime)
BS_CreateSel = "SELECT top 24 FloatTable.* ,TagTable.* FROM FloatTable,TagTable WHERE TagTable.TagName='" _
& BS_strName & "'and FloatTable.TagIndex=TagTable.TagIndex " _
& "AND FloatTable.DateAndTime > #" & BS_dTimeahad & "# order by FloatTable.DateAndTime"
End Function
红字部分有错误,& "AND FloatTable.DateAndTime > #" & BS_dTimeahad & "# order by FloatTable.DateAndTime把这段去掉后,运行无错误,但不能实现按日期查询数据功能!
请各位大侠指教,急!!万分感激!
[解决办法]
干吗非要加个‘上午/下午’啊?直接用24时制的时间不行吗!
"AND FloatTable.DateAndTime > '#" & format$(BS_dTimeahad,"yyyy-mm-dd hh:mm:ss") & "#' order by FloatTable.DateAndTime"
[解决办法]
Dim strTemp$ If (Hour(BS_dTimeahad) < 12) Then strTemp = Replace(Format$(BS_dTimeahad, "yyyy-mm-dd 上午TT:nn:ss"), "TT", Hour(BS_dTimeahad), 1, 1) Else strTemp = Replace(Format$(BS_dTimeahad, "yyyy-mm-dd 下午TT:nn:ss"), "TT", Hour(BS_dTimeahad), 1, 1) End If BS_CreateSel = "SELECT top 24 FloatTable.* ,TagTable.* FROM FloatTable,TagTable WHERE TagTable.TagName='" _ & BS_strName & "'and FloatTable.TagIndex=TagTable.TagIndex " _ & "AND FloatTable.DateAndTime > #" & strTemp & "# order by FloatTable.DateAndTime"
[解决办法]
我来回答你的问题,根本没有那么麻烦,很简单嘛
1、你的原程序不用动,把系统时间格式改成:HH:mm:ss
步骤:控制面板--区域和语言选项--区域选项--自定义--时间-时间格式选择:HH:mm:ss
你的系统时间格式原来肯定是:tt hh:mm:ss
2、系统时间格式不变,那你就修改你的原程序:
.
.
.
Dim BS_x As String
Set BS_cn = New ADODB.Connection
Set BS_rs = New ADODB.Recordset
BS_cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source=d:\BS_TABLE\BS_sheet.mdb;"
BS_x = gTagDb("BS\strtime").Value
If InStr(BS_x, "下午") Then
BS_x = Split(BS_x)(0) & " " & Split(BS_x)(2) & " " & "pm"
ElseIf InStr(BS_x, "上午") Then
BS_x = Split(BS_x)(0) & " " & Split(BS_x)(2) & " " & "am"
End If
.
.
.