Run-time Error 91 variable not set (class Modules) (2 Viewers)

VSolano

Registered User.
Local time
Today, 14:31
Joined
Feb 21, 2017
Messages
95
Hi,
I am trying to better understand the class modules in Access vba. I created my first class and I am getting a variable not set error and I can not figure out what is the problem

Any guidance will be greatly appreciate.

Code:
' the class module has the following code'
Option Compare Database
Option Explicit

Private oForm As Form
Private ControlCollection As VBA.Collection

Public Property Set p_Form(vNewValue As Form)
   Set oForm = vNewValue
  
End Property
Public Property Get p_Form() As Form
   Set p_Form = oForm

End Property
'I am calling the code from a click bottom on the form'
Option Compare Database
Option Explicit
Private NewFrm As Cls_Form


Private Sub btnTest_Click() 
  

  Set NewFrm = New Cls_Form 
   Set NewFrm.p_Form = Me
  
End Sub
 
I tested your code and it works fine without any error.
 
I am still getting Run-time error 91
Object variable or with block variable not set
 
the code works fine when I remove the information from the class initialize. The error is in here
Code:
Private Sub Class_Initialize()
  
   Dim Ctr As Control
  
   Set ControlCollection = New Collection
   For Each Ctr In oForm.Controls
      Select Case Ctr.ControlType
         Case acLabel
            Ctr.ForeColor = ColorBank.Blue
            
      End Select
   Next
   Set Ctr = Nothing 
  

End Sub
 
"Variable not defined" on ColorBank.

Is there more code you have not provided? Could provide db for analysis.

I fix that then get your error on Set NewFrm = New Cls_Form

Your Class_Initialize doesn't make sense to me as it is not setting default values of class properties. It is setting formatting properties of controls. Seems to me that belongs in another procedure. And don't see a custom class is any benefit here. Exactly what are you trying to accomplish?

Why do you Set ControlCollection then don't use it?
 
Last edited:
Class_Initialize() is not the event to set up those colors on the Form.
Class_Initialized() will be called First when you instantiate the Class.
therefore, the Class don't know anything about oForm (or oForm is not yet Created).
create a New Private method/event to set the colors, and don't use Class_Initialize().
I am assuming you have Public Enum ColorBank somewhere on any Module.

example (Cls_Form class):

Code:
Private oForm As Form
Private ControlCollection As VBA.Collection

Public Property Set p_Form(vNewValue As Form)
   Set oForm = vNewValue
  
  ' Setup colors for the controls
  Call SetUpControlColors
  
End Property

Public Property Get p_Form() As Form
   Set p_Form = oForm

End Property


Private Sub SetUpControlColors()

  Dim Ctr As Control
  

   For Each Ctr In oForm.Controls
      Select Case Ctr.ControlType
         Case acLabel
            Ctr.ForeColor = ColorBank.Blue
            
      End Select
   Next
   Set Ctr = Nothing
  
End Sub

Private Sub Class_Initialize()
   Set ControlCollection = New Collection
End Sub

Private Sub Class_Terminate()
   Set ControlCollection = Nothing
End Sub
 

Users who are viewing this thread

Back
Top Bottom