VB中sql语句数据库日期段求和请求案列

Dim cn As New ADODB.Connection '声明数据库连接对象Dim rs As New ADODB.RecordsetPrivate Sub Form_Load()'声明记录集对象cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=true;Data Source=" & App.Path & "\sjk\wysjk.mdb;Jet OLEDB:Database Password=2017" '自动识别数据库路径 DTP3.Value = Date - 30 DTP4.Value = DateEnd SubPrivate Sub Command7_Click()Dim mysql As Stringmysql = "select sum (合计金额) As 结束日期 from 物业费 where (DTP1.Value,结束日期)>=0 and (DTP2.Value,结束日期)>=0"rs.Open mysql, cn, 1, 3Label3.Caption = rs.Fields(0) rs.CloseSet rs = Nothingcn.CloseSet cn = Nothing End Sub

Dim cn As New ADODB.Connection '声明数据库连接对象
Dim rs As New ADODB.Recordset'声明记录集对象
Private Sub Form_Load()
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=true;Data Source=" & App.Path & "\sjk\wysjk.mdb;Jet OLEDB:Database Password=2017" '自动识别数据库路径 
DTP3.Value = Date - 30 
DTP4.Value = Date
End Sub
Private Sub Command7_Click()
Dim mysql As Stringmysql = "select sum (合计金额) As ç»“束日期 from ç‰©ä¸šè´¹ where ç»“束日期 between '"&DTP1.Value&"' and '"&DTP2.Value&"'"
rs.Open mysql, cn, 1, 3
Label3.Caption = rs.Fields(0) 
rs.CloseSet rs = Nothing
cn.CloseSet cn = Nothing 
End Sub追问

Private Sub Command6_Click()

Dim mysql As String

If DTP1.Value <= DTP2.Value Then

mysql = "select sum (合计金额) As 结束日期 from 物业费 where 结束日期 between '" & DTP1.Value & "' and '" & DTP2.Value & "'"

rs.Open mysql, cn, 1, 3

Label3.Caption = rs.Fields(0)

rs.Close

Set rs = Nothing

cn.Close

Set cn = Nothing

End If

End Sub

点第二次出现错误

当两个时间相同出现null

追答

rs和cn对象不能nothing

温馨提示:答案为网友推荐,仅供参考
相似回答