Karen,
Using the before update to validate a field/value means that if you dont
want to save the changes, you can set the Cancel variable passed in to =
True and the event will automatically cancel (this applies to any control
including forms). For e.g.
Pivate Sub MyTextBox_BeforeUpdate(Cancel As Integer)
' Check a condition
If Me.MyTextBox = "Hello" Then
' Cancel the update
Cancel = True
' Undo the text box
Me.MyTextBox.Undo
End If
End Sub
In the above, the name of the text box is MyTextBox. If the user enters
Hello then the update event gets cancelled. If you didnt include the undo
line then if you pressed enter it would seem like nothing is happening. You
could display a message saying "Do not enter Hello" or something like that
e.t.c. If you use Me.Undo, this will undo ALL changes on the form (to the
last successful save). Specify the control as well and this will undo
changes made to the control only.
Going on your description of what you are trying to do, I would first ask
the user if they want to use the name they have entered before displaying
the form. For e.g.
Pivate Sub MyTextBox_BeforeUpdate(Cancel As Integer)
Dim intResponse As Integer
' Code goes here to check if there is a match already
' Ask the user if they want to use the value they have entered
intResponse = MsgBox("The name you have entered may already exist. Would
you like to view all possible matches?" _
, vbYesNo + vbQuestion, "Display possible
matches?")
' Check the response
If intResponse = vbYes Then
' Cancel the update
Cancel = True
' Undo the users input
Me.MyTextBox.Undo
' Code goes here to open form and view results
End If
End Sub
Hope this gives you an idea of how you could solve your problem. I could
give you an example of how to find an existing match if you need it.
Neil.
Karen said:
Hi Neil,
When are you checking for duplicate values - in the forms before update or
the textbox's before update event? I would recommend the textbox beforeupdate
event.
I am checking in the afterupdate event, I can try the beforeupdate event.
In either case, the record has not been saved at this point. Can you
educate me on the advantage of checking beforeupdate rather than
afterupdate?
Also, what do you mean by "I check the database to see if the name may
be
a
duplicate". Does this mean if someone enters Smith, you check to see if
Smith has already been entered and if so, you use this name instead?
Exactly. Actually, I present the user with all of the names which may be a
match based on soundex of the last name.
At first glance, I would recommend using a combobox to lookup all second
names and a user then has to pick one. If they enter a name that isn't in
the list Access can then add this name to the combobox drop-down list.
(there are many questions and replies on the google newsgroups as to how to
do this - just search for "NotInList").
I decided not to use this route because the combobox really has to display
the full name for the user to recognize a match and somehow that just seemed
like it would be confusing.
Thanks for your suggestions.
Karen
Neil said:
Hello Karen,
I have a few questions before I can suggest a solution to your problem
When are you checking for duplicate values - in the forms before update or
the textbox's before update event? I would recomend the textbox beforeupdate
event.
Also, what do you mean by "I check the database to see if the name may
be