473,396 Members | 1,707 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,396 software developers and data experts.

MsAccess Form - Preventing of going to a new record after an existing one removed

Hi,

Q: MsAccess Form - AfterUpdate () event procedure - How not to allow a cursor to go to t...

I have the following event procedure:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_AfterUpdate()
  2.   If Nz(Me.ClientName, "") <> "" 
  3.      And Nz(Me.Gender, "") <> ""
  4.   Then 
  5.   Exit Sub
  6.   End If
  7.  
  8.   If Nz(Me.Gender, "") = "" Then
  9.      MsgBox "Select Gender", vbInformation
  10.      Exit Sub
  11.      End If
  12.   End Sub
What do I have to add after the
Message Box "Select Gender" in order to not let user to
go to a new record

I thought of adding something like
GoToNextRecord = False and define it as Integer

But it showed as mistake.

Please modify my code to perform that action
Sorry, I am not a VBA pro.

Thanks in an advance!
Jul 20 '18 #1
4 1393
Nauticalgent
100 64KB
Use the code on the BeforeUpdate event instead of the AfterUpdate and add
Expand|Select|Wrap|Line Numbers
  1.  Cancel = True
after your MsgBox line. The AfterUpdate event fires after you have moved off the current record to a different record or if the form has been refreshed or requeried.

This type of validity check should happen before the record is updated, hence the BeforeUpdate event and not the AfterUpdate.

In other words, move your code to the BeforeUpdate event and delete the AfterUpdate event.

Your code should look like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub FormBeforeUpdate(Cancel As Integer)
  2.     If Nz(Me.ClientName, "") <> "" And Nz(Me.Gender, "") <> "" Then 
  3.          Exit Sub
  4.        End If
  5.  
  6.        If Nz(Me.Gender, "") = "" Then
  7.               MsgBox "Select Gender", vbInformation
  8.              Cancel = True
  9.             Exit Sub ' this could be removed
  10.               Me.Gender.SetFocus ' Recomended
  11.     End If
  12. End Sub
  13.  
This answers your immediate question, however this Sub could be written much better. If you are interested, we can provide a few suggestions
Jul 20 '18 #2
zmbd
5,501 Expert Mod 4TB
Hell666,

Welcome to Bytes.com.

I've moved as much of your question as possible in to your post from the title and added the mandatory [code][/code] tags

To be honest, if you want the [Gender] field to be mandatory, IMHO, your best solution is to implement this at the table level by setting the "required" property to "Yes" and setting up some validation rules to ensure that only valid data is entered, along with a custom error message that triggers from the table. IMHO this works better in that VBA doesn't need to be enabled (don't get me wrong, VBA is my stock and trade with Access databases, but why use a Jack-hammer when a rolled up piece of newspaper will do the job?).


Your second best option would be the Before_Update event of the FORM, not the After_Update

The Before_Update event will allow you to cancel the save or update of the record while allowing the user to [ESC] to abort the record entry or update. Best thing here is that the user can't go back to older entries and attempt to delete the value in that field and save the record.

A VERY simple example would be:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.   If ((Me.Gender & "") = "") Then
  3.     MsgBox "Sorry please enter a Gender for this record"
  4.     Cancel = True
  5.   End If
  6. End Sub
This needs a lot more code such making sure the [ctrl_Gender] receives the focus, only the proper values are entered (ideally, these should be validated at the table level either with validation rules or by design) error trapping, etc...
Attached Images
File Type: png 971095_ReqField.png (14.0 KB, 542 views)
Jul 20 '18 #3
zmbd
5,501 Expert Mod 4TB
@Nauticalgent
OP double posted so I didn't see your answer until I started cleaning up the forum.
-Z
Jul 20 '18 #4
Nauticalgent
100 64KB
No worries! And I like your solution at the table level - much better to prevent a problem then fix it.

And your explanation is more eloquent than mine - I will always be a work in progress, just ask my wife!
Jul 20 '18 #5

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

Similar topics

8
by: ISmith | last post by:
Hi, I am trying to increment a field on a form after a record has been stored. It is a standard fumeric field which may contain some blocks of consecutive values in the table but is not meant to...
1
by: emmaruwa | last post by:
I have a form with two text boxes (in its details section) that pull data from two fields in my database table. This same form also has a button beside the text boxes which is supposed to open...
8
by: anansi | last post by:
Hi I have a form called 'shiftviewer' and it contains a subform called 'all shifts for current month subform'. the subform is in datasheet view (access 2007) and the main form is a column...
10
by: sara | last post by:
Hi - I have been struggling with solution ideas for this now for almost 2 weeks, and have not been able to figure this out. I have a user who creates a Purchase Order (tblPOData). In some...
7
by: kashif khan | last post by:
Hi All, I have created a form in MSAccess and linked that form with a table. now in the run mode if i am entering any thing in the textbox and closing the form with out clicking on add record...
1
by: kashif khan | last post by:
Hi All, I have created a form in MSAccess and linked that form with a table. now in the run mode if i am entering any thing in the textbox and closing the form with out clicking on add record...
4
by: Rotorian | last post by:
Hello, This is a design question not a "how to" I would like know if it can be done before searching for a non existant answer :). If a record is created via a form, can a tab be created on...
1
by: Kent Moss | last post by:
I'm Trying to duplicate the Refresh All Button as Event Procedure in a MSAccess Form. Can you assist.
2
by: Gerald Taylor | last post by:
Please excuse any idiotic questions as Access/VBA is a new area to me. I have inherited an Access Database/Application that is used to sign in and out visitors and Staff to our community Centre....
5
Seth Schrock
by: Seth Schrock | last post by:
I have a form with a subform. To save time, I have set several of the main form controls to have default values so that I don't have to edit them most of the time. The subform is often the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.