0

So I am writing a string in VBA. It needs to use 2 different numeric variables from a form that the user clicked on. My code works great if I only use 1 variable, but I can't get the syntax correct for 2 variables. Here is what I originally wrote that works fine.

stringSQL = "select table1.field1, table2.field2 from table1 where table1.flag='flag' and 
table1.weight = " & [Forms]![myform]![weight]  

When I try to add age to the string (I want to use age and weight from myform) I write this which does not work -

stringSQL = "select table1.field1, table2.field2 from table1 where table1.flag='flag' and 
table1.age= " & [Forms]![myform]![age] & " and " & table1.weight= " & 
[Forms]![myform]![weight]

1 Answer 1

1
stringSQL = "select table1.field1, table2.field2 " & _
            " from table1 where table1.flag='flag' " & _
            " and table1.age= " & [Forms]![myform]![age] & _
            " and table1.weight= " & [Forms]![myform]![weight]
Sign up to request clarification or add additional context in comments.

2 Comments

ahh thanks a million, i've been stuck on this problem for hours! I thought the & symbol meant concatenate, why is it that mine is incorrect and your works?
In your example you have table1.weight outside of the literal string part of the expression.

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.