|
本帖最后由 爱疯 于 2014-3-15 23:12 编辑
请问如何用ADODB链接带密码的ACCESS文件啊?
下面是我现有的代码,但我不知道我的ACCESS文件加密后,怎么修改我的代码啊,有哪位前辈可以帮忙看看嘛?
Private Sub CommandButton1_Click()
Dim Serial$, Rst As New ADODB.Recordset, Strsql$, Ctl As Control, i%
On Error GoTo ErrLine
If Len(TextBox1) = 0 Then
MsgBox "Pls input serial number", 1 + 16, "alert"
TextBox1.SetFocus
Exit Sub
End If
If Condb("TargetDatabase.accdb") = False Then GoTo Line1
Strsql = "select distinct ACCTNO,CUSTNM,Status from [Table] where SerialNum='" & _
TextBox1 & "' order by ACCTNO"
Set Rst = cnn.Execute(Strsql)
... ...
Public Function Condb(dbName$) As Boolean
On Error GoTo Line1
Condb = True
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & dbName
cnn.CursorLocation = adUseClient
cnn.ConnectionTimeout = 5
cnn.Open
Exit Function
Line1:
Condb = False
MsgBox "database connection error:" & Chr(10) & Err.Description
End Function
- Private Sub CommandButton1_Click()
- Dim Serial$, Rst As New ADODB.Recordset, Strsql$, Ctl As Control, i%
- On Error GoTo ErrLine
- If Len(TextBox1) = 0 Then
- MsgBox "Pls input serial number", 1 + 16, "alert"
- TextBox1.SetFocus
- Exit Sub
- End If
- If Condb("TargetDatabase.accdb") = False Then GoTo Line1
- Strsql = "select distinct ACCTNO,CUSTNM,Status from [Table] where SerialNum='" & _
- TextBox1 & "' order by ACCTNO"
- Set Rst = cnn.Execute(Strsql)
- ... ...
- Public Function Condb(dbName$, Optional pwd$ = "") As Boolean
- On Error GoTo Line1
- Condb = True
- Set cnn = New ADODB.Connection
- cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
- "Data Source=" & ThisWorkbook.Path & "" & dbName & _
- ";Jet OLEDB:Database Password=" & pwd & ";"
- cnn.CursorLocation = adUseClient
- cnn.ConnectionTimeout = 5
- cnn.Open
- Exit Function
- Line1:
- Condb = False
- MsgBox "database connection error:" & Chr(10) & Err.Description
- End Function
复制代码改了函数声明,这样在调用的时候,第2个参数是可选的,有密码的就输入密码,没有密码的话第2个参数就不用写。
|
|