Dynamic Events to Pre-Populate Start Time

hicksi

Board Regular
Joined
Mar 5, 2012
Messages
203
Hi,
I believe that my problem can be solved by an Events Class, but...

The problem that I have is that I have a variable number of rooms (specified in an Excel sheet), with a Morning and Afternoon booking. By default, when someone allocates a room, it gets pre-filled with 08:00 or 13:00.
The screen will show a textbox for each of the unallocated rooms. So there could be up to 5*2*number-of-rooms. This I achieve by dynamically adding the textbox in the "correct" position on the Userform.

What I want to achieve is to have the user click in the required textbox, have it pre-fill with the default, and then also validate the value when exiting the textbox to be a valid time and within range.

Currently, pseudo-code is like:
VBA Code:
For each Room
    Determine .Top value
    Display Room Label
    For each Day
        Determine .Left value
        For each Session
            Add offset to .Left
            If session-not-allocated then
                        Set objTxt = Frame.Controls.Add("Forms.TextBox.1")
                        With objTxt
                            .Top = iTop
                            .Left = iLeft
                            .Width = 46
                            .Tag = Identifiable Tag
                            .Visible = True
                        End With
            Endif
        Next
    Next
Next

SO... My question is how do I supply Events to each of these TextBoxes?
 
It WORKS, it WORKS, it WORKS. Thank you.

So, as a test for "full feature" of my problem, I created a form with a single commandbutton, to which THIS code applies

VBA Code:
Option Explicit

Dim TextBoxEvents   As Collection

Private Sub CommandButton1_Click()
Dim objTxt          As Control
Dim Evnt            As ClassTextBoxEvent
Dim i               As Long, j              As Long
    
    Set TextBoxEvents = New Collection
    For i = 1 To 5
        For j = 1 To 6
            If Rnd(Now()) < 0.2 Then
                Set objTxt = Me.Controls.Add("Forms.TextBox.1")
                With objTxt
                    .Top = 30 * i
                    .Left = 50 * j
                    .Width = 46
                    .Tag = i & ":" & j
                    .Visible = True
                    Set Evnt = New ClassTextBoxEvent
                    Set Evnt.MyTextBox = objTxt
                    TextBoxEvents.Add Evnt
                End With
            End If
        Next
    Next
    CommandButton1.Enabled = False
End Sub


And my WithEvent class (called ClassTextBoxEvent for clarity)

VBA Code:
Option Explicit
Public WithEvents MyTextBox As MSForms.TextBox

'Private Sub MyTextbox_Change()
'    MsgBox MyTextBox.Tag & " changed."
'End Sub

Private Sub MyTextBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If MyTextBox.Value = vbNullString Then MyTextBox.Value = Format(TimeValue(MyTextBox.Tag), "HH:MM")
End Sub
 
Upvote 0
Solution

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
It WORKS, it WORKS, it WORKS. Thank you.

So, as a test for "full feature" of my problem, I created a form with a single commandbutton, to which THIS code applies

VBA Code:
Option Explicit

Dim TextBoxEvents   As Collection

Private Sub CommandButton1_Click()
Dim objTxt          As Control
Dim Evnt            As ClassTextBoxEvent
Dim i               As Long, j              As Long
   
    Set TextBoxEvents = New Collection
    For i = 1 To 5
        For j = 1 To 6
            If Rnd(Now()) < 0.2 Then
                Set objTxt = Me.Controls.Add("Forms.TextBox.1")
                With objTxt
                    .Top = 30 * i
                    .Left = 50 * j
                    .Width = 46
                    .Tag = i & ":" & j
                    .Visible = True
                    Set Evnt = New ClassTextBoxEvent
                    Set Evnt.MyTextBox = objTxt
                    TextBoxEvents.Add Evnt
                End With
            End If
        Next
    Next
    CommandButton1.Enabled = False
End Sub


And my WithEvent class (called ClassTextBoxEvent for clarity)

VBA Code:
Option Explicit
Public WithEvents MyTextBox As MSForms.TextBox

'Private Sub MyTextbox_Change()
'    MsgBox MyTextBox.Tag & " changed."
'End Sub

Private Sub MyTextBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If MyTextBox.Value = vbNullString Then MyTextBox.Value = Format(TimeValue(MyTextBox.Tag), "HH:MM")
End Sub
That's awesome hicksi
Are you all good now? Or is there something else we can help with?
 
Upvote 0
@Fluff Might be able to sort it
Found it. Many thanks for your assistance.
Now for an add-on question... Since an instance of the event gets added to the collection for each textbox, does that mean I should make the code in there as small as possible, calling out to code in a module to do the work? Or am I just showing my 1980s coding practices, where smaller code meant faster processing?
 
Upvote 0
No need to do that as each instance uses the same code and therefore is not actually duplicated
olny time you'l find issues is if you have say 100 textboxes and each has an enormous adodb.recordset or something massive in it
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top