2

我正在尝试将数据从 SQL 表中提取到 excel 中。我已经使用数据源工具记录了一个宏。但是,我要提取的数据量通常会使 Excel 崩溃。有没有办法在我的 vba 脚本中添加一个变量来限制从 sql 表中提取的数据?本质上是在 sql 中的 select 语句中添加 where 子句。

谢谢!

-肖恩

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=10.22.30.215;Use Procedure for Prepare=1;Aut" _
    , _
    "o Translate=True;Packet Size=4096;Workstation ID="FakeName";Use Encryption for Data=False;Tag with column collation when possible=Fa" _
    , "lse;Initial Catalog=FakeCatelog"), Destination:=Range("$A$1")). _
    QueryTable
    .CommandType = xlCmdTable
    .CommandText = Array( _
    """FakeName""")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .SourceConnectionFile = _
    "C:\UFakeFilePathodc"
    .ListObject.DisplayName = "FakeName"
    .Refresh BackgroundQuery:=False
End With
End Sub`
4

2 回答 2

3

尝试连接到数据库并进行查询,而不是尝试将整个数据库拉入工作表。这应该可以帮助您开始:
确保添加“Microsoft ActiveX Data Objects 6.0 Library”参考或运行此行一次:
ActiveWorkbook.VBProject.References.AddFromGuid "{B691E011-1797-432E-907A-4D8C69339129}", 6, 0

Sub QueryDB()
    Dim dbName As ADODB.Connection
    Dim dbResults As ADODB.Recordset
    Set dbName = openDBConn("YOURDATABASE", "YourTable")
    Set dbResults = dbName.Execute("SELECT * FROM YOURDATABASE")
    While Not dbResults.EOF
        'Do Something'
        dbResults.MoveNext
    Wend
End Sub

Function openDBConn(dataSource As String, table As String) As ADODB.Connection
    Dim newDBConn As ADODB.Connection
    Set newDBConn = New ADODB.Connection
    newDBConn.CommandTimeout = 60
    Dim strConn As String
    strConn = "PROVIDER=SQLOLEDB;DATA SOURCE=" & dataSource & ";INITIAL CATALOG=" & table & ";INTEGRATED SECURITY=SSPI"
    newDBConn.Open strConn
    Set openDBConn = newDBConn
End Function
于 2012-11-16T23:19:43.993 回答
1

一旦您对代码感到满意,可能值得将其切换到late binding. Early binding很好,因为您在开发应用程序时获得了完整的智能感知,但我发现后期绑定随着时间的推移而变得不那么麻烦并且应用程序升级到新版本。

另外,由于我经常使用连接字符串,因此最好将其贴在模块的顶部...节省了将来挖掘此硬代码的时间:

(ps 这只是 Kevin 的代码,有一些更改;不一定是改进,而只是替代方案)

Global Const strConn As String = "PROVIDER=SQLOLEDB;DATA SOURCE=" & dataSource & ";INITIAL CATALOG=" & table & ";INTEGRATED SECURITY=SSPI"
Sub QueryDB()
       Dim dbName As Object
       Dim dbResults As Object
       Set dbName = CreateObject("ADODB.Connection")
       dbName = openDBConn("YOURDATABASE", "YourTable")
       Set dbResults = CreateObject("ADODB.Recordset")
       dbResults = dbName.Execute("SELECT * FROM YOURDATABASE")
       While Not dbResults.EOF
           'Do Something'
           dbResults.MoveNext
       Wend
End Sub

Function openDBConn(dataSource As String, table As String) As ADODB.Connection
       Dim newDBConn As Object
       Set newDBConn = CreateObject("ADODB.Connection")
       newDBConn.CommandTimeout = 60

       newDBConn.Open strConn
       Set openDBConn = newDBConn
End Function
于 2012-11-19T10:56:49.830 回答