cant get undo to work

Dembrey

Registered User.
Local time
Today, 21:07
Joined
Mar 6, 2002
Messages
65
Sorry to post such a dumb request but this is driving me up the wall.

I have a combobox where in the after update I have some code which goes off and does somethings depending on the value selected. Now I want to verify the selected value before accepting it (i.e before running the code of AfterUpdate).

So I thought the following code would do it:

--------------
Private Sub Combo0_BeforeUpdate(Cancel As Integer)

If Me.Combo0 = "test" Then

Cancel = True
Me.Combo0.Undo

End If

End Sub
-------------

This stops the AfterUpdate code running ok when option "test" is selected but I want the combo box value to return to the previous value that it had before "test" was selected. I thought that's what the Undo method would do but it just sits there with the value "test" (looking all smug and self satisfied).

Can't believe this is giving me so much grief.
 
Try this...

Private Sub Combo0_BeforeUpdate(Cancel As Integer)

If Me.Combo0.Value = "test" Then

Cancel = True

Me.Combo0.OldValue

End If

End Sub

HTH
 
ghudson,

I get an "Invalid use of property" error when I try that. I tried a me.combo0 = me.combo0.OldValue too and got another error; I guess you can't set a control value in its own Beforeupdate event since it would lead to an infinite loop?

Cheers.
 
Try this in the AfterUpdate event instead...
Code:
Private Sub Combo0_AfterUpdate()
    
If Me.Combo0.Value = "test" Then
    MsgBox "= Test"
    Combo0.Value = Combo0.OldValue
    Exit Sub
Else
    MsgBox "<> Test"
End If
    
End Sub
HTH
 
ghudson,

Thanks for looking at this.

I tried your suggestion which looked OK but it still refused to reset to the oldvalue. After doing some more investigating I found that the OldValue property was set to the new value even in the BeforeUpdate event.

I have realised that I missed a vital piece of info in that the combobox is unbound; I did a test with a bound combobox and it reset OK using cancel/undo in the BeforeUpdate event. Sorry to have lead you off in a wrong direction.

It seems I can trap the 'oldvalue' by storing it as a variable in the GotFocus event then reset it if necessary in the Before/After Update event (for unbound controls these two events seem interchangeable anyone know if that's right?). All in all seems like a very long winded way to go for something that should be straight forward.
 

Users who are viewing this thread

Back
Top Bottom