简体   繁体   English

如何在VBA上的SQL查询中将单元格Excel单元格引用定义为参数?

[英]how to define cell excel cell reference as a parameter in SQL query on VBA?

It would be great if someone helps on this. 如果有人对此提供帮助,那就太好了。

I would like to provide cell reference from seperate sheet as a parameter in Where query I mean instead of a string "Arunraj S", I need to give reference from the range ("c3") of sheet1("PDA"). 我想提供来自其他工作表的单元格引用作为我要查询的位置中的参数,而不是字符串“ Arunraj S”,我需要提供来自sheet1(“ PDA”)范围(“ c3”)的引用。

It is retrieving fine while giving the direct string through where query. 通过where查询提供直接字符串时,它可以很好地进行检索。

But It's throwing syntax error when I tried to change the query like below; 但是,当我尝试如下更改查询时,将引发语法错误;

 query = "SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE
 [Employee Name] =" & Sheets("PDA").Range(C3).value

Here is my code : 这是我的代码:

Sub Pull_Data_from_Excel_with_ADODB()

    Dim cnStr As String
    Dim rs As ADODB.Recordset
    Dim query As String

    Dim fileName As String
    fileName = "C:\Users\nizamudeen.s\Desktop\PDA Template.xlsx"


    cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & fileName & ";" & _
               "Extended Properties=Excel 12.0"

        query = "SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE [Employee Name] = 'Arunraj S';"


            Set rs = New ADODB.Recordset
    rs.Open query, cnStr, adOpenUnspecified, adLockUnspecified

        Cells.Clear
    Range("A2").CopyFromRecordset rs
    Dim cell As Range, i As Long
        With Range("A1").CurrentRegion
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next i
        .EntireColumn.AutoFit
    End With
End Sub

Typically, an employee's name is a text string. 通常,员工的姓名是文本字符串。 You should be able to combine single quotes (aka 'ticks') with the double quotes wrapped around your entire SQL statement in order that you do not have to deal with quotes inside quotes strings. 您应该能够将单引号(也称为“勾号”)与包裹在整个SQL语句中的双引号结合使用,从而不必在引号字符串中处理引号。

query = "SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE [Employee Name] LIKE '" & Sheets("PDA").Range(C3).value & "';"

With Bob in Sheets("PDA").Range(C3), this should read as, 使用Bob in Sheets(“ PDA”)。Range(C3)时,应显示为

SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE [Employee Name] LIKE 'Bob';

I usually 'pattern match' with the LIKE operator but for all intents and purposes, LIKE is the same as equals. 我通常与LIKE运算符进行“模式匹配”,但出于所有意图和目的,LIKE等同于相同。

You are passing the query as a string. 您将查询作为字符串传递。 As it stands now, your query looks like this (assuming the name in cell C3 is Tom Smith : 到目前为止,您的查询看起来像这样(假设单元格C3中的名称是Tom Smith

SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE
 [Employee Name] = Tom Smith

Looking at that, you can probably see that it would fail in SQL because it should read (take note of the single quotes around the name): 看到这一点,您可能会发现它在SQL中会失败,因为它应该读取(注意名称周围的单引号):

SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE
 [Employee Name] = 'Tom Smith'

So, change the query declaration to: 因此,将query声明更改为:

 query = "SELECT DISTINCT [Client Name] FROM [Execution Report$] WHERE
 [Employee Name] ='" & Sheets("PDA").Range(C3).value & "'"

Does that fix the problem? 这样可以解决问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM