vb语言怎么实现access数据表中行与行间的运算?
2个回答
展开全部
vb支持SQL查询语言,先添加部件 microsoft ado data control 6.0(oledb) ,然后再生成连接字符,也可以直接使用
Private strConn As String '连接字符串
Private StrSql As String, StrSql_Select As String, Strsql_Insert As String, Strsql_Update As String '定义SQL语句
Private conn As New ADODB.Connection '定义一个新的connection实例conn
Private rs, As New ADODB.Recordset '定义一个新的记录集Recordset实例rs
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Database_Path & "xxx.mdb;Persist Security Info=False;Jet OLEDB:DataBase Password=密码"'这个是ACCESS的连接字符串,Database_Path为数据库目录,XXX为数据库名称,记得不要忘记"\",保证文件路径存在,
Set conn = New ADODB.Connection
conn.ConnectionString = strConn '连接数据库
conn.Open
dim maolirun ,chengben ,jinglirun as single
'中间是SQL语言,假如你数据库的表明是利润,假如需要合计那一列计算,就这样
StrSql_Select = "select 合 from 利润 where 指='一、毛利润'"
Set rs = conn.Execute(StrSql_Select)
maolirun= val(rs(0))'也可以写成maolirun= val(rs("合"))
StrSql_Select = "select 合 from 利润 where 指='二、成本'"
Set rs = conn.Execute(StrSql_Select)
chengben= val(rs(0))'也可以写成maolirun= val(rs("合"))
,jinglirun=maolirun-chengben
coon.close
Private strConn As String '连接字符串
Private StrSql As String, StrSql_Select As String, Strsql_Insert As String, Strsql_Update As String '定义SQL语句
Private conn As New ADODB.Connection '定义一个新的connection实例conn
Private rs, As New ADODB.Recordset '定义一个新的记录集Recordset实例rs
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Database_Path & "xxx.mdb;Persist Security Info=False;Jet OLEDB:DataBase Password=密码"'这个是ACCESS的连接字符串,Database_Path为数据库目录,XXX为数据库名称,记得不要忘记"\",保证文件路径存在,
Set conn = New ADODB.Connection
conn.ConnectionString = strConn '连接数据库
conn.Open
dim maolirun ,chengben ,jinglirun as single
'中间是SQL语言,假如你数据库的表明是利润,假如需要合计那一列计算,就这样
StrSql_Select = "select 合 from 利润 where 指='一、毛利润'"
Set rs = conn.Execute(StrSql_Select)
maolirun= val(rs(0))'也可以写成maolirun= val(rs("合"))
StrSql_Select = "select 合 from 利润 where 指='二、成本'"
Set rs = conn.Execute(StrSql_Select)
chengben= val(rs(0))'也可以写成maolirun= val(rs("合"))
,jinglirun=maolirun-chengben
coon.close
展开全部
先把每行的各字段值读出来,再对读取的值做数学运算。
更多追问追答
追问
用数组读吗 大概的代码是咋样的呢
追答
Dim cnn As New ADODB.Connection
Dim redset As New ADODB.Recordset
'连接数据库,并打开数据库文件MDB
Private Sub OpenAccessdb(dbStr As String)
On Error GoTo ErrorProc
Set cnn = Nothing
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & dbStr & ";Persist Security Info=False;Jet OLEDB:Database Password=audiodj"
cnn.Open
Exit Sub
ErrorProc:
If Err.Description "" Then
MsgBox Err.Description, vbCritical, "错误信息"
End If
End Sub
'处理数据表MDB
Private Sub OperateAccessDb(sqlStr As String)
On Error GoTo ErrorProc
Set redset = Nothing
Set redset.ActiveConnection = cnn
redset.CursorType = adOpenStatic
redset.LockType = adLockPessimistic
redset.Open sqlStr
Exit Sub
ErrorProc:
If Err.Description "" Then
MsgBox Err.Description, vbCritical, "错误"
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询