Prompting a user to "Are U sure Yes/No" pop-up when changing data

M

Mishanya

I want to prevent the possibility of undeliberate change/update of data in
certain fields of the form.
Ideally, it'll work as in usual Office documents (pop-up "Do U want to save
the changes Yes/No"). Only that every time a user changes the data (for
example, by putting a different value from combo-box list of values), the
application will prompt him to a pop-up ("Are U sure U want to change
FieldValue to NewListValue Yes/No"-kind)
So far I've found only the way to prevent a user from changing the data in
form at all - by switching the Enable Update property in Form Design, but
that's not an idea. I do want the user to be able to update the data - only
with drowing his attention and prompting him to assure the change.
Is there any way?
 
D

Douglas J. Steele

Put code in the form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If MsgBox("Are you sure you want to make an update?", _
vbYesNo + vbQuestion) = vbNo Then
Me.Undo
Cancel = True
End If

End Sub
 
M

Mishanya

It works perfectly!
Douglas, Thanks again ("Spasibo" in Russian!)! I think, U've already helped
me before.
If only US economics leaders (Bernkaki & Co) new their job, as U know Yours!

Now, enlightened thanks to U, I can go to the Poul McCartney's gig tonight
in Tel-Aviv!
 
M

Mishanya

If U could only advice me (for the same price) how do I put the exclaimer so
it goes
"Are you sure you want to update the FieldName from OldValue to NewValue?",
where FieldName is a Caption Property of the field, OldValue is a current
value and NewValue is an intended one?
 
D

Douglas J. Steele

That's a little more involved, since more than one field could have changed,
and you'd need to loop through all of the controls on the form to determine
which ones did.

One approach would be to use the "Tag" property of the fields that can be
changed (i.e.: text boxes, combo boxes, list boxes, etc.) Let's assume you
set it to "Check". (The Tag property's just a free-form text field that you
can put anything into...)

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctlCurr As Control
Dim strChanges As String

For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Check" Then
If Nz(ctlCurr.Value, "") <> Nz(ctlCurr.OldValue, "") Then
strChanges = strChanges & ctlCurr.Name & " from " & _
ctlCurr.OldValue & " to " & ctlCurr.Value & vbCrLf
End If
End If
Next ctlCurr

If Len(strChanges) > 0 Then
If MsgBox("Are you sure you want to make the following changes:" & _
vbCrLf & strChanges & "?", _
vbYesNo + vbQuestion) = vbNo Then
Me.Undo
Cancel = True
End If
End If

End Sub
 
M

Mishanya

Well, somehow this does not work or else (wich is more plausible) I don't use
it right.
So, I'll stick to the first simple one.
Anyway, thank U very much indeed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top