For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > May 2004 > Checking if need to save changes on a form









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Checking if need to save changes on a form
Dale

2004-05-28, 2:30 pm

Hi, what's the best way - in a data entry form - to alert the user that
some changes have been made and prompt them to save or cancel? I got a
bunch of textboxes etc. on the form and was thinking of having a global
boolean variable the I will set in the Change event of the controls, and
then check this variable if the user attempts to close the form before
saving changes. Just wondering if there's an easier way especially if I
might be adding more fields later on. Thanks.
Don@home.com

2004-05-28, 2:30 pm

On Fri, 28 May 2004 10:16:38 -0700, Dale <dale0610@hotmail.com> wrote:

>Hi, what's the best way - in a data entry form - to alert the user that
>some changes have been made and prompt them to save or cancel? I got a
>bunch of textboxes etc. on the form and was thinking of having a global
>boolean variable the I will set in the Change event of the controls, and
>then check this variable if the user attempts to close the form before
>saving changes. Just wondering if there's an easier way especially if I
>might be adding more fields later on. Thanks.


Just a thought... If that *bunch of textboxes* were a control array there'd be
only /one/ place (change event) to set that varible for all them there boxes...
;->

Also makes it easier to add more boxes if they are needed...
And so on...
and so on...

Have a good day...

Don
Ken Halter

2004-05-28, 3:30 pm

Dale wrote:
> Hi, what's the best way - in a data entry form - to alert the user that
> some changes have been made and prompt them to save or cancel? I got a
> bunch of textboxes etc. on the form and was thinking of having a global
> boolean variable the I will set in the Change event of the controls, and
> then check this variable if the user attempts to close the form before
> saving changes. Just wondering if there's an easier way especially if I
> might be adding more fields later on. Thanks.


Here's another way to check for changes. If the user changes the text
back to the original value, it's not considered a change.

Needs a project with 2 forms (Form1 and Form2)
Even though it doesn't matter how many textboxes or what their names
are, as coded below, Form2 needs textboxes Text1, Text2, Text3(0 and 1)
A bit more code and you can support just about any control using the
same technique.
'==============Form1 Code
Option Explicit

Private Sub Command1_Click()
Load Form2
Form2.Text1 = "Test 1"
Form2.Text2 = "Test 2"
Form2.Text3(0) = "Test 3a"
Form2.Text3(1) = "Test 3b"
Call Form2.PrepareEntryFields
Form2.Show
End Sub
'==============Form2 Code

Option Explicit

Public Sub PrepareEntryFields()
Dim c As Control

'Copy Text to Tag
For Each c In Controls
If TypeOf c Is TextBox Then
c.Tag = c.Text
End If
Next

End Sub

Private Sub CheckForChanges()
Dim c As Control

'Compare Tag to Text
For Each c In Controls
If TypeOf c Is TextBox Then
If c.Tag <> c.Text Then
If MsgBox("Save Changes?", vbYesNo) = vbYes Then
MsgBox "Insert code to save here"
End If
Exit For
End If
End If
Next

End Sub

Private Sub Form_Unload(Cancel As Integer)
Call CheckForChanges
End Sub
'==============



--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep all discussions in the groups..
Kjell

2004-05-28, 3:30 pm

If you mean a module public boolean when you say global then I say go for it...

But, like don mention, you can do more if you can use a control array

I've something similar to what you describe, and I'm using a string variable to store the value when control is getting the focus, this way I can allow users to hit ESC to cancel what they are typing and get back to original value, when control looses foc
us in Validate event I can check to see if the new content is different from old and set a module boolean true.

And that boolean can later be used to remind users that things has been changed.

Kjell
Larry Serflaten

2004-05-28, 3:30 pm


"Dale" <dale0610@hotmail.com> wrote
> Hi, what's the best way - in a data entry form - to alert the user that
> some changes have been made and prompt them to save or cancel? I got a
> bunch of textboxes etc. on the form and was thinking of having a global
> boolean variable the I will set in the Change event of the controls, and
> then check this variable if the user attempts to close the form before
> saving changes. Just wondering if there's an easier way especially if I
> might be adding more fields later on. Thanks.


'Making changes' almost assumes you've put text in the text boxes and
now want to confirm any changes that the user has made.

If that is the case, then along side of putting text in the textboxes, you
could also store that same text in the textbox's Tag property. Then
when the user wants to leave, you can loop through the controls to
check if the Tag property no longer matches the Text property, and
offer to save if you found any differences.

What this avoids is the (rather annoying) case where the user changes
something, but then changes it back. If you use a flag, as you stated,
that would show dirty, and cause the user to deal with the save query
even when nothing was changed. If you compare what they currently
hold, with what you put in them, then the user is only queried when there
is actually data that has been changed....

LFS
Dale

2004-05-28, 6:30 pm

Thanks for all the feedback, folks.
Kjell

2004-05-28, 8:30 pm

I'm speechless.......... using tag property is briliant......

Kjell
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com