How to stop repeat code

Falcon88

Registered User.
Local time
Tomorrow, 02:16
Joined
Nov 4, 2014
Messages
318
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 ?
 
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
 
You can also make a toggle to shorten the code
Code:
Private Sub cboEmploeeAcc_Operaate()
     Me.cboEmploeeAcc.Visible = (Me.cboSantAg <> 1)
End Su
 
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?
 
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.
 

Users who are viewing this thread

Back
Top Bottom