"Object variable or With block variable not set"

Posted on 2004-11-04
Last Modified: 2010-05-02
I am trying to call a simple class module sub with a command button.  When I do that I get the error.  However, it compiles fine.

Here is the code from the form:

Private Sub Connect_Click()
Dim myLaser As CLaser
End Sub

Here is the code from the class:

Public Sub Connect()
Dim Number As String

Number = GetLaserServerIP
FLaserTestApp.Text1.Text = SetIP(Number)
End Sub

I think my problem is really the way I call the connect function from the form.  I am not sure what's wrong.  Please help me.

Question by:axnst2
    LVL 7

    Expert Comment

    Hi axnst2,

    try changing to this:

    Private Sub Connect_Click()
    Dim myLaser As New CLaser    '''' MODIFIED
    End Sub

    LVL 7

    Accepted Solution

    Dim myLaser As CLaser
    will declare the myLaser variable to be of type CLaser

    Dim myLaser As New CLaser
    declares the myLaser variable to be of type CLaser _AND_ allocates space for it

    You can accomplish the same thing like this:

    Dim myLaser As CLaser
    Set MyLaser = New CLaser
    LVL 7

    Expert Comment

    Glad I could help.

    Thanks for the 'A' :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now