473,701 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is my BeforeUpdate code not working??

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_BeforeUpda te(Cancel As Integer)
If IsNull(Me.cboRe viewDescription ) = True Or
Me.cboReviewDes cription.Value = "" Or Len(Me.cboRevie wDescription) < 1
Then
Call MsgBox("Please choose a description for this review.",
vbExclamation Or vbSystemModal, "Required Data Entry")
Cancel = True
Me.cboReviewDes cription.SetFoc us
Me.cboReviewDes cription.Dropdo wn
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

Apr 16 '06 #1
6 7596
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....bd02708d914487

hth
<tl*****@gmail. com> wrote in message
news:11******** ************@e5 6g2000cwe.googl egroups.com...
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_BeforeUpda te(Cancel As Integer)
If IsNull(Me.cboRe viewDescription ) = True Or
Me.cboReviewDes cription.Value = "" Or Len(Me.cboRevie wDescription) < 1
Then
Call MsgBox("Please choose a description for this review.",
vbExclamation Or vbSystemModal, "Required Data Entry")
Cancel = True
Me.cboReviewDes cription.SetFoc us
Me.cboReviewDes cription.Dropdo wn
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

Apr 16 '06 #2
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.dtmReviewDat e & vbCrLf & vbCrLf & _
Me.txtReviewDes cription & _
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.
:) :)

Apr 16 '06 #3
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!SubformContr olName.Form.Rec ordsetClone.Mov eLast
If Me!SubformContr olName.Form.Rec ordsetClone.Rec ordCount < 1 Then
Me!SubformContr olName.SetFocus
Me!SubformContr olName!FirstCon trolInSubform.S etFocus
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!SubformContr olName.Form.Rec ordsetClone.Mov eLast
If Me!SubformContr olName.Form.Rec ordsetClone.Rec ordCount < 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
SubformControlN ame link for an illustrated explanation.

hth
<tl*****@gmail. com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
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.dtmReviewDat e & vbCrLf & vbCrLf & _
Me.txtReviewDes cription & _
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.
:) :)

Apr 16 '06 #4
Thank you, sorry for the double post...I know about newsgroups...bu t 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

Apr 17 '06 #5
you're welcome :)
<tl*****@gmail. com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
Thank you, sorry for the double post...I know about newsgroups...bu t 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

Apr 17 '06 #6
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/micro...e5a0aaa4057b04

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/micro...e5a0aaa4057b04
If Me!fsubAddNewRe cordReviews.For m.RecordsetClon e.RecordCount < 1 Then
Me.fsubAddNewRe cordReviews.Set Focus
Me.fsubAddNewRe cordReviews!cbo ConsumerID.SetF ocus
Me.fsubAddNewRe cordReviews.For m.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

Apr 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4040
by: William Wisnieski | last post by:
Hello Everyone, Access 2000 I have a form with multiple pages on it. There is one text field on the third page of the form that I need the user to complete before leaving the form or moving to the next record. So, in the BeforeUpdate event of the form itself I have the following code:
4
11535
by: bob bob via AccessMonster.com | last post by:
I'm sure this is a simple one. I have a combo box that, when its data is changed, will trigger a second form to open for input. If the operator declines input, I want the combo box to return to its original value. The on change event is wrong for this, since the new data has already been saved. As I undertand it, the beforeupdate event won't work either, since it applied to the entire form. Would ondirty be the right event to use? ...
9
3382
by: simonmarkjones | last post by:
I want to call a function which does this when the next record button is pressed (calling it from before update) if textboxes are empty then Message box you must fill text box
2
14824
by: PC Datasheet | last post by:
In a form/subform I have an unbound combobox in the form header that sets the value of a field in the subform so that it does not have to be entered for each record. In the BeforeUpdate event of the combobox I have some code to check the value against some criteria and if the new selected value fails the criteria, I cancel the BeforeUpdate event. I need the combobox to return to its previous value. For example, the current value is MyValue...
6
3124
by: lorirobn | last post by:
Hi, I have a form with a continuous subform. I am working on putting validations in for the subform's required fields. Being somewhat new to Access (or rather, an antiquated mainframe programmer), I finally figured out that the place to put the validations is in Form_BeforeUpdate. I have 2 questions: 1) once I determine there is an error and display a message box with
3
3222
by: tlyczko | last post by:
Hello, I think what I'm asking for may not be possible, but I'll try anyway. I have MainForm with 3 subforms, one SubForm requires data entry, the other two don't (optional). MainForm has some BeforeUpdate field validation code. I would like to do the following:
2
2680
by: thatguyNYC | last post by:
Hello-- I have a form that lets the user edit the values in a lookup table. The form is bound to a table and has one textbox on it. The record nav buttons are enabled. On the top half of the form, I have a listbox showing all the records in the table. The user can double-click the listbox to jump to that record via this code: Me.RecordsetClone.FindFirst " = '" & Trim(Nz(Me.lstSaluts)) & "'" If Not Me.RecordsetClone.EOF Then...
7
6861
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and revision number. My table is indexed properly to not allow duplicates, but I would like teh user to be notified that they are typing a duplicate via a message box, then I woulld the update of the record to be cancelled. I have tried the...
11
3467
by: ChipR | last post by:
I have a generic AfterUpdate function applied to my 14 text boxes in a continuous subform, but I can't figure out the syntax to do BeforeUpdate with Cancel. Is this even possible? Private Sub Form_Load() Dim ctl As Control For Each ctl In Me.Controls If Left(ctl.Name, 3) = "Day" Then '??? ctl.BeforeUpdate = "=CheckDay(""" & ctl.Name & """)" ctl.AfterUpdate = "=FixDay(""" & ctl.Name & """)" End If Next ctl
0
8736
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8977
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7823
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6569
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2398
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.