我是VBA的新手,我正在尝试基于外部源创建一个ListObjects。问题是我想使用变量“Conn”来定义连接数组,但我得到一个错误'5'
这是我的代码。
Dim Conn as Variant
Conn = Array("OLEDB;" _
& "Provider=SQLOLEDB.1;" _
& "Password=***;" _
& "Persist Security Info=true;" _
& "User ID=a***;" _
& "Initial Catalog=BDADMIN_VTA;" _
& "Data Source=****;" _
& "Use Procedure for Prepare=1;" _
& "Auto Translate=True;" _
& "Packet Size=4096;" _
& "Workstation ID=HP-C***D;" _
& "Use Encryption for Data=False;" _
& "Tag with column collation when possible=False;")
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Conn, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = cadenaSql
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.displayName = nombre
.Refresh BackgroundQuery:=False
End With
¿你能解释一下我的错吗?我也试图用QueryTable.Add语句做类似的事情,它没有问题。