VB如何将数据库里多条数字记录相加

2025-01-03 13:29:23
推荐回答(5个)
回答1:

完整的程序代码如下:

Rem 先在菜单“工程”中“引用” MicroSoft ActiveX Data Object x.x Library(用最新版本)
Rem 加个文本框Text1,用于输入姓名,把“表1”换成你的access表名
Rem 字段2是姓名所在的字段名称,字段3是数值所在的字段名称

Private Sub Command1_Click()
Dim sConnString As String, SqlQuery As String, who As String
Dim adoConn As ADODB.Connection
Dim adoRS As ADODB.Recordset

sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\db1.mdb" '换成你的数据库文件名称
who = Trim(Text1.Text)

Set adoConn = New ADODB.Connection
adoConn.Open sConnString
Set adoRS = New ADODB.Recordset
SqlQuery = "select sum(字段3) as 字段3的和 from 表1 where 字段2='" & who & "'"
Set adoRS = adoConn.Execute(SqlQuery)
Print adoRS("字段3的和").Value
End Sub

回答2:

用一ADODC连接数据库,
如果只要张三的和则:
adodc1.RecordSource="select 字段1,sum(字段2) as 字段2的和 from tablename where 字段1='张三'"
如果需要将所有人按姓名进行求和则:
adodc1.RecordSource="select 字段1,sum(字段2) as 字段2的和 from tablename group by 字段1"

回答3:

select 字段1,sum(字段2) as 字段2 from tablename where 字段1='张三'
group by 字段1

回答4:

SELECT Sum(表名.字段2) AS 字段2之总计 FROM 表名 WHERE (((表名.字段1)="张三"));
字段2之总计,故名思意,是“显示总和”的标题啊。

回答5:

select 字段1,count(字段2) as 字段2 from tablename where 字段1='张三'
group by 字段1