Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on If and Else If statement please? 1

Status
Not open for further replies.

nunan

Technical User
Feb 11, 2004
41
GB
Hi

I am very new to coding so please be patient with me!

I have a form called AM Contact Mgmt that reads data from a table called AM Clients.

The table AM Clients contains 3 fields:
Phone - text format
Mobile - text format
Preferred Phone - number format

On the form AM Contact Mgmt I have 4 fields
Phone - text format
Mobile - text format
Preferred phone - 2 check boxes - if no check box is selected the value on Preferred phone is 0, if check box 1 is selected the value in Preferred Phone is 1 and if check box 2 is selected the value in Preferred Phone is 2.
PhoneTop - Unbound

What I want to do is have PhoneTop display the preferred contact phone number so I guess something like this:

If [Preferred Phone] = 0 Then [PhoneTop]=[Phone]
else
If [Preferred Phone] = 1 Then [PhoneTop]=[Phone]
else
If [Preferred Phone] = 2 Then [PhoneTop]=[Mobile]

I hope that this makes sense, any help would be greatly appreciated.

Thanks
 
what you are looking for is called an immediate if statement. This is how you use it in your query:

Code:
iif([Check Box 1] = 0, iif([Check Box 2] = 0, [Phone], [Mobile]), [Phone])

Or, I just realized that no check boxes and check box 1 are the same thing so you can actually use

Code:
iif([Check Box 2] = 1, [Mobile], [Phone])

The iif statement works like this:

iif(condition, selection if condition is true, selection if condition is false)

Hope this helps,

Alex


Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
Hi Alex

Thank you so much, all that for one tiny little line of code!!!

One more thing, where exactly should I place the code?
 
You just want [Phone Top] to display on your form, right?

If so, you would just set your control source for the text box to

Code:
 = iif([Check Box 2] = 1, [Mobile], [Phone]) 

'this next one might work better as it references the table not the form

= iif([Preferred Phone] = 2, [Mobile], [Phone])

If you added a third phone number field, you could use a modification to the longer piece posted above.

I am also a little confused about your check boxes. Is this a data entry form? Seems like it would be easier to have a drop down with choices 'Mobile' and 'Home', and then assign a 1 or 2 to your field in the table based on this.

Hope this helps,

Alex



Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
Hi Alex

Yes the drop down would be easier but the client I am building this for wants it that way unfortunately.

Your second option
= iif([Preferred Phone] = 2, [Mobile], [Phone])
Works perfectly.

Thank you so so much I really appreciate it
xxx

 
Glad to help, and thanks for the
star.gif
.

Take Care,

Alex


Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top