Solved

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

Posted on 2004-09-13
8
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
8 Comments
 
LVL 66

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 66

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 66

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

729 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