VBA-SQL查询数据库表到Excel,表名带有特殊字符

时间:2019-03-25 13:54:10

标签: sql sql-server excel vba

我在此处发布了有关将SQL查询到Excel的先前问题:

VBA - Import All Rows from Table in SQL to Excel

这是我后来解决的一个监督问题。但是,现在,当我尝试导入其他表时遇到一个新问题。

当我第一次整理宏时,它是针对一个带有下划线的表,例如: my_table_1_query

现在,我尝试使用与表名完全相同的代码,例如: my-table_query_1 ,并且在此行出现错误:

.Refresh BackgroundQuery:=False

错误消息:

  

'-'附近的语法不正确

这在函数 ImportSQLtoQueryTable

代码如下:

功能

ImportSQLtoQueryTable

Function ImportSQLtoQueryTable(ByVal conString As String, ByVal query As String, ByVal target As Range) As Integer

    Dim ws As Worksheet
    Set ws = target.Worksheet

    Dim address As String
    address = target.Cells(1, 1).address

    'Procedure recreates ListObject or QueryTable
    'For Excel 2007 or higher
    If Not target.ListObject Is Nothing Then

        target.ListObject.Delete

    End If

    'For 2007 or higher
    If Application.Version >= "12.0" Then

        With ws.ListObjects.Add(SourceType:=0, Source:=Array("OLEDB;" & conString), Destination:=Range(address))

            With .QueryTable

                .CommandType = xlCmdSql
                .CommandText = StringToArray(query)
                .BackgroundQuery = True
                .SavePassword = True
                .Refresh BackgroundQuery:=False
                .ListObject.Name = "DB KW Component Table"

            End With

        End With

    End If

    ImportSQLtoQueryTable = 0

End Function

StringToArray

Function StringToArray(Str As String) As Variant

    Const StrLen = 127
    Dim NumElems As Integer
    Dim Temp() As String
    Dim I As Integer

    NumElems = (Len(Str) / StrLen) + 1
    ReDim Temp(1 To NumElems) As String

    For I = 1 To NumElems

       Temp(I) = Mid(Str, ((I - 1) * StrLen) + 1, StrLen)

    Next I

    StringToArray = Temp

End Function

GetTestConnectionString

Function GetTestConnectionString() As String

    GetTestConnectionString = OleDbConnectionString( _
        "Server Location", _
        "Connection DB", _
        "Username", _
        "Password")

End Function

OleDbConnectionString

Function OleDbConnectionString(ByVal Server As String, ByVal Database As String, ByVal Username As String, ByVal Password As String) As String

    If Username = "" Then

        MsgBox "User name for DB login is blank. Unable to Proceed"

    Else
        OleDbConnectionString = _
        "Provider=SQLOLEDB.1;" & _
        "Data Source=" & Server & "; " & _
        "Initial Catalog=" & Database & "; " & _
        "User ID=" & Username & "; " & _
        "Password=" & Password & ";"
    End If

End Function

Sub:

TestImportUsingQueryTable

Sub TestImportUsingQueryTable()

    Dim conString As String, query As String
    Dim DestSh As Worksheet
    Dim tmpltWkbk As Workbook
    Dim target As Range

    'Set workbook to be used
    Set tmpltWkbk = Workbooks("New DB.xlsm")

    'Need to add check if sheet already exists
    'If sheet already exists then just refresh table

    'Add a new sheet called "DB Table"
    Set DestSh = tmpltWkbk.Worksheets.Add
    DestSh.Name = "DB Table"

    With DestSh

        .UsedRange.Clear
        Set target = .Cells(2, 2)

    End With

    'Get connection string
    conString = GetTestConnectionString()

    'Set Query to table
    query = "SELECT * FROM SAT_Keyword_DB_X7.dbo.kw_link-tbl_keyword_components"

    Select Case ImportSQLtoQueryTable(conString, query, target)

        Case Else

    End Select

End Sub

1 个答案:

答案 0 :(得分:1)

将对象名称括在方括号中以告知SQL Server它们中的所有内容都应视为文本,因此在您的情况下,$Git log 应该作为my-table_query_1传递