VBA Populate userform dynamically with custom class Frame

saboh12617

New Member
Joined
May 31, 2024
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi,
In a class module, I define a private attribute as a Userform, and then a public withEvent attribute as a SpinButton. And then the private subs mySpinButton_SpinUp() and spinDown.
Why when i show the userform and click the coresponding buttons, my events are not called correctly ? is it because of the scope of the private "mother" userform that kind of blocs the calls to the child public button, even if it is declared as public ?
Otherwise i really dont understand. I can post the code but it could be a little bit confusing as i have the code for the userform, for the "frame bloc" ie the class im talking about, and then a test sub in a module to implement all of this.

The idea is that i define a small frame with a textbox, a label and a spinbutton. When clicked on the spinbutton, the textbox updates. I can have an undefined quantity of those frames, that's why, i read, it is convenient using a class for this frame object.

The code of the Frame bloc :
EDIT : PickAttributes is the name of the userform on which i implement this element.
VBA Code:
Private Const BLOCK_W As Long = 66
Private Const BLOCK_H As Long = 72

Private Type tAttributeFrame
  tFrame As MSForms.Frame
  tTextBox As MSForms.TextBox
  name As String
  value As Long   ' faire un total
  minVal As Long
  maxVal As Long
End Type

Private this As tAttributeFrame
Private daddyForm As PickAttributes
Public WithEvents spinButt As MSForms.SpinButton

Public Sub DefineFrame(dadF As PickAttributes, fieldName As String, minVal As Long, maxVal As Long)
  Set daddyForm = dadF
  this.name = fieldName
  this.value = minVal
  this.minVal = minVal
  this.maxVal = maxVal
 
  Set this.tFrame = daddyForm.Controls.Add("Forms.Frame.1", "FRAME." & fieldName, True)
  With this.tFrame
    .Height = BLOCK_H
    .Width = BLOCK_W
    .Caption = fieldName
  End With
 
  Set this.tTextBox = this.tFrame.Controls.Add("Forms.TextBox.1", "TEXTBOX." & fieldName, True)
  With this.tTextBox
    .Height = 25
    .Width = 25
    .value = this.value
    .Top = 12
    .Left = 6
    .TextAlign = fmTextAlignCenter
    .Locked = True
  End With
 
  Set spinButt = this.tFrame.Controls.Add("Forms.SpinButton.1", "SPINBUTT." & fieldName, True)
    With spinButt
    .Height = 25
    .Width = 14
    .Top = 12
    .Left = 42
    .min = minVal
    .max = maxVal
  End With
 
End Sub

Public Sub DefinePos(dVertical, dHoriz)
  With this.tFrame
    .Top = dVertical
    .Left = dHoriz
  End With
End Sub


Private Sub class_initialize()
  ' todo default title etc
End Sub

Private Sub spinButt_SpinDown()
  this.value = WorksheetFunction.max(minVal, this.value - 1)
  this.tTextBox.Text = this.value
 
  Debug.Print "hi", this.value  ' not called
End Sub

Private Sub spinButt_SpinUp()
  this.value = WorksheetFunction.min(maxVal, this.value + 1)
  this.tTextBox.Text = this.value
End Sub

Public Property Get value() As Long
  value = this.value
End Property
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Nevermind, figured it out. I was refering to "maxVal" and "minVal" instead of "this.maxVal" and "this.minVal"...
If you have suggestions on how to reduce the amount of variables or make this code more clean i'm open to suggestions. In an ideal world i wish i could define the frames without linking them immediately to an userform. So my variables would just be the values held, and not also the userform objects.
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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