Change display in OnCurrent event not working (1 Viewer)

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
I have a form with a "Make Copy" button. The database has many records which are very similar in content. This button allows the user to select a record similar to what he wants to enter, make a copy and edit the copy, rather than having to type everything every time. This all works fine.

In order to help cut down confusion, when the duplicate record is created and displayed, I make visible the label border for every field that is filled by code, then when the user exits that field on the form, I hide the label border again. This provides a visual record of which fields have been visited (and possibly changed). This works great as well.

When the user finishes modifying the record, I clear any remaining visible borders in the form's AfterUpdate event, so that all borders are invisible again when the user goes to another record. Once again, all is good.

My problem is when the user leaves the freshly created record -without- having made any changes. There are no changes made, so the AfterUpdate event doesn't fire, and the cautionary borders remain visible. I tried clearing them in the OnCurrent event when the next record comes in, but the code simply doesn't work there. No errors, but nothing happens, the borders remain. Even when I put in a breakpoint and step the code, the borders do not get cleared. Even when I issue the appropriate command in the immediate window, nothing happens, if I do it while within the bounds of the paused OnCurrent event code. The exact same code, running or immediate works fine when called from anywhere else.

I'm using A2007 under Vista. No net search has brought up any posts about this, or at least I haven't found any, but keywords addressing this are common and results number in the hundreds of thousands.

Is there something special about the OnCurrent event that prevents screen updates? Is there another way to clear these borders when leaving an unmodified record? Might there be a better way to do the whole thing?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:13
Joined
Sep 12, 2006
Messages
15,613
how does the user leave the record without making changes?

there should be a current event for the next record

however - is your form unbound? If so, that might mean you dont get a current event
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
how does the user leave the record without making changes?
My code creates a new record and populates the fields on the form, so the user is left sitting on a newly created record. If he then clicks one of the navigation buttons, or presses PageUp/PageDn, he moves to another record, without having made any changes to the newly created record, so the AfterUpdate event does not fire.

there should be a current event for the next record
There is and it fires correctly. If you look at my post again, you'll see that I write about using it and stopping code execution in it.

however - is your form unbound? If so, that might mean you dont get a current event
No, the form is bound, and again, the event DOES fire, as I write in my original post. The problem is that the code doesn't do anything, if it's called from the OnCurrent event. Yes, it's executing, I have put in breakpoints to verify that it is. Other stuff that I do in the OnCurrent event works fine, and this same code called from other places works fine, just the changing of the label border properties -FROM THE ONCURRENT EVENT- doesn't do anything.
 

missinglinq

AWF VIP
Local time
Today, 04:13
Joined
Jun 20, 2003
Messages
6,423
Don't understand why the Form_Current event isn't working here, but if it were working and this meets your needs, you should be able to reset the formatting in the Form_BeforeUpdate event. This event fires if a record is saved, whether it is physically populated thru the keyboard or thru code.

Also, if no changes are made to the record after its creation, aren't you, ipso facto, creating a totally duplicate record?

Linq ;0)>
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
Don't understand why the Form_Current event isn't working here, but if it were working and this meets your needs, you should be able to reset the formatting in the Form_BeforeUpdate event. This event fires if a record is saved, whether it is physically populated thru the keyboard or thru code.
I don't understand why either, that's why I'm here, hoping that someone else does. The BeforeUpdate event has the same shortcomings as the AfterUpdate event - it doesn't fire if the user navigates away from the record without making any changes.
Also, if no changes are made to the record after its creation, aren't you, ipso facto, creating a totally duplicate record?

Linq ;0)>
Yes, that can happen. There is nothing in this database that prevents the creation of identical records (except for the autonumber primary key). It's not common, but it is a perfectly legitimate occurrence.
 

spikepl

Eledittingent Beliped
Local time
Today, 09:13
Joined
Nov 3, 2010
Messages
6,144
"No, the form is bound, and again, the event DOES fire, as I write in my original post. The problem is that the code doesn't do anything," How do you know? Have you checked what the acutal value of the property that you set is at the beginning and at the end of the OnCurrent- code? One could theorize that the code runs fine, but the property is unexpectedly set elsewhere afterwords.

In the debugger, you could put a Watch on the property, to see if it gets changed when expected/desired
 

spikepl

Eledittingent Beliped
Local time
Today, 09:13
Joined
Nov 3, 2010
Messages
6,144
Besides - have you tried .Repaint?
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
"No, the form is bound, and again, the event DOES fire, as I write in my original post. The problem is that the code doesn't do anything," How do you know? Have you checked what the acutal value of the property that you set is at the beginning and at the end of the OnCurrent- code?
Did you read my original post? Yes, I -KNOW- it doesn't do anything, because I put a breakpoint into the OnCurrent event code, and stepped the code manually. I have executed the code step by step, and examined both the screen and the actual values in the immediate window right after executing the commands to change the property. It doesn't change. Even when I type the command directly into the immediate window while the OnCurrent event code is paused, the value doesn't change. All this was in my original post.
One could theorize that the code runs fine, but the property is unexpectedly set elsewhere afterwords.
One could, but that's not what's happening. The property value simply doesn't change.
In the debugger, you could put a Watch on the property, to see if it gets changed when expected/desired
I have - it doesn't.
Besides - have you tried .Repaint?
Yes, but since the property value doesn't change, Repaint doesn't help. It repaints the screen, but with the unchanged values.
 

missinglinq

AWF VIP
Local time
Today, 04:13
Joined
Jun 20, 2003
Messages
6,423
The BeforeUpdate event has the same shortcomings as the AfterUpdate event - it doesn't fire if the user navigates away from the record without making any changes.
Not true! The form's BeforeUpdate event, Form_BeforeUpdate, fires any time a record is saved, even if the only data in the record has been populated thru code!

Linq ;0)>
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
Not true! The form's BeforeUpdate event, Form_BeforeUpdate, fires any time a record is saved, even if the only data in the record has been populated thru code!

Linq ;0)>
Well, I tried it this morning, put a breakpoint into the event handler. Pressed the 'Make Copy' button, let the code run to make the new record, then moved to another record. Neither the BeforeUpdate nor the AfterUpdate event fired. When I do the same thing, but make any sort of manual change to any of the fields before moving away, both events fire (both stop at the defined breakpoints and display the VBE).
 

missinglinq

AWF VIP
Local time
Today, 04:13
Joined
Jun 20, 2003
Messages
6,423
I just
  • Put a messagebox in the Form_BeforeUpdate event
  • Made a new record
  • Populated fields on the new record with code
  • Did nothing else to the record
  • Navigated away to another record and the message box appeared, as expected, showing that the event had fired.
Maybe we need to look at the code behind your 'make a copy' button.
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
I just
  • Put a messagebox in the Form_BeforeUpdate event
  • Made a new record
  • Populated fields on the new record with code
  • Did nothing else to the record
  • Navigated away to another record and the message box appeared, as expected, showing that the event had fired.
Maybe we need to look at the code behind your 'make a copy' button.

Okay, here's the 'MakeCopy' code, called from a ribbon button, hence the explicit reference to the form. Sets a clone pointer to the current record in the form, adds a new record to the form's recordset, then copies everything from the clone to the current new record. The last line calls the routine to make the label borders visible, which routine is reproduced further down. FWIW, the next-to-last line is my current work-around, to force the record to be dirty, that way any movement away triggers the update events, regardless of whether the user made any changes or not.

Code:
Public Sub Kopirovat(control As IRibbonControl)
Dim rst As DAO.Recordset
With Form_Data
    Set rst = .RecordsetClone
    With .Recordset
        .AddNew
        .Fields("DruhAutoID") = IIf(IsNull(rst.Fields("DruhAutoID")), Null, rst.Fields("DruhAutoID"))
        .Fields("RokStary") = IIf(IsNull(rst.Fields("RokStary")), Null, rst.Fields("RokStary"))
        .Fields("MesicStary") = IIf(IsNull(rst.Fields("MesicStary")), Null, rst.Fields("MesicStary"))
        .Fields("DenStary") = IIf(IsNull(rst.Fields("DenStary")), Null, rst.Fields("DenStary"))
        .Fields("RokNovy") = IIf(IsNull(rst.Fields("RokNovy")), Null, rst.Fields("RokNovy"))
        .Fields("MesicNovy") = IIf(IsNull(rst.Fields("MesicNovy")), Null, rst.Fields("MesicNovy"))
        .Fields("DenNovy") = IIf(IsNull(rst.Fields("DenNovy")), Null, rst.Fields("DenNovy"))
        .Fields("CharVyskyt") = IIf(IsNull(rst.Fields("CharVyskyt")), Null, rst.Fields("CharVyskyt"))
        .Fields("KatastrAutoID") = IIf(IsNull(rst.Fields("KatastrAutoID")), Null, rst.Fields("KatastrAutoID"))
        .Fields("OkresAutoID") = IIf(IsNull(rst.Fields("OkresAutoID")), Null, rst.Fields("OkresAutoID"))
        .Fields("RegionAutoID") = IIf(IsNull(rst.Fields("RegionAutoID")), Null, rst.Fields("RegionAutoID"))
        .Fields("Kvadrat") = IIf(IsNull(rst.Fields("Kvadrat")), Null, rst.Fields("Kvadrat"))
        .Fields("OblastAutoID") = IIf(IsNull(rst.Fields("OblastAutoID")), Null, rst.Fields("OblastAutoID"))
        .Fields("Lokalita") = IIf(IsNull(rst.Fields("Lokalita")), Null, rst.Fields("Lokalita"))
        .Fields("Zdroj") = IIf(IsNull(rst.Fields("Zdroj")), Null, rst.Fields("Zdroj"))
        .Fields("Pocet") = IIf(IsNull(rst.Fields("Pocet")), Null, rst.Fields("Pocet"))
        .Fields("Elevation") = IIf(IsNull(rst.Fields("Elevation")), Null, rst.Fields("Elevation"))
        .Fields("Poznamka") = IIf(IsNull(rst.Fields("Poznamka")), Null, rst.Fields("Poznamka"))
        .Fields("ProtokolCislo") = IIf(IsNull(rst.Fields("ProtokolCislo")), Null, rst.Fields("ProtokolCislo"))
        .Fields("ProtokolRok") = IIf(IsNull(rst.Fields("ProtokolRok")), Null, rst.Fields("ProtokolRok"))
        .Update
    End With
End With
Form_Data.Dirty = True
Form_Data.SetBrdrsVisible True
End Sub

Here's the code to make the label borders visible or not. Earlier I tried changing the border style to transparent, when that didn't work, I tried changing the border color to same as the form background, but that isn't working either. This exact routine, when called from the update events, either Before or After, works fine, but when called from the OnCurrent event, simply refuses to work. Breakpoints set, code pauses, I step through something like

lblElevation.BorderColor = i

and examine the value of lblElevation.BorderColor immediately after in the immediate window - it has NOT been set to i. Even when I execute that exact command in the immediate window, it has no effect, if I do it from within the bounds of the paused OnCurrent event code. Called from anywhere else, it works fine.

Code:
Public Sub SetBrdrsVisible(vis As Boolean)
Dim i&
i = IIf(vis, ShowBorder, HideBorder) ' Constants from Global module.
cboDruh.Controls(0).BorderColor = i
cboKatastr.Controls(0).BorderColor = i
lblKvadrat.BorderColor = i
lblElevation.BorderColor = i
cboOblast.Controls(0).BorderColor = i
txtLokalita.Controls(0).BorderColor = i
lblOd.BorderColor = i
lblDo.BorderColor = i
lblChrVyskyt.BorderColor = i
txtZdroj.Controls(0).BorderColor = i
txtZapsal.Controls(0).BorderColor = i
txtZapsano.Controls(0).BorderColor = i
lblPocet.BorderColor = i
lblProtokol.BorderColor = i
txtPoznamka.Controls(0).BorderColor = i
End Sub
 

missinglinq

AWF VIP
Local time
Today, 04:13
Joined
Jun 20, 2003
Messages
6,423
I think the reason that the Form_AfterUpdate and Form_BeforeUpdate events aren't firing is simple; you aren't creating a new record in the form!

You using a DAO recordset to add a new record directly in the underlying table, and this won't fire those events.

Have to say I've never seen a lot of copy function hacks and I've never see one constructed this way. Most use some variations of the following:

To copy all of a record
Code:
Private Sub CopyCompleteRecordl_Click()
 DoCmd.RunCommand acCmdSelectRecord
 DoCmd.GoToRecord , , acNewRec
 DoCmd.RunCommand acCmdPaste
End Sub
To only copy parts of a record
Code:
Private Sub CopyPartiaRecordl_Click()
'Copy fields from original record to variables
NewField1 = Me.YourField1
NewField2 = Me.YourField2
NewField3 = Me.YourField3

'Go to a new record
DoCmd.GoToRecord , , acNewRec

'Plug in old values from variables into new record
Me.YourField1.Value = NewField1
Me.YourField2.Value = NewField2
Me.YourField3.Value = NewField3

End Sub

Using either of these will fire the form's AfterUpdate and BeforeUpdate events.

Linq ;0)>
 

pdanes

Registered User.
Local time
Today, 01:13
Joined
Apr 12, 2011
Messages
49
I think the reason that the Form_AfterUpdate and Form_BeforeUpdate events aren't firing is simple; you aren't creating a new record in the form!
Yes, thank you, I've used those other methods as well. Just thought I'd try something different here, and it works quite well. The addition of the

Form_Data.Dirty = True

statement also forces the update events to fire when navigating away, so I have something that works, either way. But I'm still at a loss as to why the code refuses to work in the OnCurrent event. The event fires, when another record is loaded. I can set breakpoints in the event code, step the code, but the given commands simply don't do anything. Can you think of anything that might be causing that?
 

Users who are viewing this thread

Top Bottom