Solved

Forcing data entry in Form for a Required Field, and leaving form ignoring the Code

Posted on 2004-09-13
8
256 Views
Last Modified: 2012-06-21
I have this situation.  I am Currently using this code to force data entry in a required field in a form.  This works fine, but now the problem I am running into is when I click on the Exit Button.  This is the Code:

Private Sub CODREN_LostFocus()
If CODREN.Text = "" Then
MsgBox "Please provide the Contract Number to continue", vbOKOnly, "Warning"
Flag = True
Else
Flag = False
End If
End Sub

Then in the Next field I use this:

Private Sub RENREN_GotFocus()
If Flag = True Then
CODREN.SetFocus
End If
End Sub

Now, when I click in the Button to exit the form, the Message "Please provide the Contract Number to continue" shows up, and then after I click Ok the form closes.  I would like to prevent this from happening.  I was trying to introduce another If statement in the code where if the Exit button was click it would ignore the message, but I wasn't able to do it.  Any help would be highly appreciated.
Thanks in advance.
Juan
0
Comment
Question by:camilo_gs
  • 3
8 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 63 total points
ID: 12051115
GotFocus and LostFocus are not the best events to handle data validation in a form.  

Better to do this in the Form BeforeInsert event:  If the value is good, allow the update to happen, and if the value is not good, then Cancel = True to cancel the update.

Hope this helps.
-Jim
0
 
LVL 27

Expert Comment

by:jjafferr
ID: 12052571
Hi camilo_gs,

place Exit Sub at the end of your if statement, before docmd.close
this will not allow the code to continue, it will break it before it reaches docmd.close

Hope this helps

Jaffer
0
 
LVL 39

Assisted Solution

by:stevbe
stevbe earned 62 total points
ID: 12053741
I like using the BeforeUpdate event ...

Private Sub Form_BeforeUpdate(Cancel As INteger)
    If CODREN.Text = "" Then
        MsgBox "Please provide the Contract Number to continue", vbOKOnly, "Warning"
        Me.CODREN.SetFocus
        Cancel = True
    End If
End Sub
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 12166811
Looks like you have some solid advice to your question.  Please award points and close the question.  Thanks in advance.  -Jim
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 14127689
A couple of testing iterations would be nice here to see if our comments (all of which make sense to me) would solve her issue(s).
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question