LostFocus method of a text box getting executed before Form_Unload - forms

I am currently programming in MS Access VBA. I have a form with a textbox on it. I am executing some code on LostFocus event of the textbox.
However, when I close the form using mouse and by clicking on close button on right-top corner of the form, with cursor in the text box, I have noticed that LostFocus event is getting executed before Form_Unload event.
Is there a way to avoid this?
Also, where can I find priorities of the events?? Or How are priorities decided in VBA?
EDIT:
My exact application is like this.
I have a form in MS Access which is used for accepting users' login credentials.
I want to disble textbox for Password and Login button till a valid Username is entered. I check if the username is valid or not on onLostFocus event. Now, consider a case where this form is closed without entering anything in Username textbox. In that case, onLostFocus event gets executed before Form_Unload event.
However, for normal operation, I require onLostFocus event because, on that event I enable Password textbox and login button.
Could there be a different way of doing it??

The lost focus event is not usually the best event for code. Did you have a specific reason for using it? If so, you will need to add a condition to the execution.
The order of events for an Access form are:
Exit → LostFocus
If you are closing a form, the Exit and LostFocus events occur before
the events associated with closing the form (such as Unload,
Deactivate, and Close), as follows:
Exit (control) → LostFocus (control) → Unload (form) → Deactivate
(form) → Close (form)
-- http://office.microsoft.com/en-ie/access-help/order-of-events-for-database-objects-HP005186761.aspx

"I check if the username is valid or not on onLostFocus event."
Do your validation in the after update event of the username text box ... enable/disable the password text box as appropriate when the username value changes.
You can initially disable the the password text box at form load, assuming the form loads with the username text box empty.

using onDirty or onChange is probably better for what you're doing if it is a control that you're inserting data into. There's a bunch of better choices than onLostFocus. Without knowing anything specific makes it tough to know how to help you more specifically. I'd say just try another on Event, because due to the nature of onLostFocus, it'd be impossible to prepare for this contingency.

Related

MS Access 2013 textbox update macro

What I am trying to accomplish:
Use button to open a form, filter the form, and set specific value to an unbound textbox in the opened form's header. There are multiple buttons being used open the same form and I would like this textbox to changed every time a specific button is clicked.
What I have done so far:
Used a macro to open the form and the "where" condition to filter the records. I also used "SetProperty" to change the value of the unbound textbox in the opened form's header depending on which button was clicked. When I do used the SetProperty option in the macro I get the error "The control name ... is misspelled or refers to a control that doesn't exist. Error 32004
I have verified numerous times that this is the correct name for the textbox and everything. I am pretty new to access and don't do VBA all that much so any assistance would be greatly appreciated. Thanks.
First Form and Macro for the "Physical Security" Button
Second form with error and unbound txt box I want to change to "Physical Security"
A few Ideas to track down your problem:
Maybe there's a problem with opening and (immediately) accessing the forms controls(?) You could try to fire a macro from within the same from that (only) changes the value of this text-box to make sure it definitely works there. Of course you'd want to make it work there if it failed before you'd go back to your original problem.
Is the property called value? Could it be text?
Are you sure you need to separate (all) hierarchies using !? Just by desperation: Maybe try using Forms!frmVW.txtXY or Forms.frmVW.txtXY
If that doesn't solve it:
It's often best to reduce your problem to it's very basics. Copy your application (!!!!) and radically delete unneeded stuff. Or start a short experiment from scratch (one or two forms, maybe only a button and a textbox, a macro, most probably not even a single Datatable/Source).

Prevent focus loss while validating Input in JavaFX8 (as in Swing)

I'm trying to do the following. I have a TextField (or any other control) and I want to determine focus loss according to user’s input validation.
I’ve read this article https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification but it seems that JavaFX does not handle focus as Swing does.
What I’m trying to achieve is: “A component's input verifier is consulted whenever the component is about to lose the focus. If the component's value is not acceptable, the input verifier can take appropriate action, such as refusing to yield the focus on the component or replacing the user's input with the last valid value and then allowing the focus to transfer to the next component.”
When a user is focused on a textfield (or any other control) I want to validate user’s input in 3 scenarios:
1) Enter key was pressed (I would listen to the KeyEvent, validate input and, if appropriate, ask to focus on the next control, but I don’t know how to do the latter).
2) TAB key was pressed (I need to intercept the focus change event).
3)Focus is lost (for example by clicking on another control or outside the Stage or even by pressing TAB key)
I need to validate user’s input and decide whether I let focus loss or no. In a way, I need to intercept the focus change event.
I can’t simply listen to de textField.focusedProperty because that only tells me that I’m loosing focus, but I can’t (or at least I don´t know how) stop it from happening.
I tried to get information about focus subsystem in JavaFX but couldn’t find any.
I’d like to know when the engine handles focus events and act according to:
a) The control that is loosing focus (and its content)
b) The possible next control in the focus sequence.
c) If the focus remains in the same Stage o if its send to another Stage or application.
I hope I’ve been clear enough with my explanation and please forgive my English if there are any mistakes.
Thank you very much in advance.

Prevent users from typing and clicking in MS Word Add-in

I am writting a MS Word Add-in, I create a button to run a long time work with text. My work requires that during the time it is excuted, no change can be made by user ( typing, clicking, deleting ...) When this work is completed, user can type and click as usual.
I have used
this.Application.Options.AllowClickAndTypeMouse = false;
However, this doesn't work.
Who can help me!
First, your approach is wrong. The Click and Type is Word functionality and has nothing to do with disabling user Mouse clicks
(For more information about Click and Type refer to here).
Now for your question, here is one approach I would suggest:
If the user is not able to do any action, I would expect an appropriate message, so my idea is to create a modal form by using:
myForm.ShowDialog();
It should do the job and block the active document until it get closed.
I would also add a nice progress bar and "Close" button which you will enable when work has done.

On activate does not fire with my Modal/Pop-Up form to Requery

I have a modal/pop-up form frmEditContact open
on this form there is a combo box full of addressescmbAddressList, populated by a query.
when the user wants to add an address that doesn't exist there is a button that opens the frmAddress where they may add an address.
frmAddress allows them to enter an address to the list and gives the option to save, or cancel (both actions close the form after). with this form closed, the focus is now again on frmEditContact
I would like to repopulate the combo box using docmd.Requery cmbAddressList after they close the other form
I'm not sure where to handle this, I've tried On Avtivate, On Load, On Update, On Open, On Focus...but none of them fire as I keep the frmEditContact open when they are using the other form
is there a way to keep frmEditContact open the whole time, but still an action Event that will fire so that I can Requery? And does On Activate not work with modal forms?
You can requery the combo from frmAddress :
Forms!frmEditContact.cmbAddressList.Requery
I suggest you use the save button to run the requery.
Try this in your frmEditContact code.
DoCmd.OpenForm "frmAddress", acNormal, WindowMode:=acDialog
Me.cmbAddressList.Requery
Since frmAddress is opened in dialog mode, the next line (Requery) will not run until after frmAddress is closed.

windows form validation issues (.net2.0 )

I need help with some things regarding windows form validation:
closing form using standard form closing button (X) triggers validation of all controls.
Is there a way to supress it when closing form using this button, and force it only when pressing my own button?
validation of textbox (possibly other controls, I tested only textboxes) wont invoke when i change text (value) programatically. I need to type text directly into textbox if I want validation to be triggered later, before form is closed. How to tell the form that some control needs validation (but not to trigger it immedidately)?
Thanks.
EDIT:
(1) solved, using this answer.
(2) now, after i set AutoValidate property to false and added ValidateChildren() to my button, only 1 control is being validated with its current value, values of all other controls are revert to value binded to them from DataSource object . I checked it in Validating event - only first control validating keeps its current value, after this validation is finished, other controls' values are replaced with values from DataSource object. I don't understand why. Any clues?
Try this, maybe it could help you. ( for 1)
In the Forms Load event you can put
this.ControlBox = false;. This will hide your X button with the other buttons at the top.
The Form has a Form1_FormClosing event. In that Event you could call the triggers you need. Put a button on the form, and in button_Click event you type this.Close().