Writing a macro for an Access form.
Since I know of no way to "trace" the value of a local variable, I want to put a label or textbox on the form that shows the current value of the variable. I tried to use SetProperty:
SetProperty
Control Name: lblMyVariable
Property: Caption
Value: MyVarible
You won't be surprised to learn that what's displayed in the label is the word "MyVariable", not the value thereof.
I also tried =[MyVariable], but that doesn't work either.
Unfortantly, you can't bind a control to a variable.
However, what you can do is in that form do this:
Public Function MyCompanyName as string
MyCompanyName = strCompanyName ' the varible here is strCompanyName
End function.
Now, you can place in the text box, or even label this:
=MyCompanyName()
So, you can place expressions as a text box or label source, but not actual variables. By wrapping the variable inside of a public function, then you can use that public function name as the source. Of course you have to create a function for each variable you wish to display. And of course scope (global or local to the form) will also be a factor.
If the variable is to be global in scope, then the function has to be in a standard code module outside of the form. But for variables in the forms code, then a local public function will also work.
Related
In my modules, I want to use my controls from my form. For example: I want to set focus on a textbox after a certain Sub.
My current solution is to make a subroutine to set all controls in a public variable (see below).
My questions:
What is the best practice? How do people usually do this?
When should I call my subroutine? (is it the first call in the FORM_LOAD sub?)
Public TBnr As TextBox
Public Sub controlsInitieren()
Set TBnr = Forms("frm_TreeView_Example").pstNr
End Sub
Well, as a general rule, while many platforms seperate out the UI part and the code part? Well, Access is quite much a different approach - it is standard fair to place the required code inside of the form "class" (all forms in Access are a "class", and you can even have muliple instances of the SAME form open more then one time).
So, in general, your code should be in the forms code (class) module.
However, you could and call a external routine.
So in the form, you could call your above routine like this:
Call MySetFocus(me, "NameOfControlToSetFocusTo")
And your sub would look like this:
Sub MySetFocus(f as form, sCtrl as string)
f(sCtrl).SetFocus
End Sub
However, as noted, the amount of code above is more code then simply in the forms code module going:
me.ControlName.SetFocus
However, while the above is a less then ideal example, passing the form "instance" (me) to a external sub or function allows you to referance any property or method or feature that exists in the form in an external routine.
So in place of say
me("LastName") = "Zoo"
In the above sample routine, you would and could go;
f("LastName") = "Zoo"
So any place you would and could use "me" in the form, you can use the form instance you passed from the form. As noted, it is a good idea to use "me", since as I noted, Access does allow multiple copies of the form to be opened at the same time - and thus your code can't distinguish between what form instance you are using unless you pass the current "in context" form. So like in JavaScript, using "this" ?
In access that current instance of the class object is "me", and you are free to pass that instance to any sub or function you want as per above.
The best practice is to use only procedures inside the form code. In such a case you refer to a text box control in this way: Me.Textbox1.SetFocus. If you want to set some controls properties during the form loading, you can do that in the frm_TreeView_Example_Initialize event;
They usually do it in the way I described at item 1;
If you want to use such a strange/unusual way you can do it calling the subroutine whenever you want. But, in order to set a specific property value of the form frm_TreeView_Example controls you can simply use frm_TreeView_Example.TextBox1.SetFocus. You can use this way of setting in a module procedure, even before the form has been shown. You can simply show it in that procedure code using at the end: frm_TreeView_Example.Show;
I got 2 forms:
Input.vb and Writer.vb
When Input was set as default form, I was able to do the following:
In Writer.vb
Input.mymessage = texter.Text
texter is btw an Textbox.
Since I needed to change the default form to another it does not work anymore.
How to fix that? Is there any "set parent" or "Dim Input as Input.Forms.all" way to get it work again?
Already tried Dim Input as new Input.
Edit:
I found the way of using CType(Me.ParentForm, Input).mymessage = texter.Text, but Writer.Parent = Me does not work for me :/
Remember that you have object definitions (classes), object instances of those classes, and variables that have references to those instances.
People tend to forget these things apply to forms, too.
When Input was set as the default form, VB.Net was giving you a default instance for the form, and a special global variable that refers to it with the same name as the class. So the name Input in your code could be one of two different things, depending on the context: the class type, or the special variable for the default instance of that class type.
Now that Input is not the default form, you're not using that default instance any more. When you show the form, you're creating your own instance. The same is true for the Writer form. You have a Writer class, but that's only the definition for an instance of the class you create somewhere. You need to provide this instance of your Writer form with a reference to the instance of the Input form that was created.
You do that the same way you handle object references with any other .Net class.
When going one to another form, instead of using the "Private Sub" I use Public sub, it shares everything in that form to anything else trying to use it.
I have a form with 5 controls.
For textbox I am accesing like frmNote.txtNumber.Text.
When I am accessing this property directly from form frmNote then I am getting the value of textbox.
But when I created a method for example NewMethod() in different vb file and call this method from frmNote form button click event then I am not able to access frmNote.txtNumber.Text value over there inside method.
It is coming blank. Do I need to pass all control values to method from form or is there any other way around.
Because txtNumber isn't shared, you can't access it from other class. You must pass it's value to NewMethod. Code will look like this:
Public Sub NewMethod(text as String)
'Use text
End Sub
And calling NewMethod:
NewMethod(txtNumber.Text)
I have an application that uses a second form with textBox and button, and i need to pass the text from textBox to the first form. I know how to do it with a public variable, but i would like to know if there is another way to do it without using a public variable.
Yes!
If the forms are part of the same solution, you simply write:
Dim MyVal1 As Integer = Form2.MyVal
MyVal1 exists in whatever form you write it in, and MyVal2 comes from Form2. These variables can only be shared if the forms have the right privacy settings.
If the two forms are part of separate applications, the only way i know of passing variables around is through constructors.
EDIT:
In your case with a textbox, you may be able to apply similar a solution:
Dim val As String = Form2.TextBox1.Text
You can use PUBLIC properties on the second form to read/write info to the other form.
You can overload the NEW method to pass variable during declaration.
You can create a public sub/function on the second form and pass variable byval or byref.
But, I would NEVER use a public variable. That is bad programming.
Excel VBA:
I am trying to get to some activex option buttons through the OLEObjects object, but I am finding that even though I change the value of (Name) in the property window for the object, it still requires the "OptionButton1" default name as a key.
I know that some of the objects in Excel VBA have a code name and another name which is also the one used for the key to get to it from OLEObjects, but I don't know how I can change this "other" name.
I am passing in my object name as a parameter (basically) to a function, so I can't just do :
ActiveSheet.optMyNewName.Value
or whatever. I need to be able to do this:
ActiveSheet.OLEObjects("optMyNewName").Object.Value
but currently only this works:
ActiveSheet.OLEObjects("OptionButton1").Object.Value
If you pass a string into the OLEObjects collection, it uses the Caption property as the key. Pass the caption as the parameter and it should work.