• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

I would like some help with my method

I am working out of a "Learn Advanced VB" book and I have copied the code exactly, so I thought, from the text.
However, when I attempt to run it, I receive an error, variable not defined, on my "With mDispensaryItem" inside my cmdSetProperties_Click sub.
While this is not all of the code, it should be enough to hopefully point out my error.

Private mDispensaryItem As New CDispensaryItem 'Instansiate the new object

Private Sub cmdSetProperties_Click()
    'Assign form control values to properties of object
    If cboFrameSize.ListIndex <> -1 And cboFrameStyle.ListIndex <> -1 Then
        With mDispensaryItem
            .FrameSize = cboFrameSize.ListIndex + 1 'Save size number
            .FrameStyle = cboFrameStyle.ListIndex + 1 'Save style number
            .Quantity = Val(txtQuantity.Text)
        End With
        MsgBox "Enter values for object properties.", vbInformation, "Advanced Vision"
    End If
End Sub
3 Solutions
Let's rule out the obvious...

Your declaration and the event where you reference it are within the same code module, correct?

Private mDispensaryItem As New CDispensaryItem   }
                                                                            > same module?
Private Sub cmdSetProperties_Click()                      }
Robberbaron (robr)Commented:
You do have OPTION EXPLICIT as the first line of code in all modules dont you....

Then use an uppercase letter when you define each variable & control.  eg sFirstName as string

then when you use variables in the rest of code, type them with lower case only. If the variable has been declared, the IDE will convert the variable name to include the upper case characters.

This helps with the situation Erick queries as well.
Just a trick I've learned...
shawngilbertAuthor Commented:
Erick37, yes.  Both are within my form code.

robberbaron, would you mind elaborating a bit further?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


The code you have posted can be copy/pasted directly into a new form and run, generating errors of course.  But extrapolating from the code, if two combo boxes are dropped on, a text box, and a Class Module is added to the project using the three Properties that are being set in the code (I used Variant types in the class builder just to make it work, and populated the combo box with three values each just to have something to drop down...), and drop on a command button with the name to launch that code, then when running it:
It gives the message about entering properties if one or more comboboxes aren't set, otherwise, if either is, it runs fine to the end.

If that is copied directly from your program file, then you don't have a one instead of an I etc, thats a given.

The other SOURCE of error then could be a bug in the class-module code itself.
Throw in some debug.prints into the code above, and one into the Class module Initialize event as well, and see...

        Debug.Print "Prior to with"
        With mDospensaryItem
            Debug.Print "After with"
            .FrameSize = cboFrameSize.ListIndex + 1 'Save size number
            Debug.Print "After framesize"
            .FrameStyle = cboFrameStyle.ListIndex + 1 'Save style number
            .Quantity = Val(txtQuantity.Text)
        End With
        Debug.Print "Ended With"

..in class module
Private Sub Class_Initialize()
    Debug.Print "Initialized"
End Sub

Resulting run output is

Prior to with
After with
After framesize
Ended With

---  Although it really makes sense for it to only be that the Variable not Defined message would appear for an incorrectly spelled variable name, or one that isn't defined within the scope of its usage, and that some other error would arise from a bug in the class module, there does exist a possibility that there is code within the class initialize event which actually results in the non-initialization or termination of the class.
Forcing a termination into the initialization event,

Private Sub Class_Initialize()
    Debug.Print "Initialized"
End Sub

Does indeed produce a "Variable not Defined" error for the With statement.

Hence, it would seem your error could be in the class module, and should look there.
shawngilbertAuthor Commented:
Nothing is produced from these debug.print statements
shawngilbertAuthor Commented:
Great!  I found it.
I am somewhat embarrased to admit it though, but as seeing how this site is here to help others, I will.

My declaration for the item did not exist in my general declarations section.  I had placed my form_unload just above it.

Thank you for your time and efforts.  I will split the points for contributions and guidance.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now