简体   繁体   English

如何在 vba 访问中将变量放入 sql 命令字符串中?

[英]how can I put a variable in an sql command string in vba access?

letter=65 

is a variable representing a letter (A) in the alphabet.是表示字母表中字母 (A) 的变量。 I want to insert letter into part of an sql command string: sqlUrlCount = "SELECT tblMain.Name FROM tblMain WHERE (((tblMain.Name) Like '" & Chr(letter) & "*'))"我想在 sql 命令字符串的一部分中插入字母: sqlUrlCount = "SELECT tblMain.Name FROM tblMain WHERE (((tblMain.Name) Like '" & Chr(letter) & "*'))"

but I get an error: "Run-time error '3075': Syntax error in string in query expression '(((tblMain.Name) Like".但我收到一个错误:“运行时错误‘3075’:查询表达式中字符串的语法错误‘(((tblMain.Name) Like”。

this is probably because of the quotes and double quotes I used.这可能是因为我使用了引号和双引号。 but how else can I do this?但我还能怎么做?

When I tried to create a field called 'Name' in Access it complained that it was a reserved word - could be part of the problem.当我尝试在 Access 中创建一个名为“名称”的字段时,它抱怨它是一个保留字 - 可能是问题的一部分。

The code below returned an SQL string which worked when I pasted it into the query window (note - have updated 'Name' to 'sName' and the field type is Text).下面的代码返回了一个 SQL 字符串,当我将其粘贴到查询窗口中时该字符串有效(注意 - 已将“名称”更新为“sName”并且字段类型为文本)。
Also note - Access loves putting brackets around everything and most of the time they're not required.另请注意 - Access 喜欢在所有内容周围放置括号,而且大多数情况下它们不是必需的。

Sub test()

    Dim letter As Long
    Dim sqlUrlCount As String

    letter = 65

    sqlUrlCount = "SELECT sName FROM tblMain WHERE sName Like '" & Chr(letter) & "*'"

End Sub

Edit:编辑:
To use a SELECT query:要使用 SELECT 查询:

Sub Test()

    Dim sqlUrlCount As String
    Dim letter As Long
    Dim rst As DAO.Recordset

    letter = 65

    sqlUrlCount = "SELECT tblNewMain.WebAddressCompareString, " & _
        "Count(*) AS [How Many?] FROM tblNewMain " & _
        "WHERE (((tblNewMain.WebAddressCompareString) Like '" & Chr(letter) & "*')) " & _
        "GROUP BY tblNewMain.WebAddressCompareString HAVING (((Count(*))>1))"

    Set rst = CurrentDb.OpenRecordset(sqlUrlCount)

    With rst
        If Not .BOF And Not .EOF Then
            .MoveFirst
            Do
                Debug.Print .Fields("WebAddressCompareString") & " : " & .Fields(1)
                .MoveNext
            Loop Until .EOF
        End If
        .Close
    End With

    Set rst = Nothing

End Sub

If it's a DDL or DML type query - ie UPDATE, CREATE TABLE, SELECT INTO, etc then you can use DoCmd.RunSQL如果它是 DDL 或 DML 类型的查询 - 即 UPDATE、CREATE TABLE、SELECT INTO 等,那么您可以使用DoCmd.RunSQL

Your syntax is correct so try to copy-n-paste the resulting string:您的语法是正确的,因此请尝试复制粘贴结果字符串:

SELECT tblMain.Name FROM tblMain WHERE (((tblMain.Name) Like 'A*'))

into a new query.进入一个新的查询。

If it fails, adjust the SQL and then your code.如果失败,请调整 SQL,然后调整您的代码。

If it runs, then rewrite your code line (no copy-n-paste), then delete the old code line.如果它运行,则重写您的代码行(不要复制粘贴),然后删除旧的代码行。

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

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