Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2006, 10:05 AM
ru1 ru1 is offline
Authorized User
 
Join Date: Feb 2005
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Choosing the correct Event Procedure

Hello everyone. I would like to know when placing an edit on a field of a form, where is the best place to do it? Now, if there is a problem, I want to be able to cancel the update. So, do I use the [Before Update] of the field or the [Before Update] of the form? Also, how do I cancel the update and set focus on the field when there is a problem?

For instance, I want to have a first name and last name field. So, I would like to check for the presence and set focus if there is a problem in the [before update] procedure of the form, but Access won't let me set focus there. Can anyone please help?

Thanks,

Chuck [RU1]

 
Old October 31st, 2006, 04:14 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

If you are checking on a new record, you use the Before Insert event.

If you are checking on an updated recoord, use the Before Update event.

If it is a new record, then jusr reset the offending field to blank, and set focus there.

If it is an updated record, then take the oldvalue and put it back in place of the new value.

HTH

mmcdonal
 
Old October 31st, 2006, 04:25 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Those are the form events, not the text box or combo box events. It is better to check on the form events to my way of thinking.



mmcdonal
 
Old October 31st, 2006, 04:30 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Okay, here is a little code. Focus may not be needed.

Dim sLName As String

If IsNull(Me.LName) or Me.LName = "" Then
   MsgBox "Please enter a last name.", vbCritical
   Exit Sub
Else
   sLName = Me.LName
End If

   'Use code here to check sLName for conditions.
   'If wrong,
       'Me.LName = Me.LName.OldValue
   'MsgBox "Wrong Value" etc and Exit Sub

If everything is okay, then none of the conditions will be triggered to exit sub, and the record will be saved.

Of course, you can also build data validation into the table, and Access will throw these errors and reset focus on the offending field.

I use the stuff above on SQL tables with no triggers.


mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Procedure malfunction - duplicating records dstein4d Access VBA 0 April 4th, 2008 03:02 PM
Form Event Procedure Help dbartelt Access 2 June 7th, 2005 04:10 PM
Event Procedure help dbartelt Access VBA 3 May 20th, 2005 05:36 PM
Event Procedure help dbartelt Access 2 May 19th, 2005 03:35 AM
choosing directories joconnor PHP How-To 0 August 11th, 2004 09:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.