新手求指导VB

white772156443 2012-03-10 05:19:31
自定义一个可使用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

在数据库中 日期为 日期/时间 数据类型
错误显示为“至少一个参数没有被指定值”


请各位大神帮忙解决!
...全文
33 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
white772156443 2012-03-11
  • 打赏
  • 举报
回复
谢谢
其实还有个问题的不过另一个帖子“ACCESS中设计的日期字段类型为什么VB提示数据类型不匹配?”解决了,
饮水需思源 2012-03-10
  • 打赏
  • 举报
回复
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

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧