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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem when using form OnDirty property in Access2000

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
0
0
US
I have a bound form with command buttons for Add, Edit and Delete along with Save and Cancel and navigation buttons. The default mode is edit mode so the user doesn't have to click Edit on every record.

If they click Add then all buttons except Save and Cancel are disabled. I want to do the same thing if the user actually makes any changes to the current data.

When I try to use the form's OnDirty event to disable the other buttons similarly to the Add or Edit click events, I lose the data. I have tried setting KeyPreview to both True and False and it doesn't make a difference.

For example, say I click on txtAddress and begin to type in Abcd. The OnDirty event properly causes the command buttons to be disabled, but Abcd never appears in txtAddress. Any ideas what is causing this behavior?

Thanks in advance for any help you can give me!

 
Is it possible that in setting the enabled property of the buttons, txtAddress is losing the focus and therefore your keypresses are going into the ether? The OnDirty event occurs when the first key is pressed and maybe before it is sent to the application (a la KeyDown). Try txtAddress.setFocus after you have played around with the buttons.

Steve
 
Bingo, that's what it was. I have default focus control that I make so small that it is invisible on the screen and it was receiving the focus due to OnDirty processing. As soon as I set a flag to skip giving it the focus, it worked like a charm.

Thanks a ton for the response!



 
hope it's ok if i but in with my own 'ondirty' cunudrum. i looked up ms' help wrt this event and they kindly provide an example which does pretty much what i had in mind for my form to do, w/ one small hitch. the minute you enter anything (even a space) in a control on the form the button which was enabled is set to disable mode!!?? exactly (what i would think being a vba newbie) is the opposite of the coded instruction. am i wrong?

below is the code of ms':

Private Sub Form_Dirty(Cancel As Integer)
If Me.Dirty Then
Me!btnUndo.Enabled = True ' Enable button.
Else
Me!btnUndo.Enabled = False ' Disable button.
End If
End Sub

Sub btnUndo_Click()
Dim ctlC As Control
' For each control.
For Each ctlC In Me.Controls
If ctlC.ControlType = acTextBox Then
' Restore Old Value.
ctlC.Value = ctlC.OldValue
End If
Next ctlC
DoCmd.Close 'i added this docmd.close to close the form unsaved
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top