我的数据库访问总是在不同的路径,但在与我的woorkbook相同的文件夹中。
Private Sub CommandButton14_Click()
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Set cn = CreateObject("ADODB.Connection")
'here I want to use current directory as path for my mdb
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\FicheMacro\PGDB.mdb"
strSql = "SELECT Count(*) FROM AQ_DGE_MOD;"
cn.Open strConnection
Set rs = cn.Execute(strSql)
MsgBox rs.Fields(0) & " rows in MyTable"
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
答案 0 :(得分:2)
正如评论中所说,我对联系并不熟悉。但是类似于以下内容可能会有效。
'insert this two lines in your code
Dim folderPath As String
folderPath = Application.ActiveWorkbook.Path
'change strConnection to the following
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & folderPath & "\PGDB.mdb"