textbox onexit event cancel (1 Viewer)

allanlai

New member
Local time
Tomorrow, 06:50
Joined
Apr 22, 2019
Messages
6
Hi,

I am creating a form for invoice input. This includes a textbox for customer number (tbxCustomerNo) and a read-only textbox (tbxCustomerName) for customer name.

I have a DLOOKUP function in the onexit event of tbxCustomerNo to look up for the customer name from a table. If found, the customer name from the table will be set to tbxCustomerName.text. Otherwise a message box will pop up for a error message and tbxCustomerNo will get focus to force user to enter again.

Of course, there are 2 buttons on the form: btnSave and btnCancel.

The DLOOKUP function run exactly what I want. But when the user entered something the DLOOKUP function cannot found, it forced to stay in tbxCustomerNo even I click the btnCancel.

Seems that the tbxCustomerNo.onexit event do what it should be but it is logically wrong when I click the btnCancel.

What can I do to allow the user to click btnCancel when they had entered something in tbxCustomerNo?
 

essaytee

Need a good one-liner.
Local time
Tomorrow, 08:50
Joined
Oct 20, 2008
Messages
512
Move your code to the AfterUpdate event. That means you can enter the textbox as many times as you like but if you don't make any changes the code will not be triggered. You may also want to read up on the BeforeUpdate event.
 

allanlai

New member
Local time
Tomorrow, 06:50
Joined
Apr 22, 2019
Messages
6
Thanks essaytee for the suggestion.

I tried to move the codes to the AfterUpdate event but it is not performing exactly what I want.

It allows user to click the cancel button but also allow the user to move to other controls after the message box popped up.

What I want is:
If the customerID is valid, nothing happen and freely allow to move to any other controls.
If the customerID is invalid, pop up the message box and force the focus remain in the tbxCustomerID. The ONLY exception is to allow the user to click the btnCancel.
 

essaytee

Need a good one-liner.
Local time
Tomorrow, 08:50
Joined
Oct 20, 2008
Messages
512
There are a number of ways to tackle this scenario.

First, have you considered a combobox in lieu of your textbox where users select the customer required? Might be an easier approach than directly typing a customer number into a text box. The rowsource of the combobox would be based on the customer table, most likely have at least three columns (ID, CustomerNo, CustomerName). Users select by customer name, ID column would be hidden.

If wanting to persist with the textbox approach.

Create a sub procedure, the header as follows:
Code:
Sub SetFormControls (booCustomerFound as Boolean)
In the body of the sub procedure code similar to
Code:
Me.FormControlName.Enabled = booCustomerFound
Repeat for all form controls that need to be adjusted.

In the AfterUpdate event, after your Dlookup code you will know if a record was found or not. Very next line of code call the above procedure, passing in the variable (true/false). The procedure called will either enable all controls or not. Obviously, your Cancel button will always be enabled.

The above is the bare bones of what is required. It can certainly be improved upon with smarter coding.

In the OnLoad event of your form, call the procedure passing in false and that will disable all relevant controls as no customer has yet been selected.
 

allanlai

New member
Local time
Tomorrow, 06:50
Joined
Apr 22, 2019
Messages
6
Thanks essaytee. I use the combo box for a simpler procedure.
 

Users who are viewing this thread

Top Bottom