Rister
Rister

Reputation: 99

Using VBA to Update a form recordset in Access 2007

I'm doing some development in access and I'm running into an issue where I need to make sure that a form is updated in a buttons OnClick handler.

I was thinking that it might work something like this:

if me.dirty then
    me.update     'This is a nonexistent form method'
end if
<rest of handler>

If such a thing exists, will I have to call the OnUpdate Event handler manually?

Upvotes: 2

Views: 5022

Answers (4)

Bruce Munck
Bruce Munck

Reputation: 31

I am glad that I stumbled onto this post, even if it is at such a late date. I was unaware that this is a bug in Access and I just assumed that I was doing something wrong that was causing me to lose data (well, part of the time anyway) when I closed a form. Through some trial and error I discovered that forcing the recordset in the form to go to the next record just before closing the form would save my data. I used the following code in my Exit_Click subs:

DoCmd.GoToRecord , , acNext

Now I am changing over to setting <me.dirty = false> since that seems to be the preferred method. Who knows, there may be some cases where moving the record pointer doesn't work.

Upvotes: 0

David-W-Fenton
David-W-Fenton

Reputation: 23067

I would avoid the .RunCommand version because there are cases where Me.Dirty = False will work and access to the menu commands is prevented.

On the other hand, Me.Dirty has always struck me as a property that ought to be read-only, but it's not.

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91376

How about:

if me.dirty then
    me.dirty=false 
end if

Code as per Allen Browne, MVP:

http://allenbrowne.com/bug-01.html

Upvotes: 2

DJ.
DJ.

Reputation: 16257

I always use this code in my Save_Click handlers

If Me.Dirty Then    
  DoCmd.RunCommand acCmdSaveRecord   
End If

Upvotes: 0

Related Questions