|
把数据库加了个密码:123 但代码不知道怎么加
代码如下:
Sub 查询()
On Error Resume Next '忽略错误值
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Dim strCn As String, strSQL As String
Dim mdbFile As String
mdbFile = ThisWorkbook.Path & "\基础数据库.accdb"
strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mdbFile
cn.Open strCn
Sheet2.Range("A2:ZA1000").ClearContents
Sql = "Select * from " & Sheet1.Cells(8, "b") & ""
rs.Open Sql, cn, 1, 3
Do While Not rs.EOF
With Sheet2
hs = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(hs, 1) = rs("111")
.Cells(hs, 2) = rs("222")
.Cells(hs, 3) = rs("333")
End With
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
|
|