Do you refer to form controls directly?
  v1.0 Posted at 26/04/2018 4:10 AM by Tiago Araujo

When programming in form based environments one thing to remember is not to refer to form controls directly. The correct way is to pass the controls values that you need through parameters. There are a number of benefits for doing this:

  1. Debugging is simpler because all your parameters are in one place
  2. If for some reason you need to change the control's name then you only have to change it in one place.
  3. The fact that nothing in your function is dependant on outside controls means you could very easily reuse your code in other areas without too many problems re-connecting the parameters being passed in.

It's a correct method of programming.

​​Private Sub Command0_Click()
End Sub
Sub CreateSchedule()
Dim dteDateStart As Date
Dim dteDateEnd As Date
dteDateStart = Format(Me.ctlDateStart,"dd/mm/yyyy") 'Bad Code - refering the form control directly
dteDateEnd = Format(Me.ctlDateEnd, "dd/mm/yyyy")
.....processing code
End Sub

Bad Example​​

Private Sub Command0_Click()
CreateSchedule(ctlDateStart, ctlDateEnd)
End Sub
Sub CreateSchedule(pdteDateStart As Date, pdteDateEnd As Date)
Dim dteDateStart As Date
Dim dteDateEnd As Date
dteDateStart = Format(pdteDateStart, "dd/mm/yyyy") 'Good Code - refering the parameter directly
dteDateEnd = Format(pdteDateEnd, "dd/mm/yyyy")
&....processing code
End Sub

​Good Example​​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address: