access2007数据库用SQL语句查询时间段内的数据 100
用VB6中ADO控件链接access2007数据库,通过“时间”字段查询下列数据。时间数值2010-1-100:01:0012010-1-100:02:0022010-1...
用VB6中ADO控件链接access2007数据库,通过“时间”字段查询下列数据。
时间 数值
2010-1-1 00:01:00 1
2010-1-1 00:02:00 2
2010-1-1 00:03:00 3
2010-1-1 00:04:00 4
2010-1-1 00:05:00 5
2010-1-1 00:06:00 6
2010-1-1 00:07:00 7
2010-1-1 00:08:00 8
2010-1-1 00:09:00 9
2010-1-1 00:10:00 10
需要的查询结果是
2010-1-1 00:03:00 3
2010-1-1 00:04:00 4
2010-1-1 00:05:00 5
2010-1-1 00:06:00 6
2010-1-1 00:07:00 7
2010-1-1 00:08:00 8
代码如下:
dim Cnn As New ADODB.Connection
dim RS1 As New ADODB.Recordset
Cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + App.Path +"\data\data.accdb" + ";Persist Security Info=False;"
Cnn.Mode = adModeReadWrite
Cnn.Open
If Cnn.State = adStateOpen Then
Dim D1 As Date, D2 As Date
D1 = CDate("2010-1-1 00:03:00")
D2 = CDate("2010-1-1 00:08:00")
sql = "SELECT 数值 FROM opt WHERE 时间 between " & D1 & " AND " & D2 & " ORDER BY 时间"
RS1.Open sql, Cnn, 1, 3
end if
但是执行的时候总是报错“语法错误(操作符丢失)在查询表达式 ‘时间 between 2010-1-1 00:03:00 AND 2010-1-1 00:08:00’中”
尝试过更改D1,D2为String类型,报错类型不符,把数据库中时间字段改成文本用文本查询,查不到记录。各位帮帮忙,看看这段SQL语句怎么写才能查到数据,多谢! 展开
时间 数值
2010-1-1 00:01:00 1
2010-1-1 00:02:00 2
2010-1-1 00:03:00 3
2010-1-1 00:04:00 4
2010-1-1 00:05:00 5
2010-1-1 00:06:00 6
2010-1-1 00:07:00 7
2010-1-1 00:08:00 8
2010-1-1 00:09:00 9
2010-1-1 00:10:00 10
需要的查询结果是
2010-1-1 00:03:00 3
2010-1-1 00:04:00 4
2010-1-1 00:05:00 5
2010-1-1 00:06:00 6
2010-1-1 00:07:00 7
2010-1-1 00:08:00 8
代码如下:
dim Cnn As New ADODB.Connection
dim RS1 As New ADODB.Recordset
Cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + App.Path +"\data\data.accdb" + ";Persist Security Info=False;"
Cnn.Mode = adModeReadWrite
Cnn.Open
If Cnn.State = adStateOpen Then
Dim D1 As Date, D2 As Date
D1 = CDate("2010-1-1 00:03:00")
D2 = CDate("2010-1-1 00:08:00")
sql = "SELECT 数值 FROM opt WHERE 时间 between " & D1 & " AND " & D2 & " ORDER BY 时间"
RS1.Open sql, Cnn, 1, 3
end if
但是执行的时候总是报错“语法错误(操作符丢失)在查询表达式 ‘时间 between 2010-1-1 00:03:00 AND 2010-1-1 00:08:00’中”
尝试过更改D1,D2为String类型,报错类型不符,把数据库中时间字段改成文本用文本查询,查不到记录。各位帮帮忙,看看这段SQL语句怎么写才能查到数据,多谢! 展开
展开全部
sql = "SELECT 数值 FROM opt WHERE 时间 between #" & D1 & "# AND #" & D2 & "# ORDER BY 时间" 时间类型要加#号
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用#代替&试下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询