UDF函数在Excel运行ACESQL查询中,JOIN两张表不起作用

UDF function in Excel running ACE SQL query, JOIN two tables does not work

我想弄清楚 Excel 中试图连接两个 table 的函数可能出了什么问题。我认为错误在 SQL 字符串中的某处。

该函数在没有连接的情况下运行良好,正确返回 table 到数组 - 单元格区域。即当 strSQL 只是 "SELECT * FROM [" & currAddress & "] "

当字符串包含连接时不起作用,即 strSQL = "SELECT * FROM [" & currAddress & "] " & _ "LEFT JOIN [" & currAddress2 & "] ON [Indeks].[" & currAddress & "] = [Indeks2].[" & currAddress2 & "];"

这是我的代码,谢谢你的帮助:

    Function SQL(dataRange As Range, dataRange2 As Range) As Variant
    Application.Volatile

    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim currAddress, currAddress2 As String
    Dim varHdr, varDat, contentOut As Variant
    Dim nc, nr, i, j As Long

    SQL = Null

    currAddress = ActiveSheet.Name & "$" & dataRange.Address(False, False)
    Debug.Print currAddress

    currAddress2 = ActiveSheet.Name & "$" & dataRange2.Address(False, False)
    Debug.Print currAddress2


    strFile = ThisWorkbook.FullName
    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=0"";"

    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    rs.CursorLocation = adUseClient ' required to return the number of rows correctly
    cn.Open strCon



        strSQL = "SELECT * FROM [" & currAddress & "] " & _
             "LEFT JOIN  [" & currAddress2 & "] ON [Indeks].[" & currAddress & "] = [Indeks2].[" & currAddress2 & "];"


   Debug.Print strSQL

    rs.Open strSQL, cn

    'Check if recordset is empty
    If rs.EOF Then
        MsgBox "Function does not return any values"
        SQL = ""
        Exit Function
    End If


    ' Process Column Headings
    nc = rs.Fields.Count
    ReDim varHdr(nc - 1, 0)
    For i = 0 To rs.Fields.Count - 1
        varHdr(i, 0) = rs.Fields(i).Name
    Next

    ' Get Rows from the Recordset
    nr = rs.RecordCount
    varDat = rs.GetRows

    ' Combing Header and Data and Transpose

    ReDim contentOut(0 To nr, 0 To nc - 1)
    For i = 0 To nc - 1
        contentOut(0, i) = varHdr(i, 0)
    Next




    For i = 1 To nr
        For j = 0 To nc - 1
           contentOut(i, j) = varDat(j, i - 1)



        Next
    Next

  ' Optional solution: Write Output Array to Sheet2
  '  With Sheet2
  '      .Cells.Clear
  '      .Range("A1").Resize(nr, nc) = contentOut
  '  End With


    'Figure out size of calling range which will receive the output array
    Dim nRow As Long: nRow = Application.Caller.Rows.Count
    Dim nCol As Long: nCol = Application.Caller.Columns.Count

    'Error if calling range too small
    If nRow < UBound(contentOut, 1) Or nCol < UBound(contentOut, 2) Then
        'Popup message
        'MsgBox "your range is too small."
        ' or return #VALUE! error
        SQL = "Too small range" 'CVErr(xlValue)
        ' or both or whatever else you want there to happen
        Exit Function
    End If

    'Initialise output array to match size of calling range
    Dim varOut As Variant
    ReDim varOut(1 To nRow, 1 To nCol)
    'And fill it with some background value
    Dim iRow As Long
    Dim iCol As Long
    For iRow = 1 To nRow

        For iCol = 1 To nCol
            varOut(iRow, iCol) = ""   ' or "funny bear", or whatever
        Next
    Next

    'Put content in output array and return
    For iRow = 0 To UBound(contentOut, 1)
        For iCol = 0 To UBound(contentOut, 2)
            varOut(iRow + 1, iCol + 1) = contentOut(iRow, iCol)
        Next
    Next



      SQL = varOut

    'Cleanup
    Erase contentOut
    Erase varHdr
    Erase varDat

    rs.Close
    Set rs = Nothing
    Set cn = Nothing


End Function

您似乎没有在联接中指定 fields/columns。 currAddresscurAddress2 看起来都像 table。 SQL 应该是这样的:

strSQL = "SELECT * FROM [Table1] " & _
         "LEFT JOIN  [Table2] ON [Table1].[Field] = [Table2].[Field];"

IndeksIndeks2 是您的字段名称吗?如果是这样,您需要将字段名称放在 table 名称之后:

strSQL = "SELECT * FROM [" & currAddress & "] " & _
         "LEFT JOIN  [" & currAddress2 & "] ON [" & currAddress & "].[Indeks] = [" & currAddress2 & "].[Indeks2];"

我相信'Indeks'是你在两个表中的公共字段,那么strSQL应该是这样的:

strSQL = "SELECT * FROM [" & 当前地址 & "] " & _ "LEFT JOIN [" & currAddress2 & "] ON [" & currAddress & "].[Indeks] = [" & currAddress2 & "].[Indeks]"