Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-13
8
Medium Priority
?
285 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 252 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 248 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

618 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