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

Please pick this apart...

Status
Not open for further replies.

Quehay

Programmer
Feb 6, 2000
804
0
0
US
I've got a form with record control buttons (client request) that should be enable/disable acc. to record status. Here's the approach I'm using--please critique if you find a better way or a problem; please borrow if you like it:

In order to make the buttons' status reflect the appropriate options they are enabled/disabled acc. to form status.

A general module has this sub that is common to all forms:
[tt]
Public Sub SetRecordButtons(Frm As Form, SetValue As String)
'On Error Resume Next

Select Case SetValue

Case "NewRecord"

Frm.cmdNew.Enabled = False
Frm.cmdSave.Enabled = False
Frm.cmdCancel.Enabled = False
Frm.cmdDelete.Enabled = False

Case "ExistingRecord"

Frm.cmdNew.Enabled = True
Frm.cmdSave.Enabled = False
Frm.cmdCancel.Enabled = False
Frm.cmdDelete.Enabled = True

Case "DirtyRecord"

Frm.cmdNew.Enabled = False
Frm.cmdSave.Enabled = True
Frm.cmdCancel.Enabled = True
Frm.cmdDelete.Enabled = False

Case "Updated"

Frm.cmdNew.Enabled = True
Frm.cmdSave.Enabled = False
Frm.cmdCancel.Enabled = False
Frm.cmdDelete.Enabled = True

Case Else

End Select

End Sub
[/tt]
In the Form_Current Event (occurs after a Find of existing record, a Delete, or an Insert):
[tt]
Private Sub Form_Current()
On Error Resume Next

If Me.NewRecord Then
'Sub in basCoreRoutines
SetRecordButtons Me, "NewRecord"

Else
'Sub in basCoreRoutines
SetRecordButtons Me, "ExistingRecord"

End If

End Sub
[/tt]

In the Form_AfterUpdate Event (occurs after a Save):
[tt]
'Sub in basCoreRoutines
SetRecordButtons Me, "ExistingRecord"
[/tt]
To capture and respond to any Cancel operation, the following is done in the Form_Timer (every second) event:
[tt]
Private Sub Form_Timer()
On Error Resume Next

If Me.Dirty Then

SetRecordButtons Me, "DirtyRecord"

Else

If Me.NewRecord Then

'Sub in basCoreRoutines
SetRecordButtons Me, "NewRecord"

Else

'Sub in basCoreRoutines
SetRecordButtons Me, "ExistingRecord"

End If

End If

End Sub
[/tt]
* The use of any event associated with the Cancel button itself will fail because the button can't be disabled while it has focus-even the case with "Lost_Focus" event.
Jeff Roberts
Analysis, Design, & Implementation
RenaissanceData.com
 
Looks to me like you have the right idea. It looks like SetValue will always be one of the four cases in the code. In that case, good. If it could be something else, you should fill in Else.
To handle events with the Cancel button, have the code put the focus somewhere else, then alter its status.
That's good that you're using the OnCurrent event. Many people try to have controls enabled/disabled with each record. Looks like you already know that you can't do that.
 
Thanks TR! I didn't think I'd be able to reset the focus to another control while still handling an event of a control. Not sure what you mean by "controls...with each record"--just curious.

Realized that the Dirty event is better for setting the Dirty response (making for one less test in the Timer event):

[tt]Private Sub Form_Dirty(Cancel As Integer)
On Error Resume Next

'Sub in basCoreRoutines
SetRecordButtons Me, "DirtyRecord"

lblDirty.Visible = True

End Sub

Private Sub Form_Timer()
On Error Resume Next

'Catch the Undo/Cancel if it occurs
If Not (Me.Dirty Eqv lblDirty.Visible) Then
lblDirty.Visible = False
End If

If Not (Me.Dirty) Then

If Me.NewRecord Then

'Sub in basCoreRoutines
SetRecordButtons Me, "NewRecord"

Else

'Sub in basCoreRoutines
SetRecordButtons Me, "ExistingRecord"

End If[/tt] Jeff Roberts
Analysis, Design, & Implementation
RenaissanceData.com
 
Do you need to have the timer at all? If it isn't a dirty record, wouldn't it get caught by form_Current or form_AfterUpdate?
I've always been told to avoid having continuous timers like that at all costs. This one isn't a big deal, but you should get a little better performance without it.

Just a thought!

-Brad
 
Brad you're quite right--it bugs me have the Timer overhead in there, but in Acc2000 (apparently 2002 has it) there's no Undo or Cancel Record event, so it's only by running this that you can be sure to catch when a user backs out of a record with either Escape key or Cancel button (the Escape key press can be trapped but typically it takes several Escape hits to back out of record). Jeff Roberts
Analysis, Design, & Implementation
RenaissanceData.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top