0

How I can insert an array (stored in a variable) into the WHERE clause of a SQL statement in VBA?

recordset1.Open "SELECT * FROM [Table] WHERE [NettingSet] = '" & varRecord & "'"

Originial string is:

recordset1.Open "SELECT Class FROM [BCBS279_NetSet_Basics] WHERE [LoadingID] =" & LId & "And [NettingSet] = '" & varRecord & "'"

I get this error:

Run-time error '13': Type mismatch

Array:

Dim varRecord As Variant
varRecord = slide7_netset_info_ID.GetRows(10, , 0)

This array just contains strings:

 Example_1, Example_2, Example_3, ..., Example_10

Any idea how to fix it?

0

1 Answer 1

1

You want to use IN in SQL and then construct the IN list using "join" in VBA:

". . . WHERE [Name] IN ('" & Join(varRecord, "', '") & "')"
Sign up to request clarification or add additional context in comments.

11 Comments

@tmenke . . . Does this refer to the construction of the string or to the SQL call itself?
Hi Gordon, thanks for your swift Response. If I try it like that: recordset2.Open "SELECT * FROM [Table] WHERE [Name] IN (' " & Join(varRecord, "', ' ") & ")' ". I get the error: Invalid procedure call or argument (Error 5).
it refers to the SQL Call itself
@tmenke . . . Then you should edit the question and show what the string looks like after it is constructed.
that's the concrete SQL-Statement: slide7_netset_basics.Open "SELECT Class FROM [NetSet_Basics] WHERE [LoadingID] =" & LId & "And [NettingSet] IN (' " & Join(varRecord, "', ' ") & ")' "
|

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.