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
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: