CLosing forms and reports

  • Thread starter trekgoes2malaysia
  • Start date
T

trekgoes2malaysia

In VBA, I want to check if a particular form or report is open and if
so, close it. I am using the code below but it is not working (Access
returns a message saying it cannot identify the form if it is not
already open). How can I get around this?


'Closing all open report files
If [Forms]![frmGraphCyclist].Open Then
[Forms]![frmGraphCyclist].Close
End If

If [Reports]![ReportCyclistEfforts].Open Then
[Reports]![ReportCyclistEfforts].Close
End If

If [Reports]![RptTeamEfforts].Open Then
[Reports]![RptTeamEfforts].Close
End If


Patrick
 
J

Jeanette Cunningham

Patrick,
This code will close all open forms except the main menu:
Dim i As Integer
For i = Forms.Count - 1 to 0
If Forms(i).Name <> "Main Menu" Then
DoCmd.Close acForm, Forms(i).Name
End If
Next


Jeanette Cunningham
 
A

Allen Browne

The Forms collection refers only to open forms.
Use AllForms.

This kind of thing:
If CurrentProject.AllForms("frmGraphCyclist").IsLoaded Then
DoCmd.Close acForm, "frmGraphCyclist"
End If

Note that if the user was in the middle of entering something on that form,
but the entry was incomplete (e.g. a required field had not been filled in
yet), Access would close the form without notifying you that the new entry
was lost. You might want to avoid this by testing if the form has unsaved
edits (Dirty property), and forcing the save:
If CurrentProject.AllForms("frmGraphCyclist").IsLoaded Then
With Forms("frmGraphCyclist")
If .Dirty Then .Dirty = False
End With
DoCmd.Close acForm, "frmGraphCyclist"
End If

Use error handling to recover if the save fails.
 
T

trekgoes2malaysia

The Forms collection refers only to open forms.
Use AllForms.

This kind of thing:
    If CurrentProject.AllForms("frmGraphCyclist").IsLoaded Then
        DoCmd.Close acForm, "frmGraphCyclist"
    End If

Note that if the user was in the middle of entering something on that form,
but the entry was incomplete (e.g. a required field had not been filled in
yet), Access would close the form without notifying you that the new entry
was lost. You might want to avoid this by testing if the form has unsaved
edits (Dirty property), and forcing the save:
    If CurrentProject.AllForms("frmGraphCyclist").IsLoaded Then
        With Forms("frmGraphCyclist")
            If .Dirty Then .Dirty = False
        End With
        DoCmd.Close acForm, "frmGraphCyclist"
    End If

Use error handling to recover if the save fails.

--
Allen Browne - Microsoft MVP.  Perth, Western Australia
Tips for Access users -http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.




In VBA, I want to check if a particular form or report is open and if
so, close it. I am using the code below but it is not working (Access
returns a message saying it cannot identify the form if it is not
already open). How can I get around this?
   'Closing all open report files
  If [Forms]![frmGraphCyclist].Open Then
       [Forms]![frmGraphCyclist].Close
   End If
   If [Reports]![ReportCyclistEfforts].Open Then
       [Reports]![ReportCyclistEfforts].Close
   End If
   If [Reports]![RptTeamEfforts].Open Then
       [Reports]![RptTeamEfforts].Close
   End If- Hide quoted text -

- Show quoted text -

THank you for your excellent help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top