I have a variable declared in a class file. A form is called from the class. When the control goes to the from; the value stored in the varaible is lost. Hence ,when I get the control back to the class from the form, I am unable to get the value of the variable . Please help me a way out as to how to preserve that value.

Note: I also observed by putting a watch on the variable that it svalue becomes empty when the control goes to the form from the class. The variable is declared as Public in the class.

9 Years
Discussion Span
Last Post by selvaganapathy

Hi Shouvik,

its showing compile error when I declare it as : Public Static str as String

please help me out


Do u create instance of the class inside the same form u r showing? u can show the form with new Instance like

Dim MyForm as Form1
Set MyForm = new Form1

Can u give the prototype of your coding


We have a class x() with the following code in it.

Dim MyForm As frm_ChangeOrder
Set MyForm = New frm_ChangeOrder
MyForm.Show vbModal

In MyForm we r accepting values and on click of a button we r calling a function which returns a string.
We want to get that string back to class x() from where MyForm was called


I would probably use a property in a module to store values across different forms and functions within an application.

Private m_strValue as String

Public Property Get StoredValue() As String
    StoredValue = m_strValue
End Property

Public Property Let StoredValue(Value As String)
    m_strValue = Value
End Property

Something like that :)

Then you'd just do:

StoredValue = Text1.Text

Thats not to say however that my way is the best way... but hope it helps :)


Hi dinilkarun,

Try this ' In Class (say X)

Public Sub ShowChangeOrder()
   [INDENT]Dim MyForm As frm_ChangeOrder
   Dim OrderDetails As String
   Set MyForm = New frm_ChangeOrder
   MyForm.Show vbModal
   OrderDetails = MyForm.OrderDetail
   'Now OrderDetails contain the preserved value
   Unload MyForm[/INDENT]
End Sub

' In Form (say frm_ChangeOrder)

Public OrderDetail As String

Private Sub Command1_Click()
   OrderDetail = FunctionToGetString()  ' Get and Preserve in OrderDetail
   Unload Me        ' Try to unload it will call Form_Unload
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Me.Hide  ' Dont miss it
End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.