How to stop repeat code (1 Viewer)

Falcon88

Registered User.
Local time
Today, 12:01
Joined
Nov 4, 2014
Messages
299
I have access db with a form ... contains some textboxes , comboboxes .... etc
For some reasons i want to hide combocox ( cboEmploeeAcc ) , based on value of cboSantAg .
I use :
Code:
Private sub Form_Current()

If me.cboSantAg=1 then

Me.cboEmploeeAcc.visible=false

Else

Me.cboEmploeeAcc.visible=true

End if

End sub


Also I have to add the same code on after update event of the cboSantAg combobox .

Is there a way to make me not need to repeat this code ?
 

Eugene-LS

Registered User.
Local time
Today, 12:01
Joined
Dec 7, 2018
Messages
481
Is there a way to make me not need to repeat this code ?
You are thinking correctly trying to optimize code, in programming this is called "polymorphism".

You can write approximately this kind of code:
Code:
Private Sub cboEmploeeAcc_Operaate()
    If Me.cboSantAg = 1 Then
        Me.cboEmploeeAcc.Visible = False
    Else
        Me.cboEmploeeAcc.Visible = True
    End If
End Sub

Private Sub cboSantAg_AfterUpdate()
    cboEmploeeAcc_Operaate
End Sub
Private Sub Form_Current()
    cboEmploeeAcc_Operaate
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:01
Joined
May 21, 2018
Messages
8,525
You can also make a toggle to shorten the code
Code:
Private Sub cboEmploeeAcc_Operaate()
     Me.cboEmploeeAcc.Visible = (Me.cboSantAg <> 1)
End Su
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:01
Joined
Sep 21, 2011
Messages
14,232
Providing there is no other code in either event, why not just call that event from the other?, just as has been shown with a whole new sub?
 

sonic8

AWF VIP
Local time
Today, 11:01
Joined
Oct 27, 2015
Messages
998
Providing there is no other code in either event, why not just call that event from the other?, just as has been shown with a whole new sub?
Calling event procedures in code is a bad practice, because ...
- you use (call) a badly named procedure in your code and thus reduce readability.
- you then got an event procedure that is also invoked independently of the event which is probably not expected by a developer reading the code.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:01
Joined
Feb 19, 2002
Messages
43,223
I use "call" always. Old habits die hard:) But I agree, calling an event procedure is a dangerous practice so I would make a separate procedure. However, If you're going to use the Reader's Digest version of the code (I don't) then you are using one statement to call a procedure that executes one statement. That doesn't buy anything unless you expect to have to add additional code later so I would just repeat the single line of code rather than make the reader jump to a different spot to see what is going on.

There is absolutely nothing wrong with the Reader's Digest version of the code but since I work as a consultant, I never know who will end up managing the apps I build after I'm done so the code I write is always obvious. You won't have to stop and think about what it is doing. It is only inside of loops that I would bother to streamline. Efficiency wise, I couldn't tell you whether the If then else is more efficient or less so use what you are comfortable with.
 

Users who are viewing this thread

Top Bottom