Run macro by clicking away from any ActiveX textbox in a document - vba

I'm looking for a VBA script that will run whenever I click away from any of ActiveX textboxes in the document. Another alternative would be to have it run whenever I click on any textbox (without clicking away).
How can it be done without assigning subs to each textbox individually?

Double click on your textbox and it will bring up the default method for that textbox, which in my testing is Textbox1_Change(). This method will run every time you type anything into the textbox.
You see two dropdown boxes at the top of the vba editor. Drop down on the one on the right and you'll see all the other available methods for the textbox, one of which is LostFocus which I reckon will suit your purposes. Clicking on that creates a sub that will execute every time the textbox loses focus. See how you go with that. Cheers

Related

Validation on textbox VBA Word prevents button click executing

I am working on a Word template that has 5 text boxes on a user form (frmMain).
I have a validation routine for each text box, that checks if the correct format is used (i.e. date format in one text box, only allowing the use of numeric values in another, preventing all text boxes left empty and so on). One of my text boxes is called txtNumber.
As of now, one of the validation sub routine fires on txtNumber_Exit.
I also have a command button (cmdHelp) on the same user form that, when clicked, fires the sub routine that shows another user form (frmHelp) that contains a help text on how to use this template.
My problem is that when I click this command button (cmdHelp), the validation routine for the text box "txtNumber" fires. Hence I am stuck with a message (written by me) in a msgbox that says "Number can not be blank", and the frmHelp is not showing.
After this, none of my text boxes have the focus, but my cmdHelp button does.
So if I click the cmdHelp button now, the frmHelp is correctly showing.
But this is messing up the workflow, making the visual experience a bit fuzzy for the user, given the fact that the user gets unnecessary info on invalid input in the txtNumber text box, and that the user needs to click twice on the cmdHelp button.
How can I avoid this?

How do I make a userform appear on demand in Microsoft Word?

I can make it popup on opening the document, but what if I close the userform and want to it to show up again in the same document? I know in excel you can add a button onto a worksheet directly and make it show the userform on click, but this button is unavailable for microsoft Word. Is there a solution besides initiating the script by hand?
To display a VBA userform, you can trigger it from a macrobutton field, from a form field, from a shape, from a QAT button or from an ActiveX button. There are probably a couple of other methods I'm not remembering at the moment. Each is a little different in the exact steps, but all will run the command:
UserFormName.Show

How to make refresh function in VBA

I am using Access Database to make a program.
Here is the problem:
After I enter the data in textbox, which is in a blue boxes, and click 'Add Data' button, data move to the ListBox, which is marked with orange box. But I should press 'F5' button(refresh) to see the data. I want to see the data immediately after I click 'Add Data' button. Is there any way to do that?
Any comments would be greatly thankful(It would be nice if you can share your code)
Add ListBox.Requery to the button's click event right after new data has been added to the ListBox.
Private Sub YourButtonName_Click()
'Add data
Me.YourListBoxControlName.Requery
End Sub
requerying the form is the simplest way probably, but with no idea of what the button does, how your data is structured, how the form is structured, i.e. is the listbox bound or unbound, so cant really say, if F5, is doing it, then just look for the VBA method for refreshing the form.

Triggering Visual Basic Keydown events without a specific function

I'm building on top of code that a previous developer has left me, and he left something that intrigued me quite a bit.
Basically on his menus, he has a TextBox to take in user input and a button next to it to submit the value of the TextBox (for example if the user wanted to select option 1, he would input 1 into the TextBox and click the button). However, the user could also press the Enter key while focusing the TextBox, and it would be treated as the submit button was clicked.
Now this is simple enough to do, but when I check the VB code behind the menu, there's no TextBox_Keydown(...) Handles TextBox.Keydown function anywhere, only the button click event. How is he doing this? He has several menus that are similar and I can't figure out how.
A standard dialog box, if not told to act otherwise, enter does default command button and escape does cancel. In VB look at the properties Default for the command button.
I discovered how he was doing it. He basically mapped the AcceptButton and CancelButton properties of the entire Windows Form to various button functions.

How to use Mouseout Function in VBA with a condition Check?

Hi I have a VBA application with a combobox selection option.One of the combobox option is Other which when user select that option an invisibled label will pops up and prompt the user to fill the txtOther control.now my question is how I can get rid of the prompted label and make it to invisible after user fill the txtOther and move(focused)in other control?
here is a shot of my app:
Thanks for your time and help
It depends on how often or when you want the check to be done. I couldn't find a good reference to show you all of this, but you can view your VBE.
Within the code behind your userform you can use events for the text box.
If you want the check done every time you leave the text box:
Use the TextBox_Exit event.
If you want it to fire whenever the contents are changed, used TextBox_Change. There's lots of options, but based on your explanation I'd probably use Exit.
This answer shows some syntax examples for using the Textbox_Exit event.