用的是ADO查询
txtsql = "select * from table1 where Chart_fre=" & CInt(TxtFre.Text) & " and Chart_operate='" & Trim(Cmbopa.Text) & "' and Chart_Stime >= #" & DTpsta.Value & "# and Chart_Etime <= #" & DTPend.Value & "#" 'Set mrc_cha = Execute_SQL(txtsql, MsgText, cnn)Public Function Execute_SQL(ByVal SQL _
As String, MsgString As String, _
conn As ADODB.Connection) _
As ADODB.Recordset Dim Rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo Execute_SQL_Error
sTokens = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
conn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set Rst = New ADODB.Recordset
Rst.Open SQL, conn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set Execute_SQL = Rst
MsgString = "查询到" & Rst.RecordCount & _
" 条记录 "
End If
Execute_SQL_Exit:
Set Rst = Nothing
Exit Function
Execute_SQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume Execute_SQL_Exit
End Function
关键是时间段的查询,执行后,有的记录可以查询到,有的记录查询不到,请教高手这是什么原因?
txtsql = "select * from table1 where Chart_fre=" & CInt(TxtFre.Text) & " and Chart_operate='" & Trim(Cmbopa.Text) & "' and Chart_Stime >= #" & DTpsta.Value & "# and Chart_Etime <= #" & DTPend.Value & "#" 'Set mrc_cha = Execute_SQL(txtsql, MsgText, cnn)Public Function Execute_SQL(ByVal SQL _
As String, MsgString As String, _
conn As ADODB.Connection) _
As ADODB.Recordset Dim Rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo Execute_SQL_Error
sTokens = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
conn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set Rst = New ADODB.Recordset
Rst.Open SQL, conn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set Execute_SQL = Rst
MsgString = "查询到" & Rst.RecordCount & _
" 条记录 "
End If
Execute_SQL_Exit:
Set Rst = Nothing
Exit Function
Execute_SQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume Execute_SQL_Exit
End Function
关键是时间段的查询,执行后,有的记录可以查询到,有的记录查询不到,请教高手这是什么原因?
解决方案 »
- VB6增强搜索插件,希望大家支持!
- 关于WebBrowser的两个难题,请高手赐教!
- 水晶報表的數據源問題?
- 怎样遍历,listbox中的ItemData
- HTML Help Workshop中如何添加VB帮助contextID的调用?
- 如何使用空格和星号打印一棵圣诞树
- 各位高手怎样对数据库进行“增删改查”急需解答?望各高手指教小弟!!!
- 托盘问题-不响应Form_MouseMove事件,怎么办?
- 请问那里有XML的资料?
- 请问list控件它的style设为checkbox,哪怎样才能使控件中的每一列的check框在程序中为它添上勾号或撤消勾号,(不是用鼠标去点,而是用代码去操作)
- 找遍了csdn没找个这个问题的 满意答复!希望高手来看看
- VB可以获取当前操作windows的账号和密码吗?
然后把四个?的值传进去。
用ADO的Command对象来做。这样的好处是,SQL语句直观,易懂。
时间格式化
---------------------------------------------------------
用Format没问题哦,可以看看你的sql语句吗
要用双引号
此DTpsta.value已是'日期型'的数据了,两边#号就不需要。
如是上面是textbox控件传过来的值就要在两边加#号。
txtsql = "select * from table1 "& _
" where Chart_fre=" & CInt(TxtFre.Text) & _
" and Chart_operate='" & Trim(Cmbopa.Text) & "'"& _
" and Format(Chart_Stime,'hh:mm:ss') >='"& Format(DTpsta.Value,'hh:mm:ss')&"'"& _
" and Format(Chart_Etime,'hh:mm:ss') <='"& Format(DTPend.Value,'hh:mm:ss') &"'" 你可以用debug.print看看这样的SQL语句是不是符合你的要求