【问题标题】:Sql string with OLEDB gives error 1004 in VBA带有 OLEDB 的 Sql 字符串在 VBA 中给出错误 1004
【发布时间】:2023-03-29 04:43:01
【问题描述】:

我想通过 VBA 宏将文本文件导入到 excel 过滤器中。当我在 sql 字符串中使用 LIKE 运算符时,我收到错误 1004。 我试过*% 作为通配符和ALike 而不是Like 但没有区别。

test_7.txt

946737295   9CE78280    FF  1   5   FF  FF  FF  FF  FF
946737295   9CE78280    C0  FF  0   0   0   0   FF  FF
946737295   9CE68082    C0  4   0   FF  FF  FF  FF  FF

宏是:

Sub import_txt()

Dim input_path As String
input_path = "C:\test_7.txt"

Dim strSql As Variant
strSql = "SELECT * FROM [test_7]" & _
   " WHERE Column2 Like '*E7*' AND" & _
   " Column4='FF'"
   
    ActiveWorkbook.Queries.Add Name:="test_7", Formula:= _
    "let" & "    Origine = Csv.Document(File.Contents(""" & input_path & """),[Delimiter=""#(tab)"", Columns=10, Encoding=1252, QuoteStyle=QuoteStyle.None])," & "    #""Modifica tipo"" = Table.TransformColumnTypes(Origine,{{""Column1"", type text}, {""Column2"", type text}, {""Colum" & _
    "n3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}})" & "in" & "    #""Modifica tipo"""
    
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
    "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=test_7;Extended Properties=""""" _
    , Destination:=Range("$A$1")).QueryTable
    .CommandType = xlCmdSql
    .CommandText = Array(strSql) 'Array("SELECT * FROM [test_7]")
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .ListObject.DisplayName = "test_7"
    .Refresh BackgroundQuery:=False
End With
End Sub

【问题讨论】:

    标签: sql excel vba oledb wildcard


    【解决方案1】:

    SQL 语句不适用于 Power Query 提供程序。但是您可以通过与您的查询关联的公式完全实现您想要的。

    看这个例子:

    Sub readTxt()
    Dim Wb As Workbook
    Dim Ws As Worksheet
    Dim Conn As WorkbookConnection
    Dim mFormula As String
    Dim query As WorkbookQuery
    
    Set Wb = ActiveWorkbook
    Set Ws = Wb.ActiveSheet
    
    mFormula = "let " & _
        "Source = Csv.Document(File.Contents(""C:\Users\Loïc\Desktop\test\test.txt""),[Delimiter="";"", Encoding=65001, QuoteStyle=QuoteStyle.Csv])," & _
        "#""Step1"" = Table.SelectRows(Source, each Text.Contains([Column2], ""E7"") and [Column3] = ""FF"")" & _
        "in #""Step1"""
        
    Set query = Wb.Queries.Add("Test text", mFormula)
    
    With Ws.ListObjects.Add(SourceType:=0, Source:= _
         "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & "Test text" & ";Extended Properties=""""", Destination:=Ws.Range("A1"), XlListObjectHasHeaders:=xlYes).QueryTable
         .CommandType = xlCmdSql
         .AdjustColumnWidth = False
         .ListObject.Name = "test"
         .CommandText = "SELECT * FROM [" & "Test text" & "]"
         .Refresh BackgroundQuery:=False
     End With
          
    End Sub
    

    您可能希望添加一些错误处理机制(以防文件已打开等),并在不再需要时删除与文本文件的连接。

    您将找到 Microsoft 的 Power Query M 公式语言 here 的文档。

    帮助您使用 VBA 构建 M 公式的最后一个技巧:使用宏记录器!

    【讨论】:

    • 谢谢 Lo,所以你看不到让它在 excel 中工作的方法,对吧?我尝试过使用不同的提供商,但我丢失了数据,请参阅我的其他帖子。
    • @RuggeroBini 您只能访问 Excel 2007?
    • @RuggeroBini 好吧,我用一个例子编辑了我的答案。尝试在 mFormula 中添加一个步骤以根据需要进行过滤并告诉我这是否有效。
    • @RuggeroBini 请仔细阅读。 SQL 语句应该保持我写的样子。这是您必须更改以实现目标的 mFormula 字符串。今天下午我会更新我的答案,告诉你怎么做。
    • 再次感谢 Lo,我不是那么专家,我不知道我必须添加什么到 "#""Step2"" 才能过滤,你有一些参考吗?我尝试将字符串放入.CommandText = strSql,但使用like 运算符strSql = "SELECT * FROM [test_7]" & _ " WHERE AD ALIKE '*E7*' AND" & _ " B2='FF';" 错误1004 仍然出现相同的错误:不支持命令SELECT ....
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多