Problem with Cancel and setting old value back!

  • Thread starter Thread starter Niklas Östergren
  • Start date Start date
N

Niklas Östergren

Hi!

I have a form in which I´d like to ask the user if he/she rely want´s to
change a value in a record. And if the user answer NO I´d like to set the
old stored value back in the textfield else continue.

So I have the folowing code in the forms textbox:
===============================================
Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)

If MsgBox("Vill du verkligen ändra parameternumret?" & Chr(13) & Chr(10) &
"Det är väldigt ovanligt!", vbYesNo, "Ändra parameternummer") = vbNo Then
Me.txtParameterNo = lngPublicOldParameterNo
Cancel = True
End If

End Sub

Private Sub txtParameterNo_Dirty(Cancel As Integer)

lngPublicOldParameterNo = Me.txtParameterNo

End Sub

=================================================
lngPublicOldParameterNo is declared in the top of the code module:
Public lngPublicOldParameterNo As Long ' Holds old parameter number (before
change)

Now to my problem:
If the user answer NO on the msgbox after changing the value in
txtParameterNo then I get an runtime error:
Runtime error no. '-2147352567 (80020009)'

And the line "Me.txtParameterNo = lngPublicOldParameterNo" is higlighted.
When I hover above the variables the both contain the values exepcted. In
this case I have tryed to change Me.txtParameterNo from 2 to be 222 instead.
So at this moment Me.txtParameterNo have value 222 and
lngPublicOldParameterNo have value 2.

And since I answered NO on the mshbox I´d like Me.txtParameterNo to get
value 2 instead of 222.

Anyone have any clue of what I´m doing wrong here?

TIA
// Niklas
 
Not sure what all your message says, but your code should look similar to
this...

Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)
If MsgBox("Vill du verkligen ändra parameternumret?" & Chr(13) & Chr(10)
&
"Det är väldigt ovanligt!", vbYesNo, "Ändra parameternummer") = vbNo Then
[Somefieldname].value = [somefieldname].oldvalue
End If

End Sub



Rick B




Hi!

I have a form in which I´d like to ask the user if he/she rely want´s to
change a value in a record. And if the user answer NO I´d like to set the
old stored value back in the textfield else continue.

So I have the folowing code in the forms textbox:
===============================================
Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)

If MsgBox("Vill du verkligen ändra parameternumret?" & Chr(13) & Chr(10) &
"Det är väldigt ovanligt!", vbYesNo, "Ändra parameternummer") = vbNo Then
Me.txtParameterNo = lngPublicOldParameterNo
Cancel = True
End If

End Sub

Private Sub txtParameterNo_Dirty(Cancel As Integer)

lngPublicOldParameterNo = Me.txtParameterNo

End Sub

=================================================
lngPublicOldParameterNo is declared in the top of the code module:
Public lngPublicOldParameterNo As Long ' Holds old parameter number (before
change)

Now to my problem:
If the user answer NO on the msgbox after changing the value in
txtParameterNo then I get an runtime error:
Runtime error no. '-2147352567 (80020009)'

And the line "Me.txtParameterNo = lngPublicOldParameterNo" is higlighted.
When I hover above the variables the both contain the values exepcted. In
this case I have tryed to change Me.txtParameterNo from 2 to be 222 instead.
So at this moment Me.txtParameterNo have value 222 and
lngPublicOldParameterNo have value 2.

And since I answered NO on the mshbox I´d like Me.txtParameterNo to get
value 2 instead of 222.

Anyone have any clue of what I´m doing wrong here?

TIA
// Niklas
 
Hi Rick!

And thanks for trying to help me out with this one! I don´t quit understand
what you mean so I´ll try to explane my msg and what I´m trying to do and
how I have tryed to solve it.

First of all I save the old value in a public variable named
"lngPublicOldParameterNo". Declaed at the top of the code module like this:

*********************************************************************
Public lngPublicOldParameterNo As Long ' Holds old parameter number (before
change)
*********************************************************************

I store the old value so I know which value to get back if the user cancel
the event of changing the value. This is done on the textcontrols dirty
event, like this:

********************************************************************
Private Sub txtParameterNo_Dirty(Cancel As Integer)

lngPublicOldParameterNo = Me.txtParameterNo

End Sub
********************************************************************

Then I finish with asking the user if he/she realy wants to change the value
and if NOT then I set the textcontrol to the old value again and cancel the
event. This is obvious not the correct way since I get a runtime error and I
have tryed severel other solutions but can´t get it to work.

Anyway here´s the code for that and I have transated the msg into english so
you understand what I´m asking the user:

************************************************************************
Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)

If MsgBox("Do you realy want to change the parameter number?" & Chr(13) &
Chr(10) &
"It´s realy unursual!", vbYesNo, "Change parameter number") = vbNo Then
Me.txtParameterNo = lngPublicOldParameterNo
Cancel = True
End If

End Sub
*****************************************************************'*********

In your answere you tell me that I shouldn´t use the stored value in
variable lngPublicOldParameterNo to set the old value back in the
textcontrol [Me.txtParameterNo]. But instead use the fieldname bound to the
textcontrol (which is [ParameterNo]) and set this field to the old
fieldname.

But I don´t have any old fieldname. I only have one field included in this
problem and that is [ParameterNo]. The old value is stored in the variable
and the new value (which I don´t want to use) is not yet stored in the bound
field. It´s only located in the textcontrol txtParameterNo so I don´t follow
you here?

TIA!
// Niklas




Rick B said:
Not sure what all your message says, but your code should look similar to
this...

Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)
If MsgBox("Vill du verkligen ändra parameternumret?" & Chr(13) & Chr(10)
&
"Det är väldigt ovanligt!", vbYesNo, "Ändra parameternummer") = vbNo Then
[Somefieldname].value = [somefieldname].oldvalue
End If

End Sub



Rick B




Hi!

I have a form in which I´d like to ask the user if he/she rely want´s to
change a value in a record. And if the user answer NO I´d like to set the
old stored value back in the textfield else continue.

So I have the folowing code in the forms textbox:
===============================================
Private Sub txtParameterNo_BeforeUpdate(Cancel As Integer)

If MsgBox("Vill du verkligen ändra parameternumret?" & Chr(13) & Chr(10) &
"Det är väldigt ovanligt!", vbYesNo, "Ändra parameternummer") = vbNo Then
Me.txtParameterNo = lngPublicOldParameterNo
Cancel = True
End If

End Sub

Private Sub txtParameterNo_Dirty(Cancel As Integer)

lngPublicOldParameterNo = Me.txtParameterNo

End Sub

=================================================
lngPublicOldParameterNo is declared in the top of the code module:
Public lngPublicOldParameterNo As Long ' Holds old parameter number (before
change)

Now to my problem:
If the user answer NO on the msgbox after changing the value in
txtParameterNo then I get an runtime error:
Runtime error no. '-2147352567 (80020009)'

And the line "Me.txtParameterNo = lngPublicOldParameterNo" is higlighted.
When I hover above the variables the both contain the values exepcted. In
this case I have tryed to change Me.txtParameterNo from 2 to be 222 instead.
So at this moment Me.txtParameterNo have value 222 and
lngPublicOldParameterNo have value 2.

And since I answered NO on the mshbox I´d like Me.txtParameterNo to get
value 2 instead of 222.

Anyone have any clue of what I´m doing wrong here?

TIA
// Niklas
 
Back
Top