Solved If then else statement with "AND" (1 Viewer)

Leo_Polla_Psemata

Registered User.
Local time
Today, 15:42
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
 

XPS35

Active member
Local time
Tomorrow, 00:42
Joined
Jul 19, 2022
Messages
160
What error message do you get?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:42
Joined
May 21, 2018
Messages
8,555
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
 

KitaYama

Well-known member
Local time
Tomorrow, 07:42
Joined
Jan 6, 2022
Messages
1,553
You don't need = after MsgBox
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:42
Joined
Feb 28, 2001
Messages
27,226
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:42
Joined
Feb 19, 2002
Messages
43,367
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
 

ebs17

Well-known member
Local time
Tomorrow, 00:42
Joined
Feb 7, 2020
Messages
1,950
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

Top Bottom