473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting error when using Me.Dirty = False

Seth Schrock
2,965 Recognized Expert Specialist
I have some code on a form that asks you if you want to save the changes to the record before moving on to the next record or closing the form. The code is in the form's BeforeUpdate event. This is what I have currently:

Expand|Select|Wrap|Line Numbers
  1. If intResponse = vbCancel Then
  2.      DoCmd.RunCommand acCmdUndo
  3. Else
  4.      Docmd.RunCommand acCmdSave
  5. End If
This works. However, the acCmdSave is very slow; usually about 2 - 3 seconds. However, I also have a Save button that uses:

Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty = True Then Me.Dirty = False
This works in about .25 - .5 seconds. My idea was to use this same code in the form's BeforeUpdate event in place of the DoCmd.RunCommand acCmdSave. When I do, I get the following error message:

Run-time error '2115':
The macro of function set to the BeforeUpdate or ValidationRule property for this field is preventing IT Inventory from saving the data in the field.

I checked and there is no validation rule on the field that was changed. Here is the entire BeforeUpdate event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Dim strMsg As String
  3. Dim intResponse As Integer
  4.  
  5. If intSaved = 0 Then
  6.     strMsg = "you have made one or more changes to this Record.  Do you wish to save this record" _
  7.              & "with those changes?" & vbCrLf & vbCrLf & "Click OK to save changes, or Cancel to" _
  8.              & "Undo these changes."
  9.  
  10.     intResponse = MsgBox(strMsg, vbQuestion + vbOKCancel + vbDefaultButton1, "Prompt to Save Record")
  11.  
  12.     If intResponse = vbCancel Then
  13.         DoCmd.RunCommand acCmdUndo
  14.     Else
  15.         'DoCmd.RunCommand acCmdSave
  16.         Me.Dirty = False
  17.  
  18.     End If
  19.  
  20. Else
  21.     intSaved = 0
  22. End If
  23.  
  24. End Sub
Just to let you know, intSaved is publicly declared at the top of the form's VBA page. I use it to know if the Save button has been clicked (which sets the variable to 1).

What is wrong with my code?
Nov 14 '12 #1
9 9591
zmbd
5,501 Recognized Expert Moderator Expert
Think of it as a record locking conflict.
The Before Update event has fired.
Locking the record for validation.
Then If Me.Dirty = True Then Me.Dirty = False line executes in your event...
Because there is a difference between the old and new values this line of code causes the Before Update Event to fire again and when the second itteration attempts to lock the record it finds the record is already locked and thus you get the error message.
:)
(you can see the order of trip by using a debug.print "eventname" in the before insert, after insert, before update, after update etc... there's also a list of event order: Form Objects
Nov 14 '12 #2
Seth Schrock
2,965 Recognized Expert Specialist
Then why does it work with the save button? Because the BeforeUpdate event is bypassed with the intSaved variable set to 1? Does acCmdSave not lock the recordset? Or is that why the difference in time?

A lot of questions :) I just want to understand how this works so that I can use each way in the most advantageous way.
Nov 14 '12 #3
Seth Schrock
2,965 Recognized Expert Specialist
In looking for other methods of saving a record using VBA, I found an article that says that acCmdSave doesn't save the record, "acCmdSave saves *design changes to the structure of the object*" Alternative to acCmdSave. It also says that I should use acCmdSaveRecord instead. Is this correct? If so, then I would expect that acCmdSaveRecord would have the same issue as Me.Dirty = False. I will test this out shortly. At least I know what the issue is and what I need to work around.
Nov 14 '12 #4
Seth Schrock
2,965 Recognized Expert Specialist
Just tested acCmdSaveRecord. I got the same error message.
Nov 14 '12 #5
zmbd
5,501 Recognized Expert Moderator Expert
Seth,
Without knowing your form it's hard to give you too much...

1) acSave - yes, technically, it is for saving the form, table, etc... it will most often force a save on a dirty record; however, not always.

2) acSaveRecord - yes, technically, it is the one you more than likely should be using. This will cause the same error as you've already seen for the reason given in #2

3) If you are in the before update event, the record is already attempting to be saved... trying to resave it in this event seems, well, odd. However, I don't know the design of your form...

4) Code to check if the form is dirty before close is a good idea. I usually work around that by locking all of my record forms and using the footer as a "new" record or "Edit" current record zone that has a "save" button (you've seen that I think in one of the other threads yes?), which leads to...

4a) You can place your command button to save the record and in the onclick event use the If Me.Dirty = True Then Me.Dirty = False You can have the button show up in the on-dirty event and hide itself when clicked or in the after update event. Nice thing here is that if the record isn't dirty (new or old) usually no errors. It will cause the before-insert, after-insert, before-update events to fire (all if new record just update if old) so that you can use validation code. I will do this in my "management" level forms as a visual... the caption reads "SAVE Changes?" and is usually Bright-Red letters on Light-Blue background.... hurts the eyes due to colour contrast!
Nov 14 '12 #6
Seth Schrock
2,965 Recognized Expert Specialist
So basically, all I need to do is if the answer to the messages box is not vbYes (the else portion) then just exit sub and the database will save the record for me.

And yes, I have seen your New and Edit portion in the form footer.

Edit: I just tried taking out the Else portion of my code and it works just fine. The records are saved just like I need them to be and very quickly too.
Nov 14 '12 #7
zmbd
5,501 Recognized Expert Moderator Expert
Yep... you got it then.
One thing I would do is just before the undo command (between lines 12 and 13 in OP is insert Cancel=True by setting this true, you're telling Access to abort the changes and handle everything else related there to; however, make sure you leave the undo command in there to revert the record.
If you set it false, then you need to make sure that the control handles all of the changes.
Nov 14 '12 #8
Seth Schrock
2,965 Recognized Expert Specialist
Setting Cancel=True would stop the move to another record or closing of the form, correct?
Nov 14 '12 #9
zmbd
5,501 Recognized Expert Moderator Expert
Yes/No... yes if the event is triggered from a record move then setting the cancel to true should prevent the record movement (notice I used the word "should").
If the event was triggered do to a form close then it will prevent the record update however I the form will close; however, access should (notice I used the word "should" again) warn the user that edits will be lost.
Nov 14 '12 #10

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

Similar topics

1
by: Dermot O'Loughlin | last post by:
Can anyone help me with this? I get the following error when using the odbc adapter wizard in VS.Net 2003: --------------------------- Wizard Form --------------------------- An unexpected...
1
by: dipesh | last post by:
I am getting error when trying to Add Web Reference in Visual Studio 2005. "Some of the files required for web references are not installed"
0
by: Charlie | last post by:
Hi: I get a TreeNode binding error when using Data property of XmlDataSource control. When I switch to TextFile property and point to xml file on hard drive, problem goes anyway. I'm...
1
by: siddu | last post by:
Hi All, I have to do one task. First i need to copy files from vss and run bat files.i have a document with all steps. step 1: we have to take files from vss by using getlatestversion...
0
by: =?Utf-8?B?TG93bGFuZGVy?= | last post by:
Hello, I've built a web application that uses client script callbacks. It is used on a large network with a large variety of user OSes and IE versions. It was tested on IE 6 on different setups...
1
by: jonny | last post by:
Went from using Visual Web Develop express to Visual Studio 2005 and getting error when trying to open project. Error message: "One or more projects in the solution could not be loaded for the...
8
by: msnews.microsoft.com | last post by:
Our IT team relocated our application servers yesterday. They had to take one machine out of the cluster because it would prompt users for credentials if accessed using the IP address, which I...
7
by: svsenthilkumar | last post by:
Hai, I got an javascript error when using ajax in mozilla browser, anyone help me why the error is coming, meaning for this error and how can i rectify this error. Error: " nsresult:...
0
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I have WCF service and integrated with STS (WS-Federation). The Service exposes 5 endpoints Win, UNT and Federated Windows, Federated UNT and Mex endpoint. My STS Service exposes 3 endpoints...
4
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
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?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.