Cancel listbox selection

dpm

Registered User.
Local time
Today, 13:37
Joined
Mar 20, 2009
Messages
22
Hi all, this is something I've had trouble understanding, despite years of not understanding it...

I have a form with one listbox and one subform control.

Listbox contains list of reports. Subform control contains an options form for whichever report is selected in the listbox. None of the forms are bound.

When I select a report, the child subform loads an appropriate class object. The class has a dirty property and an event, which fires of course when the object is dirty.

If the object is dirty, I want to prevent user from selecting a different report, unless the current report is saved first.

I am attempting to use the BeforeUpdate event of the listbox. Seems to work ok, but if I set Cancel = True, the subform is locked unless I hit Escape. I do not want to code a SendKeys.

Does anyone have a nice, clean way of handling such a thing? Thanks.
 
I would think the check needs to be ion the Sub form.

Hard to say without seeing the forms and VBA code hor having a lot more details.
 
Not much in the way of complexity, if you dismiss the fact that I'm checking the .Dirty property of a custom object. One could be checking the .Dirty property of a bound subform, or the .Enabled property of a command button, etc.

Code:
Private Sub lstReports_BeforeUpdate(Cancel As Integer)
Dim frm As Form
 
On Error GoTo ErrHandler
Dim varRetVal As Variant
 
DoCmd.Hourglass True
 
If Me.childReportOptions.SourceObject = "frmReportsVLCCSuezmaxSub" Then
    Set frm = Me.childReportOptions.Form
    If frm.objVLCC.IsDirty = True Then
        Cancel = True 'This will be replaced by int return val from a msgbox
    End If
End If
 
ExitHandler:
Set frm = Nothing
DoCmd.Hourglass False
Exit Sub

 
etc...

The above works well, except that after the procedure finishes, I must hit the Esc key in order to return the listbox selection to where it originally was. Doing an .Undo doesn't do the trick. Neither does setting the .ListIndex property of the listbox to its previous value.

I am trying to avoid coding a check in the subform itself because The parent form's Unload event occurs before the subform Unload. Also, I intend to apply the same technique on Parent form Unload, once I figure it out.

I've attached a screenshot of the form in design view. Nothing much here...you select a report in the listbox, replacing the child's sourceobject.
 

Attachments

  • sample2.jpg
    sample2.jpg
    96.7 KB · Views: 124

Users who are viewing this thread

Back
Top Bottom