0

I've defined AllParts as a variable in VBA. As formatted in SQL (see below), I'm getting an error. New to this and can't seem to tweak appropriately. Any suggestions? Thanks!

and   msikp.concatenated_segments in (" & AllParts & ")
1
  • 1
    One line is not enough code - post the rest and don't ask us to guess... Commented Jan 28, 2017 at 3:44

1 Answer 1

1

Presumably your full VBA line looks something like this?

The actual quotation marks used for SQL strings will vary by the driver/DBMS. For SQL server, single-quotes should do:

Dim AllParts As String
Dim SQL As String

AllParts = "'Foo', 'Bar'"
SQL = "SELECT * FROM segments AS msikp " & _
      "WHERE msikp.id > 0 " & _
      "and   msikp.concatenated_segments in (" & AllParts & ")"

For Access, escaped double-quotes work better:

Dim AllParts As String
Dim SQL As String

AllParts = """Foo"", ""Bar"""
SQL = "SELECT * FROM segments AS msikp " & _
      "WHERE msikp.id > 0 " & _
      "and   msikp.concatenated_segments in (" & AllParts & ")"

If it does, then it should work, but beware that building SQL with concatenation of inputs can expose you to SQL Injection attacks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.