Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / April 2006

Tip: Looking for answers? Try searching our database.

Why is my BeforeUpdate code not working??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tlyczko@gmail.com - 16 Apr 2006 18:44 GMT
I have a BeforeUpdate where I need to ensure that no matter what, the
first four fields on the form (one text box, 3 combo box lists) have
data entered in them before the user closes the form or before the user
moves off the form to a subform on the main form, regardless of whether
the end user even put focus to any of the four form fields.

(It is easy to know if all 4 fields have values, but not if less than
four fields have values.)

I've been using code like this in the form's Before Update event (only
one example since the code is the same for all four form fields):

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.cboReviewDescription) = True Or
Me.cboReviewDescription.Value = "" Or Len(Me.cboReviewDescription) < 1
Then
   Call MsgBox("Please choose a description for this review.",
vbExclamation Or vbSystemModal, "Required Data Entry")
   Cancel = True
   Me.cboReviewDescription.SetFocus
   Me.cboReviewDescription.Dropdown
   Exit Sub
End If
End Sub

I have the same kind of in the OnExit event of each of the controls I
need to make sure have data in them.

The code seems to work well for some of the fields, but upon clicking a
btnClose (which does nothing more than close the form's window) neither
the form's BeforeUpdate or the controls' OnExit event works properly,
the message box comes up twice (or more) to be clicked OK, but the
focus does not go back to the form, the form closes without saving any
data.

If the end user doesn't put focus to any of the four controls or focus
to any of the controls on the form, I don't care, but if they have
entered data in only 1, 2, or 3 of the required controls, then I need
to make them go back to the control that needs data entry done.
Basically if data was entered in any 1 or more of the four controls, I
have to validate all four control before closing the form or moving to
the subform.

While typing, I realized I could put some tests into the OnClick event
of the close button control, I will try that now as well.

I've been trying different things for the past day, sigh...I haven't
found anything on the NGs about why my particular code seems to keep
failing, I tried re-compiling, the table for the form's query has the
four fields set to required and not to allow zero length...

Thank you, Tom
tina - 16 Apr 2006 18:57 GMT
when you have code running in the form's BeforeUpdate event of a form, and
you use a command button to close the form, you need to explicitly save the
current record in the command button's procedure, *before* the Close command
runs - and then trap the error that results when the BeforeUpdate event is
cancelled.

take a look at my explanation and sample code at
http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/110755
9658a6541d/54bd02708d914487?lnk=st&q=DoCmd.Close+group%3Acomp.databases*+author%
3Atina&rnum=1&hl=en#54bd02708d914487


hth

> I have a BeforeUpdate where I need to ensure that no matter what, the
> first four fields on the form (one text box, 3 combo box lists) have
[quoted text clipped - 48 lines]
>
> Thank you, Tom
tlyczko@gmail.com - 16 Apr 2006 19:15 GMT
Yes, I added this code to my btnClose OnClick event, and it appears to
do what I need.

I have one more question, if you (or someone else) don't mind.

After I validate that the four fields have data in them, I do this
code, to make sure they have entered the "right/correct" and I ask them
to 'double-check':

If MsgBox("The values you selected are:" & vbCrLf & _
   vbCrLf & Me.dtmReviewDate & vbCrLf & vbCrLf & _
   Me.txtReviewDescription & _
   vbCrLf & vbCrLf & Me.cboProgramID.Column(0) & vbCrLf & vbCrLf &
Me.txtLocation & vbCrLf & vbCrLf & _
   "Are these correct? Do you want to add them?" & vbCrLf & _
   "Please be SURE before you add them." _
   , vbQuestion + vbYesNo, "Add to database?") = vbNo Then
   Cancel = True 'return to editing form, these values are not yet
locked
   'Exit Sub
End If

This all works very well...but it also makes it possible to close the
form WITHOUT putting at least one record's worth of data (the visible
fields) into this form's SUBFORM.

Can I use similar BeforeUpdate code in the SUBFORM to ensure that I
have data in the first two visible fields on the first new record the
main form's subform?????

If this isn't making any sense, please tell me...I'm now going to
consult Access Hacks and Access Annoyances. :)

Thank you, Tom

P.S. You have a lot of stars by now!! :) :) You're very good at this.
:) :)
tina - 16 Apr 2006 23:31 GMT
well, let's see if i understand the situation. you're able to validate the
data in the main form to your satisfaction before the current record is
saved, and you're satisfied with how that's working for you, correct? but
you find that the user can enter data in the main form and then close the
form - without entering any records in the subform, correct? you want to
force the user to add at least one record to the subform, before closing the
main form - or moving to another record in the main form, correct?

well, that can get a little tricky - trying to force the user into adding a
record to a subform, while at the same time not boxing the user into a
situation where s/he has to add a record *even when it's not appropriate*.
i'd probably start out by trying the following:

add code to the main form's AfterUpdate event, to count the number of
records in the subform. if it's less than 1, move the focus to the subform,
and then to the first record in the subform. something like

   Me!SubformControlName.Form.RecordsetClone.MoveLast
   If Me!SubformControlName.Form.RecordsetClone.RecordCount < 1 Then
       Me!SubformControlName.SetFocus
       Me!SubformControlName!FirstControlInSubform.SetFocus
   End If

then check the recordcount again, on the subform control's Exit event, and
cancel the Exit if there are no records in the subform. something like

   Me!SubformControlName.Form.RecordsetClone.MoveLast
   If Me!SubformControlName.Form.RecordsetClone.RecordCount < 1 Then
       Cancel = True
       Msgbox "Enter a record in the subform, please."
   End If

note that in both instances, the code is running in the *main* form, not the
subform. also, if there are no records in the subform, and yet you do *not*
get an error message when you click the Close command button, then you'll
have to put the AfterUpdate event code in the Close button's event
procedure, as well.

if you're not clear on the difference between a subform *control* and a
subform *form object*, go to
http://home.att.net/~california.db/instructions.html and click the
SubformControlName link for an illustrated explanation.

hth

> Yes, I added this code to my btnClose OnClick event, and it appears to
> do what I need.
[quoted text clipped - 33 lines]
> P.S. You have a lot of stars by now!! :) :) You're very good at this.
> :) :)
tlyczko@gmail.com - 17 Apr 2006 11:59 GMT
Thank you, sorry for the double post...I know about newsgroups...but I
thought my question might get lost, and it's something I'm quite behind
on...and I thought if I explained better a second time, I might have a
better chance of getting an answer. Your answer is great! :)

I knew I had to work with the events but not well enough where/which
events, your explanation was a big help.

I do try a lot of things before I post...

Thank you, Tom
tina - 17 Apr 2006 15:08 GMT
you're welcome  :)

> Thank you, sorry for the double post...I know about newsgroups...but I
> thought my question might get lost, and it's something I'm quite behind
[quoted text clipped - 7 lines]
>
> Thank you, Tom
tlyczko@gmail.com - 17 Apr 2006 16:25 GMT
I struggled for about an hour with your suggestion, which made perfect
sense...
It kept not working, then I searched around some more with different
phrases and found this link:

http://groups.google.com/group/microsoft.public.access.forms/tree/browse_frm/thr
ead/ab25c45ed525d018/3cc7ccc0fa76d98e?rnum=1&q=force+subform+data+entry&_done=%2
Fgroup%2Fmicrosoft.public.access.forms%2Fbrowse_frm%2Fthread%2Fab25c45ed525d018%
2F3cc7ccc0fa76d98e%3Ftvc%3D1%26q%3Dforce+subform+data+entry%26#doc_d6e5a0aaa4057
b04


It talks about using the subform's Dirty property...

So I have in After Update:
'4/17/06 set subform to dirty and set focus to it, this prevents
leaving the form
'4/17/06 must disable close button on the window for this to work,
however
'4/17/06 reference link:
http://groups.google.com/group/microsoft.public.access.forms/tree/browse_frm/thr
ead/ab25c45ed525d018/3cc7ccc0fa76d98e?rnum=1&q=force+subform+data+entry&_done=%2
Fgroup%2Fmicrosoft.public.access.forms%2Fbrowse_frm%2Fthread%2Fab25c45ed525d018%
2F3cc7ccc0fa76d98e%3Ftvc%3D1%26q%3Dforce+subform+data+entry%26#doc_d6e5a0aaa4057
b04

If Me!fsubAddNewRecordReviews.Form.RecordsetClone.RecordCount < 1 Then
Me.fsubAddNewRecordReviews.SetFocus
Me.fsubAddNewRecordReviews!cboConsumerID.SetFocus
Me.fsubAddNewRecordReviews.Form.Dirty = True
MsgBox "testing form.dirty"
End If

I am still testing, but it seems to work as necessary.
I only test to make sure they have at least one consumer record in the
subform, since I can't know in advance how many sub-records there
should be...

Thank you again, Tom
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.