Solved

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

Posted on 2004-09-13
8
231 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now