Close/Hide Form when No Records

CT Witter

MrExcel MVP
Joined
Jul 7, 2002
Messages
1,212
I have a form that is based on a recordset. The issue that I'm having is closing/hiding the form when no records are present.
I was going to check the recordset before opening this form but with a lot of records this will get slow to do a preliminary check. I'd rather go with that there will be records and if not just exit the form.

Code:
Private Sub Form_Load()
On Error GoTo Err_Form_Load
    Set mrst = New ADODB.Recordset
    mrst.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=C:\X\x.mdb"
    mrst.Source = strSQLView
    mrst.CursorType = adOpenStatic
    mrst.LockType = adLockBatchOptimistic
    mrst.CursorLocation = adUseClient
    mrst.Open Options:=adAsyncFetch
Exit_Form_Load:
    Exit Sub
Err_Form_Load:
    If Err.Number = "-2147217908" Then
    MsgBox "No Records Found!" & vbCrLf & vbCrLf & "Please Try Again", vbInformation + vbOKOnly, "No Records Found!"
    Me.Visible = False
    DoCmd.OpenForm "frmSearchDialog"
    End If
   MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_Form_Load
End Sub

Code:
Private Sub Form_Unload(Cancel As Integer)

   On Error GoTo Form_Unload_Error
    If MsgBox("Save all changes back to the database?", vbQuestion + vbYesNo) = vbYes Then
    Dim wksp As DAO.Workspace
    Set wksp = DBEngine.Workspaces(0)
        'This will commit only changed records to the database
        wksp.BeginTrans
        'Update the Updated Options
        mrst!Updated = Now()
        mrst!UpdatedBy = CurrentUser()
        mrst.MarshalOptions = adMarshalModifiedOnly
        mrst.UpdateBatch
        wksp.CommitTrans
    Else
        mrst.CancelBatch
    End If

Exit_Form_Unload:
    Exit Sub
Err_Form_Unload:
    'wksp.Rollback
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Exit_Form_Unload
End Sub

I keep getting an undefined error (Error 0 at line ) from the Unload Event Any help?

Thanks,
CT
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try

I tried this on a form I have, but with one field.


Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acNewRec
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToControl "EndTime"
If "EndTime" = "" Then
DoCmd.Close
End If

End Sub

Would it work with a field that would have something in it, if the record wasn't empty?
 
Upvote 0
Thanks for the suggestion but won't work in this case as the recordset for the form isn't initalized during the form open.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top