I have defined a public sub in a common module (normal module). I am trying to access this in class module.
Below is both the code -
Public loadGroupIndicator As Boolean
Public Sub stopLoadGroupIndicator()
loadGroupIndicator = False
End Sub
Public Sub startLoadGroupIndicator()
loadGroupIndicator = True
End Sub
Public Sub getLoadGroupIndicator()
getLoadGroupIndicator = loadGroupIndicator
End Sub
I am trying to use "getLoadGroupIndicator" function in below function of a class module (testClass)
' This is in testClass
' Private sub to execute something on the event
Private Sub m_chckBox_Click()
If getLoadGroupIndicator Then
MsgBox "loadGroupIndicatorLocal is true"
Else
MsgBox "some other msg based on the object property"
End If
End Sub
I am getting the below error msg
Please help me fix this. I am new to VBA and struggling on this since yesterday.
Thanks in advance for any help or relevant pointers.
Related
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?
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
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
I put this code into the module page
Option Explicit
Dim correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
Then when I call these 2 subs from a sheet my variable correct never switch to True
Private Sub CommandButton4_Click()
Call setCorrect
Call checkCorrectTrue
End Sub
Because you're using Dim correct As Boolean it is only available in that module. To be able to use the variable across modules you need to declare it using Public
Try using
Option Explicit
Public correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
I have a strange problem defining a class in an Excel VBA macro.
To do this I go to Excel→DEVELOPER(tab)→Visual Basic (the new view is opened). In this new view I do Project Name→Insert→Class Module and a new file is created.
Then I try to create a simple class in this file, like:
Class VirtualWindow
Public Sub Class_Initialize()
Debug.Print "Class_Initialize"
End Sub
Public Sub Class_Terminate()
Debug.Print "Class_Terminate"
End Sub
End Class
End Class is in red color, this meaning that it's not ok and when I try running it to see what error it throws it gives me the "Invalid outside procedure" error. I don't understand where is the problem.
You don't need the Class VirtualWindow and End Class here (since it is Visual Basic for Applications, not Visual Basic 6: VB vs. VBA) you can define the name in the properties field:
After this, from another function or class you can create an instance of the class:
Private w as VirtualWindow
Public Sub YourSub()
Set w = new VirtualWindow
End Sub
You're mixing up VB6 syntax with another language I think.
You do not have to put "Class VirtualWindow" and "End Class" in your code.
Just:
Public Sub Class_Initialize()
Debug.Print "Class_Initialize"
End Sub
Public Sub Class_Terminate()
Debug.Print "Class_Terminate"
End Sub