Need help trying to pass variables.

Mendel

Registered User.
Local time
Today, 13:45
Joined
Jan 18, 2001
Messages
34
I'm getting stuck here. I made a simple form (called Test) with an option group with 2 option buttons (optBlue and optRed). I have a label called lblTest and a command button called cmdGo. The cmdGo has an On_Click() property set to event procedure. The code for the cmdGo calls function ChangeColor that exists in a regular module. When thr form is run pushing the cmdGo button should change the backcolor of the label based on what option button is selected. This is the code that I have written:

Form Module:

Option Compare Database
Option Explicit

Private Sub cmdGo_Click()
Call ChangeColor
End Sub

Regular Module:

Option Compare Database
Public Blue As String
Public Red As String
Public optgrpColor As OptionGroup
Public lblTest As Label
Public Test As Form_Test
Option Explicit

Function ChangeColor()

Set optgrpColor = Test

Select Case optgrpColor

Case 1
lblTest.BackColor = vbBlue
Case 2
lblTest.BackColor = vbRed
End Select

End Function

I'm getting an error when trying to set optgrpChangeColor

I get run-time error 91. Object variable or With block variable not set.

I'm not familiar enough with VBA to figure out how to fix this. If anyone could look at my code and see what it is that I'm doing wrong I sure would appreciate it.

Thanks.


[This message has been edited by Mendel (edited 03-13-2001).]
 
You seem to be trying to set optgrpColor to the form object rather than the optiongroup object.

Try: -

Set optgrpColor = form_Event_form_UpdateSiteDatabase.optgrpColor

Also as you're directly referencing the form in the code module you might as well have this function in the form module. Personally I tend to keep functions in code modules generic and keep form specific functions in the form modules.
 
I still get the same error when I tried what you suggested.

The reason why I want to call the function instead of keeping it in the form code is because I just wrote this quick form to test how to pass variables. My real form has many many buttons and other controls and my form code is getting very very very long and ugly.

Thanks for your help though.
 
I've got it to work now I did it this way:

Option Compare Database

Public Blue As String
Public Red As String
Public optgrpColor As OptionGroup
Public lblTest As Label
Public Test As Form_Test


Option Explicit

Function ChangeColor()

Set Test = Forms!Test
Set optgrpColor = Test.optgrpColor
Set lblTest = Test.lblTest

Select Case optgrpColor
Case 1
lblTest.BackColor = vbBlue
Case 2
lblTest.BackColor = vbRed
End Select

End Function
 

Users who are viewing this thread

Back
Top Bottom