所以我有一个宏设置,它将连接到Access数据库并将SQL查询输出到指定的工作表。宏本身运行正常,但是当我尝试使用它后一天它将无法正常工作。它删除现有的连接字符串,获取新的ADODB连接字符串,并将其粘贴回代码后,它确实有效,但我不想每天都去重做它。这是我目前的代码(为了便于阅读,我编辑了它):
Sub AccessDatabaseCopy()
Dim AccessConn As ADODB.Connection
Dim AccessData As ADODB.Recordset
Dim AccessField As ADODB.Field
Set AccessConn = New ADODB.Connection
Set AccessData = New ADODB.Recordset
AccessConn.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;...."
AccessConn.Open
On Error GoTo CloseConnection
With AccessData
.ActiveConnection = AccessConn
.Source = "SELECT DISTINCTROW dbo_GEN_STW_Name1.master_stw, ..."
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Worksheets("Access Table").Range("A1").Select
For Each AccessField In AccessData.Fields
ActiveCell.Value = AccessField.Name
ActiveCell.Offset(0, 1).Select
Next AccessField
Range("A1").Select
Range("A2").CopyFromRecordset AccessData
Range("A1").CurrentRegion.EntireColumn.AutoFit
CloseRecordSet:
AccessData.Close
CloseConnection:
AccessConn.Close
End Sub
编辑:对不起,这里是完整的连接字符串:
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=S:\ISS\CELSON\Carestream Database.accdb;Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False"