views:

38

answers:

1

In my VBA code, I query a SQL table. I want to add an if/then statement so if one radio button is selected it pulls a certain value, and if the other radio button is selected, it pulls a different value. My if is radiobutton1, and my else is radiobutton2, although the else can just be to take the other value.

Here's the specific part of the code:

strSQL = strBeginSQL1(strRiskSegment, "Detail")
strSQL = strSQL & "a.field1, b.field1, "
strSQL = strSQL & "a.field2, b.field2, a.field3, b.field3,"

If my if/then is for field3 (the radio button will point to field4.)

How do I add the if/then statement in at this point? I thought it would be:

strSQL = strBeginSQL1(strRiskSegment, "Detail")
strSQL = strSQL & "a.field1, b.field1,"
strSQL = strSQL & "a.field2, b.field2, If radiobutton2.true then a.field4, b.field4, else a.field3, b.field3,"
strSQL = strSQL & "a.field5, b.field5,"

What should I be doing?

+1  A: 

you should be using stored procedures.

It would probably make your code a little cleaner to just build two different strSql & statements for example

strSQL = strBeginSQL1(strRiskSegment, "Detail")
strSQL = strSQL & "a.field1, b.field1,"
if (radiobutton2.value = true) then
strSQL = strSQL & "a.field4, b.field4"
else if (radiobutton3.value = true)
strSql = strSQL & "a.field3, b.field3"
endif
strSQL = strSQL & "a.field5, b.field5,"

but this is a terrible way and you should just pass these variables to a stored proc.

george9170
I tried this but I got:run-time error '438';Object doesn't support this property or method
Daniel
Eh, just a syntax error. It's (radiobutton2.value = true) Thanks for the help!
Daniel
no problem, just out of curiosity are stored procedures an option with this project?
george9170
Don't see why not. I'm not so familiar with them, that's all. Next week's training on exactly that topic should help, there will be a lot of updates to the project, so maybe in phase 2 we'll clean up the code a bit.
Daniel
good deal, stored procedures are going to be a life saver
george9170