Do you refer to form controls directly?
Updated by Brook Jeynes [SSW] 1 year ago. See history
123
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:
- Debugging is simpler because all your parameters are in one place
- If for some reason you need to change the control's name then you only have to change it in one place
- 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()CreateScheduleEnd SubSub CreateSchedule()Dim dteDateStart As DateDim dteDateEnd As DatedteDateStart = Format(Me.ctlDateStart,"dd/mm/yyyy") 'Bad Code - refering the form control directlydteDateEnd = Format(Me.ctlDateEnd, "dd/mm/yyyy").....processing codeEnd Sub
❌ Figure: Bad example
Private Sub Command0_Click()CreateSchedule(ctlDateStart, ctlDateEnd)End SubSub CreateSchedule(pdteDateStart As Date, pdteDateEnd As Date)Dim dteDateStart As DateDim dteDateEnd As DatedteDateStart = Format(pdteDateStart, "dd/mm/yyyy") 'Good Code - refering the parameter directlydteDateEnd = Format(pdteDateEnd, "dd/mm/yyyy")&....processing codeEnd Sub
✅ Figure: Good example