Dylan
Dylan

Reputation: 81

Declaring more than one Condition in IF statement for same result

I have a scenario where I need two conditions in an IF statement to show the same result.

i.e:

 IF Condition 1 = something AND Condition 2 = Anything           
   txtMessage = "Hello world."

 Else
   txtMessage = "Goodbye world.

Condition 1 being the 'Tag' option for controls in Access, and Condition 2 being the text in the control such as "N/A".

I know in Java you can use && to declare this, but how can I do this in VBA? I've checked out similar questions in the forums and Google but couldn't find an answer for this. Other people suggest using elseif statements, but there should be a way to declare two conditions which result in the same thing in one line, no? Am I over-looking something simple here, or is this the wrong way to look at it?

EDIT

Here is a snippet of the code I am using

If ctrl.Tag = "BlkChk" Then
    If IsNull(ctrl) Then
       strMsg = strMsg & "- " & ctrl.Name & vbCrLf
    End If
End If

Basically, I would like this code to check if the control is either blank, or contains the text "N/A" If so, print the text " Hello World." If not, print the text "Goodbye world."

Upvotes: 0

Views: 202

Answers (2)

Will H
Will H

Reputation: 36

If you're trying to achieve the same as && in Java, that is called a short-circuit. The way it works is that only if the first condition evaluates as true is the second condition evaluated. A short-circuit is not available in VBA, but you can achieve something similar with a Switch statement.

See: AndAlso/OrElse in VBA

Upvotes: 2

Reticulated Spline
Reticulated Spline

Reputation: 2012

Are you saying you want txtMessage to equal "Hello world." if either of the conditions are true or if both conditions are true?

If the former, use the keyword Or. If the latter, use the keyword And.

Source: http://msdn.microsoft.com/en-us/library/wz3k228a.aspx

Upvotes: 1

Related Questions