新手求指导VB
自定义一个可使用SQL语句的函数
Public Function ExecuteSQL(ByVal SQL As String, Msgs As String) As ADODB.Recordset '自定义函数
Dim cnn As ADODB.Connection '创建一个新的数据库连接
Dim rst As ADODB.Recordset '创建一个新的数据库记录,
Dim sCmd() As String
sCmd = Split(SQL)
Set cnn = New ADODB.Connection '创建数据库连接
cnn.Open ConnectStr '打开
If InStr("INSERT,DELETE,UPDATE", UCase$(sCmd(0))) Then
cnn.Execute SQL '执行命令
Msgs = sCmd(0) & " 查询成功"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic '
Set ExecuteSQL = rst
Msgs = "查询到" & rst.RecordCount & " 条记录 " '查询结果的数目
End If
On Error GoTo Err1
Err_Exit:
Set rst = Nothing '关闭
Set cnn = Nothing
Exit Function
Err1:
Msgs = "查询错误: " & Err.Description
Resume Err_Exit
End Function
然后
Dim mrc As ADODB.Recordset
Dim MsgText As String
Dim txtSQL, Date1, Date2 As String
Dim sum As Currency
If Combo2.Text <> "" Then
If Combo3.Text <> "" Then '月份大于0,则开始为本月1日
Date1 = Trim(Combo2.Text) + "-" + Trim(Combo3.Text) + "-01"
If Combo3.Text = 12 Then '月份为12,则结束为下一年度1月1日
Date2 = Trim(Combo2.Text + 1) + "-1-1"
Else
Date2 = Trim(Combo2.Text) + "-" + Trim(Combo3.Text + 1) + "-1"
End If
Else
Date1 = Trim(Combo2.Text) + "-1-1"
Date2 = Trim(Combo2.Text) + "-12-31"
End If
If txtSQL = "select sum(金额) from 日常收支表" Then
txtSQL = txtSQL & " where 日期 > CDate(Date1) and 日期 < CDate(Date2)"
Else
txtSQL = txtSQL & " And 日期 > CDate(Date1) and 日期 < CDate(Date2) "
End If
End If
Set mrc = ExecuteSQL(txtSQL, MsgText)
With MSFlexGrid1
.Cols = 1
.ColWidth(0) = 4300
.TextMatrix(0, 0) = "在该时间段该人员在该收支类型方面经手的财产金额为"
.TextMatrix(1, 0) = mrc.Fields(0) & "元"
End With
在数据库中 日期为 日期/时间 数据类型
错误显示为“至少一个参数没有被指定值”
请各位大神帮忙解决!
[解决办法]
If txtSQL = "select sum(金额) from 日常收支表" Then
txtSQL = txtSQL & " where 日期>'"& CDate(Date1) &"' and 日期 <'"& CDate(Date2) &"'"
Else
txtSQL = txtSQL & " And 日期>'"& CDate(Date1) &"' and 日期 <'"& CDate(Date2) &"'"
End If