how declare local variable in vb.net with largescope - scope

example:
class start
sub one
dim n as integer=5+2
end sub
sub tow
msgbox(n )
end sub
end class
I want to declare a variable in a sub and use it from another sub ...or its scope is for class but declares it in a sub.
thanks.

Related

Passing text box text from a userform to a sheet using MVP pattern

Ok... So I am trying to wrap my head around properties. I have done a ton of googling but really need to try myself to fully understand. I have 3 components:
I have a UserForm: "frmView1"
I have a class module: "clsPresenter1"
I have a regular module: "modModel1"
My UserForm has a txtbox "TextBox1" and here is the userform code:
Option Explicit
'View (The V in MVP)
Public Event OnTxtBoxChange()
Public Property Get TxtBoxInfo() As String
TxtBoxInfo = TextBox1.Text
End Property
Private Sub TextBox1_Change()
RaiseEvent OnTxtBoxChange
End Sub
My Class module:
Option Explicit
'Presenter Class (The P in MVP)
'Implements Business Logic
Private WithEvents objUserForm As frmView1
Private Sub Class_Initialize()
Set objUserForm = New frmView1
End Sub
Public Sub TxtBoxContent()
objUserForm.TxtBoxInfo
End Sub
Private Sub objUserForm_OnTxtBoxChange()
DoSomething
End Sub
My regular module:
Option Explicit
'Model Module (The M in MVP)
'Business Logic
Private objPresenter As clsPresenter1
Public Sub DoSomething()
Sheet1.Cells.Clear
Sheet1.Cells(3, 3) = objPresenter.TxtBoxContent
End Sub
So this is a shortened version to simplify my question. In reality I have code to show the form and close the form and a few other things that are working fine. But my question is... How do I pass whatever the user types in the txt box to the worksheet? I keep getting:
Compile error: Expected Function or variable
Ok... So I may have figured it out... I changed:
Public Sub TxtBoxContent()
objUserForm.TxtBoxInfo
End Sub
To:
Public Function TxtBoxContent() As String
TxtBoxContent = objUserForm.TxtBoxInfo
End Function
This seems to work... Please let me know if this is not the correct way...
Maybe this is actually the correct way? Again changing:
Public Sub TxtBoxContent()
objUserForm.TxtBoxInfo
End Sub
To:
Public Property Get TxtBoxContent() As String
TxtBoxContent = objUserForm.TxtBoxInfo
End Property
Again this seems to work... But why would I ever use a function in my class module ("presenter") if I could always use a property instead?

When assigning value to class variable in VBA excel, the value is not set. How do I set a class declared variable?

I'm pretty new to VBA but I have been trying to figure out what is going wrong here. When I try to set a variable declared in my class module from a Sub in my module, the value isn't assigned for some reason and I can't figure out why. How do I get the variable table to go all the way through to the function call response = add_edit_data("fund_management.db", table)? It all compiles and runs, until it reaches the add_edit_data function which of course does not work without this variable. My Debug.Print checkpopup.table in the Sub returns nothing although I know the variable table from calling the Sub is set correctly as Debug.Print table returns the correct value when called from inside the Sub.
My module looks like this:
Private checkpopup As class_checkpopup
Public Sub checkbox_popup_fund(table)
Set checkpopup = New class_checkpopup
checkpopup.Show
checkpopup.table = table
Debug.Print checkpopup.table
End Sub
The class module is:
Public table As String
Private WithEvents check_box_popup As check_box
Private Sub Class_Initialize()
Set check_box_popup = New check_box
End Sub
Public Sub Show()
check_box_popup.Show
End Sub
Private Sub check_box_popup_Closed()
End Sub
Private Sub check_box_popup_Yes()
response = add_edit_data("fund_management.db", table)
End Sub
and I activate the eventhandler with:
Public Event Yes()
Private Sub check_box_yes_Click()
RaiseEvent Yes
End Sub
Thank you to Nicholas Hunter and Variatus for helping me out. I wasn't aware that it mattered to set the variable before showing the Form, but it does. I changed my module to:
Private checkpopup As class_checkpopup
Public Sub checkbox_popup_fund(table)
Set checkpopup = New class_checkpopup
Let checkpopup.table = table
checkpopup.Show
Debug.Print checkpopup.table
End Sub
and now my value carries through all the way to my function. Thank you very much for the help

Using variables in multiple macros

Can someone explain what am I doing wrong here. Excel doesn't seem to set the variable value if I run the second macro before the first one. I would like to set the values just once and use them in multiple macros. I have 6 macros that will be using the same values. The code included below is a short code I use to test.
Option Explicit
Dim Test As String
Sub sub1()
Test = "Test"
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
This is the popup i get if the second macro is executed before the first one.
Just call the second macro from the first. Otherwise Test will not be assigned a value.
You can read about scope here.
Dim Test As String
Sub sub1()
Test = "Test"
MsgBox Test
sub2 'or call sub2
End Sub
Or you can use a Constant, e.g.
Public Const Test As String = "Test"
(see my edit below - the original answer doesn't answer the question you asked properly)
You can set it as a public variable
Option Explicit
Public Test As String
Sub sub1()
Test = "Test"
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
Or call sub2 from sub1 with a parameter
Sub sub1()
Test = "Test"
MsgBox Test
'call the second sub with the Test variable here
sub2 Test
End Sub
Sub sub2(test as string)
MsgBox test
End Sub
or have Sub1 be a function and return the value
Function sub1()
Test = "Test"
MsgBox Test
'call the second sub with the Test variable here
sub1 = Test
End Function
Sub sub2()
test = sub1()
MsgBox test
End Sub
EDIT :
Or you could wrap it all in another sub so you are able to define the public variable
Public Test as string
Sub sub1()
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
Sub run()
Test = "Test"
sub1()
sub2()
End Sub
If you want a variable to keep its value for the whole excel session (i.e. a static variable) use the Global keyword
Global Test As String
Remember, one of your procedures must assign a value to it. Otherwise it will have a value of ""

How do I call a module from my userform in vba?

I have trying to call a module by assigning my sub name however it does not work
I tried by
Private Sub Filtert04_click()
Call filter 'the sub name
End Sub
It would be great if someone could help thx
Make sure the Sub in the module is defined as Public
Public Sub DoIt()
MsgBox "Hi"
End Sub
and then from your userform, you can call it like this:
Private Sub Filtert04_click()
DoIt
End Sub

How to tell which form module called your class

Suppose, for instance, that I want a method that adds a ComboBox. Maybe I try this
Public Sub AddComboBox()
Dim cb As MSForms.ComboBox
Set cb = <Calling form module>.Controls.Add("Forms.ComboBox.1")
End Sub
How can I get <Calling form module>?
As others have said, pass the instance of the form to class method. Unlike others, I'm going to add:
Declare the argument AS
MSForms.UserForm
Pass the parameter ByVal.
If calling from the UserForm itself,
use the Me keyword in the call.
He's a brief example:
' <Module1.bas>
Option Explicit
Sub Main()
UserForm1.Show vbModeless
UserForm2.Show vbModeless
End Sub
' </Module1.bas>
' <UserForm1.frm>
Option Explicit
Private Sub UserForm_Activate()
Dim c As Class1
Set c = New Class1
c.AddComboBox Me
End Sub
' </UserForm1.frm>
' <UserForm2.frm>
Option Explicit
Private Sub UserForm_Activate()
Dim c As Class1
Set c = New Class1
c.AddComboBox Me
End Sub
' </UserForm2.frm>
' <Class1.cls>
Option Explicit
Public Sub AddComboBox(ByVal MSForms_UserForm As MSForms.UserForm)
Dim cb As MSForms.ComboBox
Set cb = MSForms_UserForm.Controls.Add("Forms.ComboBox.1")
End Sub
' </Class1.cls>
I think you're writing this the wrong way. Instead of trying to determine who called the method, just pass the <Calling Form Module> to AddComboBox() as an argument. Like this:
Public Sub CallToAddComboBox()
AddComboBox(<Calling form module>)
End Sub
Public Sub AddComboBox(CallingFormModule as <Module Object Type>)
Dim cb As MSForms.ComboBox
Set cb = CallingFormModule.Controls.Add("Forms.ComboBox.1")
End Sub

Resources