SecureCloud
SecureCloud

Reputation: 103

vb6, variable not defined for Label using Module

Sorry to ask such a dumb question.. but for the life of me i cant get it.. i have searched EVERYWHERE... This is a Re-Creation of my code that gives the same error. This is the most basic example i could re-create.

I dont understand why i have to declare a Label ?? (or an object)

What I am trying to accomplish is use my main form to call all the modules.

This is the FORM

'frmMain.frm
Option Explicit

Public Sub btnOpen_Click()
    GetNum
End Sub

This is the MODULE

'modGet.bas
Option Explicit

Public Sub GetNum()
    Dim a As String
    Dim b As String

    a = "hello"
    b = "world"
->  Label1.Caption = a    'ERROR, Compile Error, Variable not Defined. (vb6)
    Label2.Caption = b
End Sub

YES, i have a form, with a Button named 'btnOpen', i have 2 Labels named 'Label1' & 'Label2'

If i ADD..

Dim Label1 As Object  'in MODULE

i get a different error.. ERROR '91' Object Variable or With block variable not set

IF I put everything in 1 FORM, it works..(but i want to use separate modules) I Commented out 'OPTION EXPLICIT' ... same error.

In another Test, i got the error for a TextBox..

TextBox1.Text = x

Once i get the answer for this, i can apply it for everything... I'm sure it's simple too and imma feel stupid. :-(

One of my Main Things is Querying WMI, and i get the ERROR '91' for the Label (This is in a For Each Loop) .. But its the same error, its like its makin me Declare Objects..(using Modules)

Label1.Caption = objItem.Antecedent

If Someone Could PLEASE Help me...

Upvotes: 1

Views: 5226

Answers (2)

jac
jac

Reputation: 9726

You get the error because Label1 and Label2, and your other controls for that matter do not exist in the scope of modGet.bas. They can only be referenced (the properties accessed or set), from with the form. The different error you get when you add Dim Label1 As Object is caused because an you defined Label1 as an Object, not as a Label, and an object does not have a Caption property. Unless you have a good reason for putting the GetNum sub in a .bas module move it into the form and it should work.

I modified the second example. It will modify the strings passed into it in a way that when execution passes back to the form you can assign the strings to your textboxes. I am against modifying controls on a form from another module because it goes against the idea of encapsulation.

'modGet.bas
Option Explicit

Public Function GetHello() As String
    Dim strHello As String

    strHello = "Hello"
    GetHello = strHello

End Function

'frmMain.frm
'Option Explicit

Public Sub btnOpen_Click()
    Label1.Caption = GetHello()
End Sub

Something a little different.

'MyModule.bas
Public Sub HelloWorld ByRef Value1 As String, ByVal Value2 As String)

    On Error GoTo errHelloWorld

    Value1 = "Hello"
    Value2 = "World"

    Exit Sub

errHelloWorld:
    ' deal with the error here

End Sub

'frmMain.frm
Option Explicit

Private Sub frmMain_Load()
    Dim strText1 As String
    Dim strText2 As String

    HelloWorld(strText1, strText2)
    Text1.Text = strText1
    Text2.Text = strText2
End Sub

I also added basic error handling in the second example

Upvotes: 4

Hrqls
Hrqls

Reputation: 2951

Use

form1.label1.caption = a

But make sure form1 is loaded

Upvotes: 4

Related Questions