VBA - Import namespaces? - vba

Is it possible to import namespaces in VBA? I'm writing Word macros and I want to put all functions inside Modules or Classes. But once I do that I see that I cannot reach the controls in the VBA form easily. I have to include the name of the form first, then access the control within that one. I can't just go
TextBox1.Caption
I have to go
Form1.TextBox1.Caption
Is there a way to get around this?

You shouldn't be coupling your classes/modules so tightly. A worker module shouldn't require knowledge of the Form1 class, because then it can't be used separately in another project.
Instead, you probably want to pass the function in your helper class an argument on which it performs work, and then returns the result (if necessary). As a completely useless, trivial example:
Public Sub SetLabelText(ByVal lbl As Label, ByVal caption As String)
lbl.Caption = caption
End Sub
And you would call it from within the form class, like so:
MyHelpers.SetLabelText(Label1, "New Label Caption")
That way, you can use the functions in your helper class from any form.
But, as far as your actual question, no. VBA doesn't have any concept of "namespaces". The best you can do is the With statement, but having to do this frequently is more likely an indication of a design flaw, as I discussed above.

Related

sub functions vs event, what is the difference?

I went through events in Vb.net and i found they are really an amazing feature..but still confused about how to use them effectively.
The real doubt is that the functions and sub could be effective alternative for events. Whatever i do with events i can manage to do it via functions and methods. Wherever there is Raisevent, i can substitute it with a function or procesure. so then, what is the real benefit of events and in which cases i can use them instead of functions and procedures?
Let's see if you understand it with an example. You have the Button control. It defines a lot of events (Click,MouseDown,Keypress...). When you put a button in a Form, you decide there what are the events you want to manage. So in the form you can do something when a button is clicked, or not doing anything.
Imagine if this was done with regular methods. In that case, the button should have a reference to the form where the button is, and the form should have a Click method, is it using it or not.
The event driven programing makes very easy to define some Events to signal when something happens in a class, and is the class that creates the instance the one that decides if that event is relevant or not.
thank you for clarification, I got the point. The point I Missed was that : event is fired in the same class, but execution is done in a different class.
I mean, when I define a class, I can put RaiseEvent somewhere to recognize something, but I correlate the event with a sub in a different class.
so, in such a class called Wallet I can put a statement like :
If Dollars > 5 Then
RaiseEvent Above5()
End If
and in such different class called AllWallet :
Dim WithEvents myWallet as new Wallet
Sub myWallet_Above5() Handles myWallet.Above5
MsgBox("Dollars are more than 5")
End Sub
otherwise, I mean if events are fired in the same class they are defined in , the sub and functions can substitute events.
thanks to all

Accessing a form's tooltip from another class in visual studio (vb.net)

I am quite new to visual studio. Developed previously with vba.
Encountering a problem writing a language translation class.
The language dependent values are stored in a localdb table every row having formname , controlname and controlproperty as wel as a column per language.
The Language class Handles the translations at runtime. For forms I use a Sub FormUpdate(frm as Form) called from the form's load event as FormUpdate(Me) it checks the form's language kept in a custom parameter against the current language and updates the .text property of the controls on the form that are exposing some language specific text (Buttons, Labels, Tabcontrol etc..).
However I could not solve how to access and update the form's Tooltip component from the language class. (in vba it was easy as every control was exposing it's Controltiptext property)
In the form's class you would use MyTooltip.Settooltip(CtrlName,Text) but I could not figure out how to do that from another class (the tooltip component is not part of the form's controls - and I could not figure out nor find info how to do it) ??
Could someone advise pls?
I was so focused on finding a similar way to the one I used before that I did not see the easy way to resolve it - not yet enough accustomed to the new environment.
Sure, I am interested in an answer to my question but instead of making it too complicated using external components I'll do it the easy way by overloading the FormUpdate sub adding a sub with an extra parameter as Tooltip.

Best practice for using main form controls

So lets say I have a form1 that opens on startup. Form1 contains a textbox named textbox1 and a button named button1. When I click button1 this is code that is called:
Class form1
Sub CapLetters () 'buttons click event
Dim myObj as new MyObject
MyObj.CapAllLetters
Textbox1.text = MyObj.GetThoseCapLetters
End sub
End class
Now, I know this is kinda dumb, but my main question is, since im creating a new object, the textbox1 will not be available in my object unless I specifically call it like:
Form1.textbox1.text
Is this good practice or is there a better way? Now in my program I have about 10 textbox and comboboxs I need my object to use. I know i could do something like this:
MyObj.CapAllLetters (textbox1.text)
But that doesn't seem like a good idea to pass that many values in a method.
I think I need a way for my object to gather all the info upon initating it?
Thoughts?
You basically seem to be asking whether it's OK to use the default instance of a form. The answer is yes. In recent versions of VB, each form that has a parameterless constructor also has a default instance, which is a single instance that can be accessed anywhere and at any time via the class name. This feature exists because it makes it easier for beginners to access forms from different places in a project without having to worry about passing references to forms around that project.
In the case of the startup form in a project, it is always the default instance of its type, assuming that you haven't disabled the application framework. That means that you can always access your startup form anywhere in your project using the default instance of its type.
Now, you'll find that experienced developers rarely use default instances. That's because they are never required and rarely add value if you know what you're doing. That means that you can always access your application's startup form is reasonable locations throughout your project without using the default instance.

VBA OOP extending existing classes

I'm a rookie at VBA(Excel) but I know some OOP stuff from java. What I want to do is extend the class MSForms.TextBox to add my own constructors and properties. I've tried using the implements keyword but my class won't compile. It would also be helpful to add these fields into some kind of container variable since they're arranged in a 9x9 pattern. Here's what I want my custom textbox to do. (It's for a game)
I need to be initiate it and place it on the form and put initial text into the box based on what is passed to the constructor. I also would like to have it check the values at runtime with an event to make sure they comply with the game rules. If it does not comply I would like to change the background color to red.
I'm able to do all of this when I just make a bunch of form controls the old fashioned way and use a sub controlled by a button to check the text box values.
Any help on extending classes would be much appreciated.
Thanks
I don't think you can do that in VBA. I would keep two arrays, one array of TextBox and one array of a new class representing your extended properties. Then you keep them in sync. e.g. Textbox #71 and extended properties #71.
You can look at the following link for making and working with control arrays.
http://www.siddharthrout.com/index.php/2018/01/15/vba-control-arrays/

custom constructors for forms in vb.net: Best practices

I'm quite new to vb.net, and windows forms developement as a whole, so this might all be very basic, but here goes.
I would like to open a new form from some other form, and pass some selected object from a control on that form to the new form. The sensible way to do this, I thought, was as a parameter to the forms constructor. Now I know that the visual studio GUI creates partial classes for my forms, that hold the properties that I can drag onto there in the designer. I assume it also holds a default constructor. Since it might do all sorts of stuff that is needed to initialise the form, I figured I should call it from my custom constructor ala
public sub new(byval my_parameter as Foo)
Me.new()
Me.my_parameter = my_parameter
do_some_initialisation()
end sub
That clearly wasn't it, because it can't find a default constructor. The thing is, visual studio goes trough great lengths to prevent me from seeing the generated constructor, so I know how to access it. This leads me to believe that I am actually doing it wrong, and should have set out on some different path, as the path you are forced in to usually is the sensible thing to do, which I usualy find out way too late.
So how should I be doing something like this?
This is a fairly simple example.
This goes into your "main" form (the one you want to call your new form from):
Dim childForm1 As New form2Name(item)
childForm1.Text = "Title of your new form"
Call childForm1.Show()
form2Name(item) breaks up like "form2Name" is the name of the form you want to open and "item" is the parameter to be passed.
In your new form (form2Name) add this code:
Public Sub New(ByVal item As String)
InitializeComponent() ' This call is required by the Windows Form Designer.
MsgBox(item)
End Sub
You can do whatever else you need in your form.
Hope this helps.
For VB.Net I think the call you are after is
MyBase.New()
Your derived form class automatically inherits the default constructor for System.Windows.Forms.Form. This default constructor is invoked automatically before your derived constructor code executes. The reason you can't find any code for the default constructor is because the derived class does not specialize the default constructor. If you wish to define your own default constructor, you may. You can also define a constructor without parameters.
You code should work fine if you remove this line:
Me.New()