select * from allrecord between #" & Format(DTPstart.Value, "yyyy-mm-dd") & "#and #" & Format(DTPend.Value, "yyyy-mm-dd") & "#""
上面的语句总是无法通过编译。
[此贴子已经被作者于2007-9-18 16:01:52编辑过]
[此贴子已经被作者于2007-9-18 16:01:52编辑过]
该语句的环境是;Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cnn.Open "Provider=SQLOLEDB.1;User ID=sa;pwd=123456;Initial Catalog=Northwind;Data Source= LOCALHOST ;"
rs.Open "select * from allrecord between #" & Format(DTPstart.Value, "yyyy-mm-dd") & "#and #" & Format(DTPend.Value, "yyyy-mm-dd") & "#"",cnn, adOpenDynamic, adLockOptimistic, adCmdText
'rs.Open "select * from allrecord ", cnn, adOpenDynamic, adLockOptimistic, adCmdText
With MSFlexGrid2(1)
Do Until rs.EOF
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = rs!Date
.TextMatrix(.Rows - 1, 1) = rs!rwldata
.TextMatrix(.Rows - 1, 2) = rs!bmdata
.TextMatrix(.Rows - 1, 3) = rs!tmdata
rs.MoveNext
Loop
End With
cnn.Close
报错提示是:实时报错:“3079”,连接无法用于此操作。在此上下文中它可能已被关闭或者无效。
但是如果把rs.Open "select * from allrecord between #" & Format(DTPstart.Value, "yyyy-mm-dd") & "#and #" & Format(DTPend.Value, "yyyy-mm-dd") & "#"",cnn, adOpenDynamic, adLockOptimistic, adCmdText
改成rs.Open "select * from allrecord ", cnn, adOpenDynamic, adLockOptimistic, adCmdText
就可以通过,显然不是rs的连接问题,应该是这句话在此上下文中可能无效。
请大家帮忙分析下,看按时间段查询怎么来写?已经按照3楼的方法测试了,还是出现同样的错误。
开始没看到4楼的回复,就有了5楼回复2楼
,后来用4楼的方法测试了下,发现还是不行。出现的错误提示:
实时错误:‘-2147217900(80040e14)',
在关键字’convert‘附件有语法错误。
這樣都 有錯??不會吧。。。
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cnn.Open "Provider=SQLOLEDB.1;User ID=sa;pwd=123456;Initial Catalog=Northwind;Data Source= LOCALHOST ;"
rs.Open "select * from allrecord where convert(varchar(10),日期字段,112) between convert(varchar(10),'"& DTPstart.Value &"',112) and convert(varchar(10),'"& DTPend.Value &"',112)",cnn,adOpenDynamic,adLockOptimistic
With MSFlexGrid2(1)
Do Until rs.EOF
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = rs!Date
.TextMatrix(.Rows - 1, 1) = rs!rwldata
.TextMatrix(.Rows - 1, 2) = rs!bmdata
.TextMatrix(.Rows - 1, 3) = rs!tmdata
rs.MoveNext
Loop
End With
cnn.Close
[此贴子已经被作者于2007-9-18 15:31:30编辑过]