On Timer

  • Thread starter Thread starter DebbieG
  • Start date Start date
D

DebbieG

I copied some code from another database and I can't get it to work in my
new database. The SAVE button on my form is not visible. I want it to be
visible when the record is dirty. If the record is dirty and I press the
ESC key, I want it to be invisible. And if I save the record, I want it to
be invisible. Here is my code:

Private Sub Form_Current()
cmdSave.Visible = False
End Sub

Private Sub Form_Dirty(Cancel As Integer)
Me.cmdSave.Visible = True
End Sub

Private Sub Form_Timer()
Dim fDirty As Boolean
fDirty = Me.Dirty
If Not (fDirty Eqv cmdSave.Visible) Then
cmdSave.Visible = fDirty
End If
End Sub

The timer interval is set to 250. The visible property on the SAVE button
is set to NO.

The ESC key is working great. But when I click SAVE I get:

Run-time error '2165'
You can't hide the control that has the focus.

I am missing something but can't seem to find it. Like I said, it works
fine in my other database. I've searched everywhere I know, looked in all
my manuals, but can't find where I found this code in the first place.

Thanks in advance for any help.
Debbie
 
DebbieG said:
I copied some code from another database and I can't get it to work in my
new database. The SAVE button on my form is not visible. I want it to be
visible when the record is dirty. If the record is dirty and I press the
ESC key, I want it to be invisible. And if I save the record, I want it to
be invisible. Here is my code:

Private Sub Form_Current()
cmdSave.Visible = False
End Sub

Private Sub Form_Dirty(Cancel As Integer)
Me.cmdSave.Visible = True
End Sub

Private Sub Form_Timer()
Dim fDirty As Boolean
fDirty = Me.Dirty
If Not (fDirty Eqv cmdSave.Visible) Then
cmdSave.Visible = fDirty
End If
End Sub

The timer interval is set to 250. The visible property on the SAVE button
is set to NO.

The ESC key is working great. But when I click SAVE I get:

Run-time error '2165'
You can't hide the control that has the focus.

I am missing something but can't seem to find it. Like I said, it works
fine in my other database. I've searched everywhere I know, looked in all
my manuals, but can't find where I found this code in the first place.


If this works somewhere else, then something about it is
different. As the error message states, you can not make a
control invisible when it has the focus.

The Save button's click event, should set the focus to some
other control before it makes itself invisible.

Sub cmdSave_Click()
. . .
Me.someothercontrol.SetFocus
Me.cmdSave.Visible = False
. . .
End Sub

BUT, the timer event can fire at any time so its code has to
check if the Save button has the focus before making it
invisible.

Private Sub Form_Timer()
If Me.ActiveControl.Name <> "cmdSave" Then
Me.cmdSave.Visible = Me.Dirty
End If
End Sub
 
Out of curiousity, why use a timer event at all? I avoid them as much as
possible since they can cause unexpected things to occur when the event runs and
the user is entering data.

I would just use the On Dirty, On Current, and After Update Events to call one
routine. Something like the following UNTESTED code.

Private Sub ShowHideSave ()

If Screen.ActiveControl.Name = "CmdSave"
AND me.Dirty = True Then
SomeOtherControl.SetFocus
End If

CmdSave.Visible = Me.Dirty

End Sub
 
This works great (if I turn off the use of the ESC key). However, After
Update I do a requery and it goes to the first record. How do I make it
stay on the current record? Do I just need to do a requery if I add a new
record? If so, where do I put that?

Thanks so much!


Out of curiousity, why use a timer event at all? I avoid them as much as
possible since they can cause unexpected things to occur when the event runs
and
the user is entering data.

I would just use the On Dirty, On Current, and After Update Events to call
one
routine. Something like the following UNTESTED code.

Private Sub ShowHideSave ()

If Screen.ActiveControl.Name = "CmdSave"
AND me.Dirty = True Then
SomeOtherControl.SetFocus
End If

CmdSave.Visible = Me.Dirty

End Sub
 
Back
Top