我正在使用下面的代码将银行端数据库链接到前端。无需在后端数据库上输入密码即可正常工作。如何将相同的代码与受密码保护的后端文件一起使用。注意:以下代码是从[Stackoverflow问题] [1]
获得的 [1]: https://stackoverflow.com/questions/3315306/how-can-a-relative-path-specify-a-linked-table-in-access-2007
Private Sub Form_Load()
Dim strOldConnect As String
Dim strNewConnect As String
Dim intSlashLoc As Integer
Dim intEqualLoc As Integer
Dim strConnect As String
Dim strFile As String
Dim strCurrentPath As String
strCurrentPath = CurrentProject.path
Dim tblDef As TableDef
Dim tblPrp As Property
For Each tblDef In CurrentDb.TableDefs
Debug.Print tblDef.Name
If tblDef.Connect & "." <> "." Then
strOldConnect = tblDef.Connect
intEqualLoc = InStr(1, strOldConnect, "=", vbTextCompare)
strConnect = Left(strOldConnect, intEqualLoc)
intSlashLoc = InStrRev(strOldConnect, "\", -1, vbTextCompare)
strFile = Right(strOldConnect, Len(strOldConnect) - intSlashLoc)
strNewConnect = strConnect & strCurrentPath & "\" & strFile
tblDef.Connect = strNewConnect
tblDef.RefreshLink
End If
Next tblDef
End Sub
答案 0 :(得分:0)
Access Microsoft ACE OLEDB 12.0
的整个连接字符串为:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; Jet OLEDB:Database Password=MyDbPassword;
请参阅此链接以获取参考https://www.connectionstrings.com/access/
在您的情况下,这将达到目的:
tblDef.Connect = "PWD=" & MyPassword & ";DATABASE=" & YourDatabasePath
答案 1 :(得分:0)
我自己找到了一种解决方法,希望与大家分享。
Public Function DBconnect()
Dim Password As String
Dim FileName As String
Dim CurrentConnection As String
Dim AccessConnect As String
Dim NewConnection As String
Dim CurrentPath As String
Dim CurrentLocationEnd As Integer
AccessConnect = "MS Access;PWD=password;DATABASE="
Password = "password"
CurrentPath = CurrentProject.Path
Dim tblDef As TableDef
Dim tblPrp As Property
For Each tblDef In CurrentDb.TableDefs
Debug.Print tblDef.Name
If tblDef.Connect & "." <> "." Then
CurrentConnection = tblDef.Connect
CurrentLocationEnd = InStrRev(CurrentConnection, "\", -1, vbTextCompare)
FileName = Right(CurrentConnection, Len(CurrentConnection) - CurrentLocationEnd)
NewConnection = AccessConnect & CurrentPath & "\" & FileName
tblDef.Connect = NewConnection
tblDef.RefreshLink
End If
Next tblDef
End Function