简体   繁体   English

如果/然后使用RadioButton

[英]If/Then with RadioButton

In my VBA code, I query a SQL table. 在我的VBA代码中,我查询一个SQL表。 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. 我想添加一个if / then语句,以便如果选择了一个单选按钮,它将拉某个值,如果选择了另一个单选按钮,它将拉一个不同的值。 My if is radiobutton1, and my else is radiobutton2, although the else can just be to take the other value. 我的if是radiobutton1,我的else是radiobutton2,尽管else可以只是取其他值。

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.) 如果我的if / then用于field3(单选按钮将指向field4。)

How do I add the if/then statement in at this point? 此时如何添加if / then语句? 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? 我该怎么办?

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&语句可能会使您的代码更简洁一些

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. 但这是一种糟糕的方法,您应该将这些变量传递给存储的proc。

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

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