如何用vba代码将access数据库连接求代码?

如何用vba代码将access数据库连接求代码?,第1张

Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点,是 Microsoft Office 的系统程序之一。用vba代码将access数据库连接求代码,代码如下:

Sub FYMXDL()

Dim XQID As Integer

Dim JZID As Integer

Dim FYID As Integer

Dim FBXZ As String '分包性质

Dim DW As String

Dim SARR(1 To 31) As Double

Dim rst As New ADODB.Recordset

mYpath = ThisWorkbook.Path &"\jzfydata.accdb"

Set cONn = CreateObject("ADODB.Connection")

cONn.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0Data Source=" &mYpath

cONn.ConnectionString = cONn.ConnectionString &"Jet OLEDB:Database "

cONn.Open

XQID = Cells(3, 2).Value

JZID = Cells(3, 5).Value

'清空改小区-建筑的费用明细

Sql = "delete  from fymxb where 小区ID=" &XQID &" AND 建筑ID = " &JZID

cONn.Execute Sql

Const kshh = 7

hh = kshh

Do While Cells(hh, 3).Value >0

FYID = Cells(hh, 3).Value

FBXZ = Cells(hh, 11).Text

For i = 1 To 31

SARR(i) = Round(Cells(hh, 13 + i - 1).Value, 2)

Next i

Sql = Sql &"," &SARR(i)

Next i

Sql = Sql &" )"

cONn.Execute Sql

hh = hh + 1

Loop

End Sub

下面这段程序是连接数据库,并将excel表格内容写入数据库。代码如下:

Sub FYMXDL()

Dim XQID As Integer

Dim JZID As Integer

Dim FYID As Integer

Dim FBXZ As String '分包性质

Dim DW As String

Dim SARR(1 To 31) As Double

Dim rst As New ADODB.Recordset

mYpath = ThisWorkbook.Path &"\jzfydata.accdb"

Set cONn = CreateObject("ADODB.Connection")

cONn.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0Data Source=" &mYpath

cONn.ConnectionString = cONn.ConnectionString &"Jet OLEDB:Database "

cONn.Open

XQID = Cells(3, 2).Value

JZID = Cells(3, 5).Value

'清空改小区-建筑的费用明细

Sql = "delete from fymxb where 小区ID=" &XQID &" AND 建筑ID = " &JZID

cONn.Execute Sql

Const kshh = 7

hh = kshh

Do While Cells(hh, 3).Value >0

FYID = Cells(hh, 3).Value

FBXZ = Cells(hh, 11).Text

For i = 1 To 31

SARR(i) = Round(Cells(hh, 13 + i - 1).Value, 2)

Next i

Sql = "INSERT INTO fymxb(小区ID,建筑ID,费用ID,分包性质,工作量,单价合计_中标,人工费_中标, 主材费_中标, 辅材费_中标, 机械费_中标, 管理费_中标, 利润_中标,规费_中标,税金_中标,合价_中标,单价合计_标准成本,人工费_标准成本,主材费_标准成本,辅材费_标准成本,机械费_标准成本,管理费_标准成本,利润_标准成本,规费_标准成本,税金_标准成本,合价_标准成本,单价合计_实际成本,人工费_实际成本,主材费_实际成本,辅材费_实际成本,机械费_实际成本,管理费_实际成本,利润_实际成本,规费_实际成本,税金_实际成本,合价_实际成本) VALUES (" &XQID &", " &JZID &", " &FYID &", '" &FBXZ &"'"

For i = 1 To 31

Sql = Sql &"," &SARR(i)

Next i

Sql = Sql &" )"

cONn.Execute Sql

hh = hh + 1

Loop

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6848624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-29
下一篇 2023-03-29

发表评论

登录后才能评论

评论列表(0条)

保存