Solved If then else statement with "AND"

Leo_Polla_Psemata

Registered User.
Local time
Today, 14:39
Joined
Mar 24, 2014
Messages
364
Hi
I use this beat of code and returns the results i wish.

Code:
If country - "China" then
       msgbox = "something"
elseif country = " Japan" then
    msgbox = "something else"
else
    msgbox = "something more"
Endif

I would like to add the "and" and make the code as below but the syntax is not correct
Do you know what syntax should i use ?
Code:
If country = "China" And region = "north" then
       msgbox = "something"
elseif country = "China" And region = "South" then
    msgbox = "something different"
elseif country = "Japan" And region is "east" then
    msgbox = "something else"
elseif country = "Japan" And region is "west" then
    msgbox = "something more"
else
    msgbox = "more"
Endif
 
What error message do you get?
 
Looks fine, I am not seeing any issue with the code. If you wanted to you could reformat it with nested ifs, which may make the logic a little easier to see but not really any better or worse.
Code:
If country = "China" Then
  If region = "North" Then
    MsgBox "something"
  ElseIf region = "South" Then
    MsgBox "something"
  End If
ElseIf country = "Japan" Then
  If region = "East" Then
    MsgBox "something"
  ElseIf region = "West" Then
    MsgBox "something"
  End If
End If
 
You don't need = after MsgBox
 
Last edited:
MSGBOX is a function, not a variable. (Or if it IS a variable, you just overloaded it.) The "=" uses MSGBOX as a variable and the compiler knows better.
 
Are you asking us to debug real code or pseudo?

I never use ElseIf. I much prefer to use a Case statement.
Code:
Select Case country
    case "China"
      If region = "North" Then
        MsgBox "something"
      ElseIf region = "South" Then
        MsgBox "something"
      End If
    Case "Japan"
      If region = "East" Then
        MsgBox "something"
      ElseIf region = "West" Then
        MsgBox "something"
      End If
    Case Else
        Msgbox "something else"
End Select
 
It all looks pretty experimental now. In the database, however, you will quickly come to replacing such if-then cascades with a lookup table and appropriate access to it. A table can then be maintained better and expanded with variants.

I never use ElseIf
... as you can see in the fifth and eleventh lines of code. (I think I passed the attention test.)
 

Users who are viewing this thread

Back
Top Bottom