簡體   English   中英

查詢表時,對記錄集的排序不起作用

[英]Order by on a recordset does not work when querying a table

我正在使用下面的代碼從 Excel 表中獲取過濾和排序的數據。 結果數據經過過濾,但未排序。 如果我遺漏任何東西,請告訴我:

提供給 RangeToRecordset 公式的變量如下。

ExcelFile = ThisWorkbook.FullName
SQL = select * from [Inputs_Source Table$B3:AL2232] WHERE [Worksheet] = 'Global Inputs' AND [Section] = 'GA' ORDER BY 'Worksheet order','SubSection order','Section order'

 Public Function RangeToRecordset(ExcelFile As String, strSQL As String, Optional HasHeader = "Yes") As Recordset
' need to add the following reference:
' Microsoft ActiveX Data object...
' https://online-training.ro/content/vba/range-to-recordset/

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")
    
    ExcelFile = Replace(ExcelFile, "'", "''") 'DDU [2022.03.04]: ADD THIS TO HANDLE ' IN THE NAME
    
    objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source='" & ExcelFile & "';" & _
            "Extended Properties=""Excel 8.0;HDR=" & HasHeader & ";"";"
    
    objRecordset.Open strSQL, _
        objConnection, adOpenStatic, adLockOptimistic, adCmdText
    Set RangeToRecordset = objRecordset
End Function

您正在按一些固定字符串排序。 嘗試使用字段名稱:

SQL = select * from [Inputs_Source Table$B3:AL2232] WHERE [Worksheet] = 'Global Inputs' AND [Section] = 'GA' ORDER BY [Worksheet order],[SubSection order],[Section order]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM