473,434 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

BeforeUpdate event running but record not being saved

I have a form displayed in datasheet view where the user can modify all but a few fields. In the form's BeforeUpdate event, i have the following code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     Dim ctrl As Control
  3.  
  4.     If Me.Dirty Then
  5.         For Each ctrl In Me.Controls
  6.             With ctrl
  7.                 If .ControlType = acTextBox Then
  8.                     Debug.Print .Name
  9.                     If .Value <> .OldValue Then
  10.                         .SetFocus
  11.                         Debug.Print .Name, .OldValue, .Text
  12.                     End If
  13.                 End If
  14.             End With
  15.         Next
  16.     End If
  17.     'Cancel = False
  18.     'DoCmd.RunCommand acCmdSaveRecord
  19. '    DoCmd.Save
  20. End Sub
  21.  
This is a test to see if I can later save the changed values to another table in the same database, When as a user, I modify the data in several fields and then click the arrow navigation button to move to the next or previous record in the datasheet, the code above runs successfully displaying the field names whose values have changed and the changes as well. However, the record remains in edit mode and does not move to the next or previous record as I expected based on the fact I clicked the Next or Previous navigation button, If I press the button again, it saves the record and moves to the Next or Previous record as usual. As you can see in the code, I tried several ideas to force the save and move, but none produced the expected result.

Anyone have any idea why?
Apr 15 '23 #1
1 8507
NeoPa
32,556 Expert Mod 16PB
Hi, and welcome to Bytes.com.

Let's start by saying I'm not absolutely clear on what is causing this behaviour, but I have my suspicions ;-) I'll point out what I see and hopefully that will lead you to :
  1. Something that works.
  2. Something that makes good logical sense.
  1. Line #4 is superfluous. The event procedure can only possibly be triggered (and thus this code run) when there are changes to be saved (IE. Me.Dirty = True).
  2. I would not expect setting the focus to a Control on the current record to cancel the update but it would certainly cancel the movement you requested to another record.

    Maybe it does have the side-effect of cancelling the update too. Try without that element to see if it works.
  3. Lines #17 will neither help nor hurt. Cancel is always set to False when (before) this procedure is entered, and the rest of your code does nothing to change this.

    It seems like a good idea to try it, but I'm not too surprised it didn't help. That said, had you not already tried it I would have suggested it ;-)
  4. Lines #18 to #19 should never be seen within the Form_BeforeUpdate() procedure. They will always cause problems as you are already within the saving process if you're in this code.
  5. For clarity of code, and you've done a pretty good job on that generally, I always recommend (for line #15) that you include the variable name with the Next (IE Next ctrl).
  6. Minor quibble as most people get confused by this - TextBoxes are not Fields. They are Controls that are sometimes (only if bound) associated with Fields.
Let us know how you get on after reading this :-)
Apr 15 '23 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tom | last post by:
Say a form has a subform, a button that pops up a message box and a cancel button. Q1. If data is entered in one or more fields on the main form and the user then clicks the button that opens the...
7
by: todholt | last post by:
Hello, I am trying to bypass a form's automatic update, and instead call a stored procedure in sql server. I am using continuous forms. The problem I am having is with returning to the next...
0
by: Deano | last post by:
My beforeupdate event on the main form fires when a change has been made and the record has not been saved (2 variables are used to track this). Works great but if they make a change in my...
10
by: MLH | last post by:
Would like to examine the value entered into a textbox on an A97 form during the BeforeUpdate event. The textbox may or may not have had an earlier entry in it prior to the latest value that is...
6
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...
0
by: adolph | last post by:
Hi, I have a beforeupdate evbent that tests to see that a customer chosen before the user can enter an amount. If the customer has not been chosen yet, then I would like to popup a message...
8
by: evn678 | last post by:
Hello all, I am trying to handle a scenario where a user clicks the 'X' close control on a form window border. I want to force the user to use the form's 'save' button to write the updated...
16
Seth Schrock
by: Seth Schrock | last post by:
I have some code in the forms BeforeUpdate event that checks if a certain checkbox is checked. If it is not, I have a message appear that says "Do you wish to cancel this transaction?" with VbYesNo...
13
Seth Schrock
by: Seth Schrock | last post by:
I am getting an error when trying to cancel my form's BeforeUpdate event. It says: You can't save this record at this time. ACH Manager may have encountered an error while trying to save a...
1
by: MrYoda1 | last post by:
I’m trying to work out some VBA code to test the condition/value of a couple of checkboxes. I want this code to run in the BeforeUpdate event. I found something similar, just not similar enough. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.