473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cancel SetFocus on Text Field When Hitting Exit Command Button

I have a form with required fields. In order to force these requirements on the user I use a message box to alert the user of the requirement followed by a SetFocus back to the required text field. However, I do have a command button that allows the user to basically cancel the data entry session for the form and exit to the Main Menu. How can I override or cancel the SetFocus on the various required fields when the user simply opts to cancel the data entry and exit to the main menu? Because of the SetFocus on these text fields I keep getting the message box alert that the field is required and placed back into the required field as a result fo the SetFocus.

I have copied the VBA for one of the required fields (They are all basically the same) and the VBA behind the "Main Menu" command button below. Any help is much appreciated.

Text Field VBA:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Txt_Cust_Name_Exit(Cancel As Integer)
  2.  
  3. If IsNull(Me.Txt_Cust_Name) Then
  4.  
  5.     MsgBox "This is a Required Field." & vbCrLf & "Please Enter the Customer Name Before Proceeding"
  6.     Cancel = True
  7.     Me.Txt_Cust_Name.SetFocus
  8.  
  9. End If
  10.  
  11. End Sub
Main Menu Command Button:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Cmd_Open_Main_Menu_06_Click()
  2.  
  3. MsgBox "This New Customer Has NOT Been Added"
  4. DoCmd.Close acForm, "Frm_New_Customer", acSaveNo
  5.  
  6. DoCmd.OpenForm "Frm_Main_Menu", acNormal
  7.  
  8. End Sub
Apr 28 '16 #1
2 2084
PhilOfWalton
1,430 Expert 1GB
Several suggestions.
Firstly, I keep the Main Menu open the whole time, because, if you want to look up some information on form 2, and minimise form 1, the menu is there to open form2.

Secondly, I suggest you do all your checking that fields are entered correctly on the Before_Update of the form. At the moment you appear to be checking only that the required fields are filled in. You may also want to check one piece of data against another. As an example, suppose you have a field with a value man, woman child, you may want to check that if it is a child their birthday is entered.

To cancel your entry using a command button try
Expand|Select|Wrap|Line Numbers
  1. Private Sub Btn1_Click
  2.     SendKeys "{ESC}{ESC}"
  3.     ' Optionally close the form here with any message
  4.     ' Optionally open your main menu here
  5. End Sub
  6.  
Phil
Apr 28 '16 #2
zmbd
5,501 Expert Mod 4TB
I personally would AVOID the SENDKEYS command

At one time SENDKEYS was very useful and often the only way to accomplish some tasks; however, it can be very flaky as one cannot always be sure what object has the focus due to the user interaction! Say the user has your application and Word open. I know, 99.9% of the time in this case, most likely not an issue... it's the 0.1% that will cause you the most heartburn.

One option I use is a form level variable that I set for check points such as this:

Open your form in design view.
<alt><f11> to open the VBA Editor Window
Show the code for your form (easy enough, on the left should be the navigation pane, your form should be listed, simply right-click and show code

You should have the:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
lines in this module (and all others IMHO read the trouble shooting section here> > Before Posting (VBA or SQL) Code as for why this is so importaint

Just below these two lines insert
Expand|Select|Wrap|Line Numbers
  1. Private zFailSafe As Boolean
or any other name you'd like to give it

I tend to set this value to False in the form's on_load event just to make sure of the value.

Now in your code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Txt_Cust_Name_Exit(Cancel As Integer)
  2.    If not zFailSafe then 
  3.       If IsNull(Me.Txt_Cust_Name) Then
  4.  
  5.         MsgBox "This is a Required Field." & vbCrLf & "Please Enter the Customer Name Before Proceeding"
  6.         Cancel = True
  7.         Me.Txt_Cust_Name.SetFocus
  8.  
  9.        End If
  10.    end if
  11.  
  12.  End Sub
In your close and open command button set the zFailSafe to True:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Cmd_Open_Main_Menu_06_Click()
  2.  
  3.     zFailSafe = True
  4.     MsgBox "This New Customer Has NOT Been Added"
  5.     DoCmd.Close acForm, "Frm_New_Customer", acSaveNo
  6.  
  7.     DoCmd.OpenForm "Frm_Main_Menu", acNormal
  8.  
  9. End Sub
Now this way, your control's exit event my fire; however, nothing happens if the "Open_Main_Menu" is used to close the form.

>> As for checking the data during entry (using the before_update event)... a very good point and if the form is bound something that one really should consider - I usually do this at the table level for most data validation which helps to keep the form's code cleaner; however, there are some situations that one simply must code for to ensure proper data entry.
Apr 29 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mel | last post by:
is there a way to disable text field history from poping up when the user enters a command that is already in the hitsory cache ? i know i can turn it off from WinDoze preferences. can...
1
by: dhowell | last post by:
I have a form with 3 BOUND text boxes. box1 box2 box3 I want to have a comand button that populates those boxes when clicked. The values are to be looked up via a corresponding product#. ...
3
by: gra | last post by:
Hi Access 2002 I have a Command Buton using some VB code to open an Excel spreadsheet. However, when the spreadsheet opens it doesn't refresh the data. The spreadsheet is a pivot table which...
10
by: dkyadav80 | last post by:
<html> /// here what shoud be java script for: ->when script run then not display all input text field only display selection field. ->when user select other value for institute only this...
1
by: thelouvre | last post by:
I have a little program here that I think would simulate something close to what I'm trying to do. I'm assuming if I can make it work, I can get the big stuff to work. I have a form, that basically...
2
by: cheweedog | last post by:
Can't set the open form by specific data. The form fields don't appear to choose. Trying to link number to number. One form contains a field called number, but does not appear as a choice to <> to...
2
by: lonelylonely | last post by:
how to display in new text field when i add option from drop down list?
10
by: JoeKid09 | last post by:
Hi All, Thanks you in advance for your help. I am working on a form that has text box for Budget information, combo box for location and list box for population which is related to locations combo...
0
by: sreeharshakakar | last post by:
Hie..I am working asp page with java script as script language. In a form, I have two radio buttons stating active or inactive. I have two text fields next to radio buttons containing Date activated...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.