Share via

Trouble looping through form objects

Anonymous
2015-03-07T21:25:56+00:00

I am trying to create a sub that loops through AllForms, and saves/closes all of them except for one specific form called Globals.  I found and adapted some code, but I'm having trouble with one aspect.  The code I found looped through AllForms using an element called obj that was defined as Object.  See below...

    Dim obj As AccessObject

    Dim dbs As Object

    Set dbs = Application.CurrentProject

    For Each obj In dbs.AllForms

            If obj.IsLoaded And obj.Name <> "Globals" Then

                ''  If form is unsaved (i.e., "is 'Dirty'"), then save record (by forcing Dirty = False)

                If obj.Dirty = True Then obj.Dirty = False

                '' Close form

                DoCmd.Close ObjectType:=acForm, ObjectName:=obj.Name

            End If

    Next obj

    Set dbs = Nothing

Problem is that the Obj.Dirty statement won't work with obj declared as an AccessObject.  It throws a run-time error 438 ("Object doesn't support this property or method").

I thought maybe I could declare it as a Form (which seemed like the most likely variable type to work with AllForms), but that ends up throwing a run-time error 13 ("Type mismatch") on the "For each..." line.

So question is, how can I design this For Each statement to allow me to get to the form properties of the obj members?

Note: I'm suspecting that I'll get some answers that say either (1) "You don't need to save the data" (because it's saved magically somehow whenever you close a form) or (2) "There's a way to save the data automatically as part of the "DoCmd.Close" statement.

For point (1), if that's the case, do please let me know.  I assume that access doesn't just dump dirty data when it closes things, but I always code things to save, just to be safe.  I'm not sure where I'd look to see the rules on this about what gets saved or not saved.

For point (2), I didn't use the Close argument of the DoCmd.Close statement because I couldn't tell from the documentation what that did... It said it "saves changes to the object," but I couldn't tell if that saved changes just to the design or orientation of the form, or actually wrote all of the dirty data to the database.  That's a different question, but also appreciate any insight on that if you have it.

Regardless of these points, I'm interested in the answer to the original question, as I have other things I want to do in future versions that will adjust other form properties within this loop.

Thanks!

Emily

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-03-07T22:12:42+00:00

It sounds like you may be confusing Save (which saves design changes to the structure of a form) with saving the Data on a bound form. Setting Dirty to False forces a save of the data, assuming that the data passes all its referential integrity and validation and BeforeUpdate tests.

Also, the AllForms collection is literally ALL forms, open or not. If all you want to do is close all open forms, the Forms collection would return them, without the need to do an extra check:

Dim db As DAO.Database

Dim frm As Form

Set db = CurrentDb

For Each frm In db.Forms

   If frm.Name <> "Globals" Then

      If frm.Dirty Then

         frm.Dirty = False ' you need some error trapping here in case the record can't be saved

     End If

  End If

Next frm

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-03-08T00:15:53+00:00

    One thing I noticed, just in case anyone reads this thread in the future.  The Forms collection does not appear to have any open modal forms that I have open in it.

    I worked around that, but just thought someone might want to know that if they read this.

    Thanks!

    Emily

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-03-07T23:22:16+00:00

    Thanks Daniel.  Looks like what I stumbled into is the same as what you posted.  That makes me feel better.

    Thanks to everyone!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-03-07T23:18:54+00:00

    Thanks John,

    When I run that, Ihave an error on compiling.  It trips on the following line:

         For Each frm In db.Forms

    The message is "Compile error: Method or data member not found."  I notice that if I delete ".Forms" from "db.Forms" and then type a dot, it brings up a lot of properties and methods that do not include Forms.

    I played around a little bit, and although I'm a little out of my depths, I got the following to work.  Anything wrong with this code?  Please tell me if I'm about to blow up my database :).  I bolded the interesting parts.

        Dim frm As Form

        Dim db As Object   

        Set db = Application.Forms

        For Each frm In db

                If frm.Name <> "Globals" Then

                    If frm.Dirty = True Then frm.Dirty = False

                    DoCmd.Close ObjectType:=acForm, ObjectName:=frm.Name

                End If

        Next frm

        Set db = Nothing

    Thanks again!

    Emily

    Was this answer helpful?

    0 comments No comments