Private Sub Command3_Click()
    On Error GoTo gpError
    Dim objCn As New Connection, objRs As New Recordset
    Dim strCn As String, strSQL As String
    Dim a As Integer
    '建立数据库连接 ado是数据源名
    strCn = "Provider=MSDASQL.1;Persist Security Info=False;" & "Data Source=ado"
    objCn.ConnectionString = strCn
    objCn.ConnectionTimeout = 30
    objCn.Open
        strSQL = "select sum(no) as total from 例子"
    Set objRs.ActiveConnection = objCn
    objRs.Open (strSQL)
    Text4.Text = objRs("total")    objCn.Close
    Set objRs = Nothing
    Set objCn = Nothing
   
gpError:
   
End Sub

解决方案 »

  1.   

    目的就是点击command3  text4就显示出库中列no的和
    我用的是VB+access
      

  2.   

    strSQL = "select sum([no]) as total from 例子"
      

  3.   

    楼主代码好像没有问题,如果有问题可能是no引起。同意楼上的[no]
      

  4.   

    楼主的代码会报错吗?
    看错误内容就应该知道是怎么回事。gpError:
       '加一句
       MsgBox Err.Description
      

  5.   

    我一点按钮 TEXT就显示0 根本不是和 我数据库里是有东西 不是空的
      

  6.   

    可以了 strSQL = "select sum([no]) as total from 例子"
    他说的对 zq972(*^_^*)
      

  7.   

    strSQL = "select sum([no]) as ftotal from 例子"
    if objrs.state=adstateopen then objrs.close
    objrs.open strsql,objcn,adopenkeyset,adlockreadonly
    if objrs.recordcount>0 then
        Text4.Text = objRs!ftotal
    else
        text4.text=""
    end if