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
1、打开代码窗口,添加引用:Imports System.Data.SqlClient。
2、输入以下代码:
“Public conn1 As SqlConnection = New SqlConnection _
("server=192.168.1.79Initial Catalog= studentUser ID= panqePWD=shentai768@")”,vb就已经成功连接sql数据库了。
3、代码详解:声明关键字Public(因为是全局变量,所以用Public 来声明)。
4、连接参数。
5、如果SQL 数据库就在本机,则用以下代码连接:
("server=.Integrated Security=FalseInitial Catalog= studentUser ID= panqePWD=shentai768@")。
6:如果代码太长,影响可读性,可以用空格加"_"后,回车换行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)