Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form change 1

Status
Not open for further replies.

kimtp

Programmer
Jun 15, 2002
310
US
I have a form with a bunch of txt boxes, combo boxes and check boxes. I would like to capture when the form changes to alert the user when a movement is made to leave the form without saving the data entered. An example would be clicking a cmd button to clear the form.

Thought about using the txtbox change property but that would trigger an unwanted action if the form is filled from the data base with a 'find' procedure.

Another approach would be to use the keypress, keyup or keydown property.

Any ideas or comments of to the usefullness of the latter approach would be great.

Thanx.
 
I would use some sort of boolean variable that says when the data was altered. For instance, in your change event you could set the boolean to True and in your find command you can set the value to false after all of the fields have been populated.
 
Thanx for the response. Yes, a boolean would be used. The question really is which event property would be best.
 
I think I'd use 2 booleans, and probably the change event. Why two booleans? SO we can do something like this (you'll needa form with a textbox and two command buttons):
Code:
[blue]Option Explicit

Private KeepClean As Boolean
Private IsDirty As Boolean


Private Sub Command1_Click()
    KeepClean = True
    Text1.Text = "Automated change"
    KeepClean = False
End Sub

Private Sub Command2_Click()
    If IsDirty Then
        [green]' data changed by user so do whatever
        ' e.g.[/green]
        MsgBox "Text has been changed by the user"
    End If
    Unload Me
End Sub

Private Sub Text1_Change()
    If Not KeepClean Then
        IsDirty = True
    End If
End Sub[/blue]
 
Many thanx for the suggestions. Here is what I have been working on to solve this situation.

Declared bFormChanged as public boolean and placed it into the keypress event handler. Then whenever the user tries to move from the form/changes records, a msgbox alerts to the change.

It works. Well, almost. For some reason or another the DEL key is not considered a keypress and therefore does not start the process. I have tried

Code:
Private Sub txtZip_Change()
    If Chr(127) <> "" Then Call FormChanged
End Sub
which has no affect. If it is changed to <> then every key press seems to affect the procedure. Tried
Code:
Private Sub txtZip_Change()
    If txtZip_KeyPress(KeyAscii As Integer) = Chr(127) Then
       Call FormChanged
    end if
End Sub

I get a compile error: expected list separator or )

Grateful for any suggestions. Thanx.
 
In your first code sample, without a redesign of the operating system Chr(127) will usually equal Chr(127). It will never equal an empty string!

In your second example you are comparing the name of a procedure against an Integer. The KeyAscii value is a local variable in the KeyPress event, and so is not valid outside that procedure. Try using the code provided, but it does look as if you need some basic tutorials first. This will get you started on procedures.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top