菜鸟求助各位大神VB对ACCESS查询统计问题
环境是VB6.0,Access2003。iddata.mdb有一张表表名为idlist,有如下几项:ID(身份号码:字符型),SKRQ(缴款日期:日期型),PZHM(凭证号码:字符型),JE(金额:货币型)。
VB上建了一个command1,一个text1,想实现text1内输入ID值,点击command1,弹出消息对话框输出数据库表内的ID等于text1的JE,我的代码为:
Private Sub Form_Load()
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DocumentsandSettings\Administrator\My Documents\iddata.mdb;Persist Security Info=False;"
End Sub
Private Sub Command1_Click()
If Text1.Text = "" Then
msg = MsgBox("请输入ID号", 0, "信息提示")
Text1.Text = ""
Text1.SetFocus
Exit Sub
Else
Dim rst As New ADODB.Recordset
Dim sql1 As String
result = Text1.Text
sql1 = "select JE from idlist where ID='" + result + "'"
con.Open
rst.Open sql1, con, 1, 1
If rst.BOF And rst.EOF Then
msg = MsgBox("该ID无交款记录")
Set rst = Nothing
con.Close
Exit Sub
Else
msg = MsgBox("该ID已缴" & count(sql1) & "笔款,合计交款金额为"& sum(sql1)&"元")
Set rst = Nothing
con.Close
End If
End If
End sub
运行的时候有两个问题:
一、无论输入什么数值,都是提示"该ID无交款记录",是不是IF循环里面的问题?该怎么改?
二、msg = MsgBox("该ID已缴" & count(sql1) & "笔款,合计交款金额为"& sum(sql1)&"元")是否正确,正确的代码该怎么写?
[解决办法]
Private Sub Command1_Click() Dim rst As New ADODB.Recordset Dim sql1 As String If Text1.Text = "" Then msg = MsgBox("请输入ID号", 0, "信息提示") Text1.Text = "" Text1.SetFocus Exit Sub End If result = Text1.Text sql1 = "SELECT Count(ID) As n, Sum(JE) As m FROM idlist WHERE ID='" & result & "'" con.Open rst.Open sql1, con, 1, 1 If rst!n = 0 Then msg = MsgBox("该ID无交款记录") rs.Close Set rst = Nothing con.Close Exit Sub End If msg = MsgBox("该ID已缴" & rst!n & "笔款,合计交款金额为" & rst!m & "元") rst.Close Set rst = Nothing con.CloseEnd Sub