VBA get instance name inside of class - vba

Sub Main()
Dim test As New Class1
End sub
Class1:
Private Sub Class_Initialize()
msgbox(Name_of_Class_Instance)
End Sub
I want the msgbox to show "Test"
Can that be done in VBA?

As per my understanding, you are trying to get the class instance name which is declared in module.
First of all you need to do is create a class like below.
Public classname As String
'Below method is going to get the value from module.
Public Sub Class_Initialize()
MsgBox classname
End Sub
Second, create a module like below.
Sub getname()
Dim test As Class1
Set test = New Class1
'Here we are passing the class name as 'test' and executing the 'Class_Initialize' method
With test
.classname = "test"
.Class_Initialize
End With
End Sub
Now you will get the instance name of the class.

Related

Communicating with a class that's in use in VB.NET

I'm trying to communicate with a class that's already in use, something like this.
Assume "PROJECTNAME" as a namespace or the whole project
Class1:
Public Class Class1
Dim Text1 As String
Sub ChangeString(input as String)
Text1 = String
End Sub
Sub Main()
Do
' Code to prevent the Process stopping (Ignore This)
Loop
End Sub
End Class
Class 2:
Imports PROJECTNAME
Public Class Class2
Dim ForeignClass1 As New Class1
Dim ForeignClass2 As New Class1
Sub Main()
Do
'Code to prevent the Process stopping(Ignore This)
Loop
End Sub
End Class
Class 3:
Imports PROJECTNAME
Public Class Class3
Dim CustomClass As New Class2
Sub Main()
'Code here to interact between "ForeignClass2" and "ForeignClass1"
End Sub
End Class
The codes above is a concept that I'm trying to explain, not the actual code as it's a private code that I'm not suppose to expose. So what I want is to make "ForeignClass2" interact with "ForeignClass1" or make "ForeignClass1" interact with "ForeignClass2" without touching or making a new "Class1" array and just "ForeignClass1" and "ForeignClass2" only. How can I do that?

Access a class modules variables within a second class

I was wondering if there's a way in which I can share variables between instances of separate class modules?
I have two classes:
Class 1
Class 2
Inside class 1, I have multiple global variables which I would like Class 2 to have access to once instantiated.
I could use get and set properties for each of the variables but I have about 40/50 so it just seems a bit tedious.
So, instead, I'm trying to pass the current instance of Class 1 to Class 2 using set property.
I've created a minimal example to illustrate my current efforts:
Class 1:
Public test As String
Private Sub Class_Initialize()
Call setTest
Dim b As Class2
Set b = New Class2
End Sub
Public Property Set Classed(ByRef vClass As Class1)
Set vClass = Me
End Property
Public Sub setTest(t As String)
test = "Sam"
End Sub
Class 2:
Private Sub Class_Initialize()
Dim newClass As Class1
newClass.Classed = newClass
' Want to be able to access the test String from class 1
End Sub
Obviously what I am doing at the moment is incorrect, so am wondering if someone could point out where I'm going wrong and show me how to achieve this class sharing?
Just to add: when running the code, I receive a compile error at line: newClass.Classed = newClass. Error: Invalid use of property
Not too sure but I sense a bit of a Circular Reference in your example?
What Are Circular References?
A circular reference occurs when two objects hold references to each other.
You could try an alternative by exposing a Dictionary object through your class, where the Key will be your "variable name", and the Value will hold the actual value.
An example could be:
Class1
Option Explicit
Private mList As Object
Public Property Get List() As Object
Set List = mList
End Property
Private Sub Class_Initialize()
Set mList = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set mList = Nothing
End Sub
Implementation:
Sub ClassTest()
Dim a As Class1
Set a = New Class1
Dim b As Class1
Set b = New Class1
a.List("VarName") = "Sam" 'Set
b.List("VarName") = a.List("VarName") 'Get / Set
Debug.Print b.List("VarName") 'Get
Set a = Nothing
Set b = Nothing
End Sub
'Output
'Sam

Calling private sub from module, concatenated reference

I have a couple of forms (i.e. frmTest) with bound comboboxes (i.e. cboTest). I'm trying to solve the NotInList event by a public sub, which calls back a button click sub's in these forms (i.e. btnTest_Click).
Form frmTest:
Private Sub cboTest_NotInList(NewData As String, Response As Integer)
Response = acDataErrContinue
Item_NotInList NewData, Me, "btnTest"
End Sub
Public Sub btnTest_Click
'....
End sub
Module:
Public strNotInList_Text As String
'public variable to store entered text
Public Sub Item_NotInList (strNewData As string, frmForm As Form, strControl As String)
Dim strControl_Sub As String
strNotInList_Text = strNewData
strControl_Sub = "." & strControl & "_Click"
Application.Run frmForm.Name & strControl_Sub
End Sub
Acces returns an error "Program ... didn't found a procedure frmTest.btnTest_Click."
Why ?
Reference frmTest.btnTest_Click looks to be correct. Sub btnTest_Click is declared as public.
Thank you for yor help.
Couldn't you create an interface let say IMyInterface with this method btnTest_Click (should be named differently) and let the forms you want to call this method implement this interface. Then change the signature of Item_NotInList like this:
Public Sub Item_NotInList (strNewData As string, frmForm As IMyInterface, strControl As String)
' ...
frmForm.btnTest_Click
' ...
Because the instance of the form is available in the method Item_NotInList you simply call the target method. Does this help?
Example:
Add class module and name it e.g. IMyInterface (can be named according to your needs). Add empty body of the method (don't add any implementation).
IMyInterface
Public Sub TestClick()
' will be implemented in your forms
End Sub
Then implement this interface in your forms e.g. in Form frmTest and others which should be used with the Item_NotInList method.
Form frmTest example
Implements IMyInterface
Private Sub IMyInterface_TestClick()
' here goes your implementation
End Sub
Standard Module test code
Sub test()
Dim f1 As UserForm1
Set f1 = New UserForm1
Item_NotInList f1
Dim f2 As UserForm2
Set f2 = New UserForm2
Item_NotInList f2
End Sub
Sub Item_NotInList(testForm As IMyInterface)
testForm.TestClick
End Sub
Thats it. HTH

Access variable in Shared Sub

is there a way to access a variable in Form_Load from an event handler?
Please dont mind the code, this is just a representation of my question.
Public Class Form
Public Sub Form_Load()
Dim x as string
x = MyClass.MethodGetValue()
End Sub
Private Shared Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class
It's about the scope of the variable. In your situation you need a class variable. This allows it to be used anywhere inside of this class.
Public Class Form1
Private x As Object 'pick the datatype that matches your needs
Public Sub Form_Load()
x = MyClass.MethodGetValue()
End Sub
Private Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class

Variable Protection Level in Script

I am creating a script programatically. However I cannot access the dataset from the modeule. See "Sample" below. ('ds' is not declared. It may be inaccessible due to its protection level.)
In the Sub main() I can access ds without a problem
In the Sub test() in Cacl_Module I cannot access ds.
Namespace Evaluator
Public Class Evaluator
Public ds As New DataSet
Public ComboBox1 As New ComboBox
Public CheckBox1 As New CheckBox
Public TextBox1 As New TextBox
Sub main()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Class
Public Module Calc_module
Sub test()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Module
End Namespace
That's because ds is a member of the Evaluator class. In the Main method of the Evaluator class, you can access its members. But in the test method of the Calc_Module, you don't have an instance of Evaluator. You should create one, like that:
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
Debug.Print(MyEvaluator.ds.Tables("Table1").Rows.Count)
Debug.Print(MyEvaluator.CheckBox1.Checked)
End Sub
End Module
Or even better (no need to duplicate code)
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
MyEvaluator.main()
End Sub
End Module