Organizing If-else-statement (1 Viewer)

Thinh

Registered User.
Local time
Yesterday, 21:39
Joined
Dec 20, 2006
Messages
114
Here is a solution that will make nested if else statement easier to read and handled. This approach comes handy in many situation. Below is just how easy a simple control validation can look like.

standard approach:

if len(me.txtcontrol1 & "") = 0 then
msgbox "required control1"
end if

if len(me.txtcontrol2 & "") = 0 then
msgbox "required control2"
end if

if len(me.txtcontrol3 & "") = 0 then
msgbox "required control3"
end if

Approach I suggest:

dim status as integer
if len(me.txtcontrol1 & "") = 0 then status = status + 1
if len(me.txtcontrol2 & "") = 0 then status = status + 2
if len(me.txtcontrol3 & "") = 0 then status = status + 4

select case status
case 1
msgbox "required control1"
me.txtcontrol1.setfocus
case 2
msgbox "required control2"
me.txtcontrol2.setfocus
case 3
msgbox "required control1 and control2"
me.txtcontrol1.setfocus
case 4
msgbox "required control3"
me.txtcontrol3.setfocus
case 5
msgbox "required control1 and control3"
me.txtcontrol1.setfocus
case 6
msgbox "required control2 and control3"
me.txtcontrol2.setfocus
case 7
msgbox "required control1 and control2 and control3"
me.txtcontrol1.setfocus
case else
"save the records"
end select


I only use binary code logic to give its uniqueness. so if you have 4 controls that need to be check then your numbers will be 1,2,4,8 and if you have 5 controls to be check your numbers will be 1,2,4,8,16 etc.
This approach will take care of all possible combination given 3 controls. It would be clutter and hard to check if the same logic was applied with nested if-else-statement.
Here the breakdown and the reason why it wont produce any duplicated. I am only using 1,2 and 4.

001 = 1 1
010 = 2 2
011 = 3 2+1
100 = 4 4
101 = 5 4+1
110 = 6 4+2
111 = 7 4+2+1

Please let me know if you have any question regarding the logic or anything else.

David
please give me an exemple when the given setup will produce duplicate. I want to find the flaw that you mention regarding the solution I posted.

Thinh
 
Last edited:

DCrake

Remembered
Local time
Today, 05:39
Joined
Jun 8, 2005
Messages
8,632
What you arre using is known as magic numbers, the only mistake you have used is using the number 1, somewhare along the line you will have duplicate outcomes. You need to start at 2.

David
 

ajetrumpet

Banned
Local time
Yesterday, 23:39
Joined
Jun 22, 2007
Messages
5,638
i'm wondering how this got in this section...
 

Vassago

Former Staff Turned AWF Retiree
Local time
Today, 00:39
Joined
Dec 26, 2002
Messages
4,751
i'm wondering how this got in this section...

It was approved by a mod/admin. I'm not sure who, but seems like legit code to me. Since David replied, it could have been him.
 

Thinh

Registered User.
Local time
Yesterday, 21:39
Joined
Dec 20, 2006
Messages
114
The number I use are exponitial power of two. There is no issue using these number. I am just using binary code to list all the combination. I have given the break down of the logic and there is no way there could be a duplicated.

001 = 1 1
010 = 2 2
011 = 3 1+2
100 = 4 4
101 = 5 1+4
110 = 6 2+4
111 = 7 1+2+4

correct me if am wrong.
 

Users who are viewing this thread

Top Bottom