Form Controls as Pseudoarrays

Solo712

Registered User.
Local time
Today, 15:54
Joined
Oct 19, 2012
Messages
836
Hi everyone,
Every so often I see requests on Access boards inquiring about a way to streamline events for multiple controls of the same kind, so they are executed as a single function. Surprisingly, most answers are negative in the sense that VBA (unlike VB) doesn't know control arrays. Then someone may come and say, no it's quite possible and offers a solution managing the controls and events associated with them via a class module. This of course works (in theory), but such approach is costly in terms of overhead.
There is another solution which strikes me as much simpler. It is based on the observation that VBA controls on a form come with a numeric suffix which can be managed so they become a set of ordinals 1 to x, with no breaks. Such collection of controls can easily easily be utilized to provide an identifying argument to a function called by any event. So, one function to many, many controls of a kind. No formal collection needs to be created and controls encapsulated, because the controls have a numeric identifier already, which can be used to control the associated events individually.
A classical example of a form with a considerable number of controls would be a monthly calendar. You need 37 rectangles, and within them 37 combo/listboxes if you want to track multiple activities within that day. To implement the controls as pseudoarrays and link them to the event functions all you need is something like this:

Code:
Private Sub InitializeEventsforControlArrays()
   Dim ctl As Control, i As Long

   For i = 1 To 37
     Set ctl = Me.Controls("List" & i)
     ctl.OnClick = "=GetList(" & i & ")"
     ctl.OnMouseDown = "=DEmpty(" & i & ")"
     '
     Set ctl = Me.Controls("Rct" & i)
     ctl.OnClick = "=GetDay(" & i & ")"
     '
   Next i

   Set ctl = Nothing

The event functions then identify the calling control and relate the event to it exclusively binding it (via workday offset) to the particular day of the month.

Few notes on editing the listboxes:

1) You need to establish the active day in the month. (By default, current month is displayed and today is the active day. It is marked red on top of box.) You can establish another day as active in one of two ways:
a) by clicking on any part of the listbox that is not an entry. The active day will be displayed in the right bottom corner and the top of the box. Then you click the Edit Day Entries button at the bottom. All the day's entries will be in the recordset.
b) by clicking directly on an entry of the listbox, in which case you will be allowed to access that record only.
In both cases, the single calendar entry form will appear.
2) Relocation of entries to another day (even in another month) is permitted except for new entries. They must be made in the "active day".
3) The editing form for a single calendar entry will tell you when it gets dirty by displaying an pencil icon in the upper right corner. While the form is in edit mode, you may not exit. You either have to "Save" the record or "Undo" the changes.

Have fun with it.

Jiri
 

Attachments

Last edited:
I think you may confuse people with the term "pseudo array" since that is not really what you are doing. Because a real control array has other attributes.
What you are really doing is creating a common parameterized function to serve as an event handler instead of building individual event handling procedures.
I explain this in detail here, how a single function can trap multiple events. I do agree that many people are not aware how easy this is to have a single function handle multiple events.
I would edit the thread and do away with the "pseudo array' term and explain this more as creating a common function to serve as an event handler. I would rename the thread "Assigning a Common Event Handler to a Group of Controls.
 
As stated above, this approach has been around for a long time. It can indeed simplify repetitive coding.
I have used a similar numbering system for many years in several of my own applications including my revamped version of the Better Date Picker originally created by Brendan Kidwell back in 2003
 
Last edited:
Thanks, Colin.

Guys, I made no claim to a historical discovery in the OP. It's just that a number of posters at AWF seem unaware that this coding style is available and very effective for a task like creating an event calendar. Take e.g. the collection of calendars and date pickers that MajP posted last November. https://www.access-programmers.co.u...endar-controls-and-calendar-resources.315041/
None of the respected authors featured - not even the venerable Allen Browne - availed themselves of the possibility of. creating the event links by initializing code - surely a big labour saver.
 
Hi everyone,
Every so often I see requests on Access boards inquiring about a way to streamline events for multiple controls of the same kind, so they are executed as a single function. Surprisingly, most answers are negative in the sense that VBA (unlike VB) doesn't know control arrays. Then someone may come and say, no it's quite possible and offers a solution managing the controls and events associated with them via a class module. This of course works (in theory), but such approach is costly in terms of overhead.
There is another solution which strikes me as much simpler. It is based on the observation that VBA controls on a form come with a numeric suffix which can be managed so they become a set of ordinals 1 to x, with no breaks. Such collection of controls can easily easily be utilized to provide an identifying argument to a function called by any event. So, one function to many, many controls of a kind. No formal collection needs to be created and controls encapsulated, because the controls have a numeric identifier already, which can be used to control the associated events individually.
A classical example of a form with a considerable number of controls would be a monthly calendar. You need 37 rectangles, and within them 37 combo/listboxes if you want to track multiple activities within that day. To implement the controls as pseudoarrays and link them to the event functions all you need is something like this:

Code:
Private Sub InitializeEventsforControlArrays()
   Dim ctl As Control, i As Long

   For i = 1 To 37
     Set ctl = Me.Controls("List" & i)
     ctl.OnClick = "=GetList(" & i & ")"
     ctl.OnMouseDown = "=DEmpty(" & i & ")"
     '
     Set ctl = Me.Controls("Rct" & i)
     ctl.OnClick = "=GetDay(" & i & ")"
     '
   Next i

   Set ctl = Nothing

The event functions then identify the calling control and relate the event to it exclusively binding it (via workday offset) to the particular day of the month.

Few notes on editing the listboxes:

1) You need to establish the active day in the month. (By default, current month is displayed and today is the active day. It is marked red on top of box.) You can establish another day as active in one of two ways:
a) by clicking on any part of the listbox that is not an entry. The active day will be displayed in the right bottom corner and the top of the box. Then you click the Edit Day Entries button at the bottom. All the day's entries will be in the recordset.
b) by clicking directly on an entry of the listbox, in which case you will be allowed to access that record only.
In both cases, the single calendar entry form will appear.
2) Relocation of entries to another day (even in another month) is permitted except for new entries. They must be made in the "active day".
3) The editing form for a single calendar entry will tell you when it gets dirty by displaying an pencil icon in the upper right corner. While the form is in edit mode, you may not exit. You either have to "Save" the record or "Undo" the changes.

Have fun with it.

Jiri
Hi everyone,
Every so often I see requests on Access boards inquiring about a way to streamline events for multiple controls of the same kind, so they are executed as a single function. Surprisingly, most answers are negative in the sense that VBA (unlike VB) doesn't know control arrays. Then someone may come and say, no it's quite possible and offers a solution managing the controls and events associated with them via a class module. This of course works (in theory), but such approach is costly in terms of overhead.
There is another solution which strikes me as much simpler. It is based on the observation that VBA controls on a form come with a numeric suffix which can be managed so they become a set of ordinals 1 to x, with no breaks. Such collection of controls can easily easily be utilized to provide an identifying argument to a function called by any event. So, one function to many, many controls of a kind. No formal collection needs to be created and controls encapsulated, because the controls have a numeric identifier already, which can be used to control the associated events individually.
A classical example of a form with a considerable number of controls would be a monthly calendar. You need 37 rectangles, and within them 37 combo/listboxes if you want to track multiple activities within that day. To implement the controls as pseudoarrays and link them to the event functions all you need is something like this:

Code:
Private Sub InitializeEventsforControlArrays()
   Dim ctl As Control, i As Long

   For i = 1 To 37
     Set ctl = Me.Controls("List" & i)
     ctl.OnClick = "=GetList(" & i & ")"
     ctl.OnMouseDown = "=DEmpty(" & i & ")"
     '
     Set ctl = Me.Controls("Rct" & i)
     ctl.OnClick = "=GetDay(" & i & ")"
     '
   Next i

   Set ctl = Nothing

The event functions then identify the calling control and relate the event to it exclusively binding it (via workday offset) to the particular day of the month.

Few notes on editing the listboxes:

1) You need to establish the active day in the month. (By default, current month is displayed and today is the active day. It is marked red on top of box.) You can establish another day as active in one of two ways:
a) by clicking on any part of the listbox that is not an entry. The active day will be displayed in the right bottom corner and the top of the box. Then you click the Edit Day Entries button at the bottom. All the day's entries will be in the recordset.
b) by clicking directly on an entry of the listbox, in which case you will be allowed to access that record only.
In both cases, the single calendar entry form will appear.
2) Relocation of entries to another day (even in another month) is permitted except for new entries. They must be made in the "active day".
3) The editing form for a single calendar entry will tell you when it gets dirty by displaying an pencil icon in the upper right corner. While the form is in edit mode, you may not exit. You either have to "Save" the record or "Undo" the changes.

Have fun with it.

Jiri
I see an error when I execute
 

Attachments

  • upload(8).png
    upload(8).png
    84.9 KB · Views: 417

Users who are viewing this thread

Back
Top Bottom