By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

User Input Validation - Data Checking

P: n/a
I am using a routine to check to see if a phone number (PK) has alread
been entered, and takes the user to that record if it is found -- as
follows:

Private Sub Contact_telephone___BeforeUpdate(Cancel As Integer)
Dim rs As DAO.Recordset
Dim iAns As Integer
Set rs = Me.RecordsetClone
rs.FindFirst "[phone] = '" & Me![Contact Telephone #] & "'"
If Not rs.NoMatch Then
iAns = MsgBox("Phone " & Me![Contact Telephone #] & " already
exists;" _
& vbCrLf & "Click OK to go to that record, Cancel to start over",
_
vbOKCancel)
Cancel = True
If iAns = vbOK Then
Me.Undo
Me.Bookmark = rs.Bookmark
Else
Me.Undo
End If
Else
' New Customer -- Just let it be added
End If

End Sub

-- This works as designed when you enter a number that is already in
the database.

The problem is when the customer calls and opens a record with a
different telephone number. What I am trying to do to deal with this
possibility is use the same code (above) for the first name and the
last name -- to at least alert the user that there is a possibility
that the name may already be entered.

The code I am using is similar to the above as follows:

Private Sub LName_BeforeUpdate(Cancel As Integer)
Dim rs As DAO.Recordset
Dim iAns As Integer
Set rs = Me.RecordsetClone
rs.FindFirst "[FName]&[LName] = '" & Me![FName] & Me![LName] & "'"
If Not rs.NoMatch Then
iAns = MsgBox("Last Name " & Me![LName] & " already exists;" _
& vbCrLf & "Click OK to go to that record, Cancel to Continue", _
vbOKCancel)
Cancel = True
If iAns = vbOK Then
Me.Undo
Me.Bookmark = rs.Bookmark
Else
' <<<<<<<<<<<<<<<<<<<< Where problem is
>>>>>>>>>>>>>>>>>>>

End If
Else
' New Customer -- Just let it be added
End If

End Sub

So -- From the top, what happens:
1. Customer, John Smith with telephone 123-456-7890 -- it is entered.
2. Same customer, John Smith calls again with telephone 234-123-4567.
3. Above routine finds John Smith and indicates that the name may be
in the database. User has option to go to that record (OK), or
(Cancel to Continue), Here is the problem: As it is, when you select
Cancel, it stays in the Lastname field and when you try to advance to
the next field, it triggers the BeforeUpdate event again, etc. What
is the best way to set the environment up so that it is the way it was
before the BeforeUpdate event was triggered -- so that the user can
continue to input the record.

Thanks for any help with this.

Robbie Bollinger

What happens is when the first name and the last name is entered.
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
R Bolling wrote:
So -- From the top, what happens:
1. Customer, John Smith with telephone 123-456-7890 -- it is entered.
2. Same customer, John Smith calls again with telephone 234-123-4567.
3. Above routine finds John Smith and indicates that the name may be
in the database. User has option to go to that record (OK), or
(Cancel to Continue), Here is the problem: As it is, when you select
Cancel, it stays in the Lastname field and when you try to advance to
the next field, it triggers the BeforeUpdate event again, etc. What
is the best way to set the environment up so that it is the way it was
before the BeforeUpdate event was triggered -- so that the user can
continue to input the record.

Thanks for any help with this.

Robbie Bollinger


I can't imagine using a phone number as a primary key. If I were
designing your tables, I'd have an autonumber field and a phone number.
But you may have a reason for that.

Are you sure that the first name ALWAYS exists?

You could put your routine in the AfterUpdate event instead and do a
Me.Undo

Here is what I have done in the past when I want to stay in the same field
but clear out the entry so I can move on to the next field if I use the
BeforeUpdate event of the control.
Sub Field_BeforeUpdate(Cancel as integer)
If validationcheckfails then
Cancel = True
Me.Field.Undo
Sendkeys "{Del}"
endif

but you want to move to the record when the validation fails. You could
set a form global variable to true or false at the top of your code
Option Explicit
Option Compare
Dim blnGoOn As Boolean

In your BeforeUpdate, instead of setting Cancel = True, set blnGoTo
If I need to go to other record then
blnGoTo = True
Else
blnGoTo = False
Endif

And in the AfterUpdate event do something like
If blnGoTo then
Me.Undo
...code to go to other record
endif

But you can avoid a lot of this by simply checking in the AfterUpdate
event.


Nov 12 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here's an If...Then construct I use to do the same thing in the
control's BeforeUpdate event procedure. It checks for SSN in the db.
The function "SSN_exists()" returns True/False when the user entered
SSN is already in the db. If the user clicks the "Yes" button on the
MsgBox the routine "RetrievePatient()" changes the form's RecordSource
to retrieve the record w/ the user-entered SSN. The importance of
this If...Then construct is the order in which "things are done..."

1. Is there another record with the data the user just entered?
2. Ask if user wants to get that record.
3. If Yes then Undo the form and set up the form to show the existing
record.
4. If there exists a record w/ just entered data & user doesn't want
to retrieve that record, Cancel the Controls Update.

"Things done" steps are in parentheses.

If SSN_exists(strSSN) Then ' (1)
intAnswer = MsgBox(MSG, vbQuestion + vbYesNo, myCaption)
If intAnswer = vbYes Then ' (2)
Me.Undo ' (3)
Call RetrievePatient(strSSN) ' (3)
End If
Cancel = True ' (4)
End If

End Sub

HTH,

MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP/d8QIechKqOuFEgEQLHgACglrQ8K3407p8mH4SLkJ0lVR0+12MA niGO
6QOM/C67OLCMoG2AP7kG2BPm
=lJVg
-----END PGP SIGNATURE-----
R Bolling wrote:

< SNIP >
The code I am using is similar to the above as follows:

Private Sub LName_BeforeUpdate(Cancel As Integer)
Dim rs As DAO.Recordset
Dim iAns As Integer
Set rs = Me.RecordsetClone
rs.FindFirst "[FName]&[LName] = '" & Me![FName] & Me![LName] & "'"
If Not rs.NoMatch Then
iAns = MsgBox("Last Name " & Me![LName] & " already exists;" _
& vbCrLf & "Click OK to go to that record, Cancel to Continue", _
vbOKCancel)
Cancel = True
If iAns = vbOK Then
Me.Undo
Me.Bookmark = rs.Bookmark
Else
' <<<<<<<<<<<<<<<<<<<< Where problem is

End If
Else
' New Customer -- Just let it be added
End If

End Sub

Robbie Bollinger


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.