自定义一个可使用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在数据库中 日期为 日期/时间 数据类型
错误显示为“至少一个参数没有被指定值”
请各位大神帮忙解决!
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在数据库中 日期为 日期/时间 数据类型
错误显示为“至少一个参数没有被指定值”
请各位大神帮忙解决!
解决方案 »
- [求助]自定义路径转换为绝对路径
- 急求水晶報表9.0在某些型號的激光打印機打印時字體不正常的解決方法!
- 关于while(dr.read())的处理,求助
- com在vb 下成功,在asp调用下产生:缺少对象: '[undefined]',不明白,请高手帮忙
- 窗体问题。谢谢
- 关于线程调用的过程的参数,怎么传?
- 请问VB中如何在一个字符串中得到一个字母所在位置
- 那里有《用VB5进行高性能Web开发》(英文版:《Visual Basic 5 Web Development》)的源码?
- 怎样用Vb修改Access97中的数据库字段长度
- 打包成功后,安装出错!在线等待!!!:)
- vb MSHFLEXGRID中录入数据的速度问题
- 有C和Java基础,学习VB,入门看什么书?进阶看什么书?
txtSQL = txtSQL & " where 日期>'"& CDate(Date1) &"' and 日期 <'"& CDate(Date2) &"'"
Else
txtSQL = txtSQL & " And 日期>'"& CDate(Date1) &"' and 日期 <'"& CDate(Date2) &"'"
End If
其实还有个问题的不过另一个帖子“ACCESS中设计的日期字段类型为什么VB提示数据类型不匹配?”解决了,