If and statement

duvenhlc

Registered User.
Local time
Today, 13:05
Joined
Feb 4, 2008
Messages
54
Please help

I had think myself into a corner with someting simple. What is the format for a statement where two fields need to be equal to a value.

If me.x = "1" and me.y = "2" then me.z = "whatever"
end if

What should this statment actually look like

Thanks
 
Howzit

You have pretty much got it. I tend to always put the else in, but I think you don't need it if there is nothing you want to do.

I'm not sure if it makes a difference if the part after the Then shold be on a separate line. I have always put it on a new line, to make it easier to read.

Code:
if me.x = "1" and me.y = "2" then
   me.z = "whatever"
else
   something else or nothing
end if
 
but this syntax is incorrect

If me.x = "1" and me.y = "2" then me.z = "whatever"
end if


its either

no endif is needed if all on one line
if a then b

or
an endif needed if the condition is put on another line
if a then
b
end if


and if .. else .. endif
if a then
b
else
c
end if
 
Howzit

Thanks Dave - I've only ever used the If..Else..End if method, but have seen some examples of your post elsewhere. Nice to clarfiy the rules
 
BTW...

If you are using more and more complex If statements it is advisable to keep them readable...
So instead of...
Code:
    If Forms("frmReportingParameters")!cboSomeCombo = "Yes" And Forms("frmReportingParameters")!cboAnotherCombo = "Yes" and Forms("frmReportingParameters")!cboComboNR3 = "Yes" Then

Use something like so....
Code:
    If Forms("frmReportingParameters")!cboSomeCombo = "Yes" And _
       Forms("frmReportingParameters")!cboAnotherCombo = "Yes" And _
       Forms("frmReportingParameters")!cboComboNR3 = "Yes" Then

Good luck and Happy coding !
 
Ok Seems like I sort it, I just thought one should see a "End If" statement

If Me.Value1 = "1" And Me.Value2 = "1" Then _
Me.Answer = "Red"
If Me.Value1 = "1" And Me.Value2 = "2" _
Then Me.Answer = "Green"
If Me.Value1 = "1" And Me.Value2 = "3" _
Then Me.Answer = "Blue"

Thanks for you help
 
Your coding /formatting is somewhat unussual though .. LOL

Code:
If Me.Value1 = "1" And Me.Value2 = "1" Then Me.Answer = "Red"
If Me.Value1 = "1" And Me.Value2 = "2" Then Me.Answer = "Green"
If Me.Value1 = "1" And Me.Value2 = "3" Then Me.Answer = "Blue"
would be more ' regular ' in this case...

Also what happens with Value1 = "2" or Value2 = 4 ?? I would suggest something like:
Code:
If Me.Value1 = "1" then
    if Me.Value2 = "1" Then 
        Me.Answer = "Red"
    elseIf Me.Value2 = "2" Then 
        Me.Answer = "Green"
    elseIf Me.Value2 = "3" Then 
        Me.Answer = "Blue"
    else
        Me.Answer = "Unknown value2"
    end if
elseif .... some others
else 
    me.answer = "Unknown value1" 
end if

Greets :)
 
Thanks for all the info, I am taking info off a decision matric. The x axis is the probability, and the y axis is the consequence. The "block" where the ends meet will be the colour according to the matrix. Both these values need to be taken into acount, that is why I use the "and".

I manage so solve the isssue, regards
 
If Me.Value1 = "1" And Me.Value2 = "1" Then _
Me.Answer = "Red"
If Me.Value1 = "1" And Me.Value2 = "2" _
Then Me.Answer = "Green"
If Me.Value1 = "1" And Me.Value2 = "3" _
Then Me.Answer = "Blue"

just to sort the syntax

you are using line continuation characters so

if Me.Value1 = "1" And Me.Value2 = "1" Then _
Me.Answer = "Red"


is exactly the same as

if Me.Value1 = "1" And Me.Value2 = "1" Then Me.Answer = "Red"

Now if you DIDNT use the underscore and had

if Me.Value1 = "1" And Me.Value2 = "1" Then
Me.Answer = "Red"
end if


then you WOULD need the end if
 

Users who are viewing this thread

Back
Top Bottom