如何用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

方法如下:

假设数据库均为Access文件:

ThisWorkbook.Path &"\数据库1.accdb"

ThisWorkbook.Path &"\数据库2.accdb"

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0Data Source=" &ThisWorkbook.Path &"\数据库1.accdb"

对于非连接数据库数据库2,数据表前面需要加上数据库全名:

[MS Accesspwd=密码Database=" &ThisWorkbook.Path &"\数据库2.accdb].数据表名

Public Sub SynTableConfig()

Dim cnn As New ADODB.Connection, sh As Worksheet

Dim rs As New ADODB.Recordset

Dim cnnStr As String, SQL As String

'建立与SQL Server数据库服务器的连接

cnnStr = "Provider=SQLOLEDBInitial Catalog=" &myDataBase &"User ID=" &myname &"Password=" &mypassword &"Data Source=" &serverip

cnn.ConnectionString = cnnStr

cnn.Open

SQL = "select * from sys_table where id='123' order by 表名称"

Set rs = cnn.Execute(SQL)

While Not rs.EOF

.............

rs.MoveNext

Wend

rs.Close

Set rs = Nothing

cnn.Close

Set cnn = Nothing

End Sub


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存