各位大侠,小弟初学VB,我现在能用VB对ACCESS的数据进行增删改以及查询
但是当我想对数据库中的数据进行例如:求和,求平均值之类的操作时就比较困难。我的数据库如下(ACCESS)
ONE    TWO
1      1
1      2
1      3
1      4
1      5
我现在定义了 Public adoRS As New ADODB.Recordset  
Public cn As ADODB.Connection
在我的窗体里,有一个按钮和LABEL控件,我想点击按钮在LABEL控件上显示TWO的平均值
请问那位大侠能告诉我
小弟在线等各位大侠的解答,调试成功马上结帖。
先谢谢了。

解决方案 »

  1.   

    我知道SQL语句是:
    select AVG(two) from 表名 where one='1';
    可是在程序中怎么用就不知道了。
      

  2.   

    private sub command1_click()
        dim conn as new adodb.connection
        dim rs as new adodb.recordset
        dim strsql as string
        
        With conn
            If .State = adStateOpen Then .Close
            .ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb;Mode=ReadWrite;Persist Security Info=False"
            .Open
        End With
        
        strsql="select AVG(two) as avg_two from 表名 where one='1'"
        if rs.state=adstateopen then rs.close
        rs.open strsql,conn,adopenkeyset,adlockreadonly
        if rs.recordcount>0 then
            label1.caption=rs!avg_two 
            label1.refresh
        end if
        rs.close
        conn.close
        set rs=nothing
        set conn=nothing
    end sub