SSW Foursquare

Do you refer to form controls directly?

Last updated by Brook Jeynes [SSW] 8 months ago.See history

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()
 CreateSchedule
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

We open source. Powered by GitHub