Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jun 2008
    Posts
    163

    Unanswered: close almost all forms except... // also, record lock question

    What's the proper way to write an onclick event that closes almost all open forms except certain specified forms?

    Also, where in access can I set the record lock settings?

    thanks! =D

  2. #2
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    The On Click event needs to loop through the form names and close each, but in that loop you'll need to make exceptions for which forms you want to leave open.

    Record lock settings can be found in the Record Locks property of each form.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    If you have a lot of forms open (especially having multiple data forms open on the same recordsets), you also have a tendency to get record locks. I tend to cringe when I read that a user needs code to close multiple forms and also wonders about the record lock settings.

    Forms should open, close and follow an orderly fashion.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  4. #4
    Join Date
    Jun 2008
    Posts
    163
    @ST: thanks again! I was hoping the solution would not be as tedious as manually shutting each form by name, but that's just me being lazy now.

    @pkstormy
    sorry! I didn't clear it up. I was asking about record locks because I have more than one user accessing the DB at the same time and was wondering what the restrictions are. It had nothing to do about the closing forms.

    As for the question about forms, its because I have a log-out button and a background form (which is nothing but a background image, linked). I want the button to close all the open forms and leave the background form open then re-open the login form. For now, I don't have a log out button but an exit db button instead. No record-locking problems for now, and hopefully, only a minimal few in the future.

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You do not have to loop through the forms name, you can loop through the Forms collection! I've done this before, but can't remember where - if the lightbulb goes off I'll find it and post back.

    I've implemented this in the passed in a very similar situation - log out procedures.
    George
    Home | Blog

  6. #6
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Might Something like this work (although I dug this up from my archives and it does seem like it might be dated but should work ok):

    Public Function closeforms()
    On Error Resume Next
    Dim obj As Object
    For Each obj In Application.Forms
    DoCmd.close acForm, obj.Name
    Next obj
    End Function

    Maybe adding in a test before the docmd.close command in the above code to test to not close a certain form (ie:

    if obj.Name = "MainFormBackground" then goto LblAboveNextObj (and create a label called: LblAboveNextObj: above the Next Obj line.)

    ie (make this function in a module).....
    Public Function closeforms()
    On Error Resume Next
    Dim obj As Object
    For Each obj In Application.Forms
    if obj.Name = "MainFormBackground" then goto LblAboveNextObj
    if obj.Name = "SomeOtherFormNotToClose" then goto LblAboveNextObj
    DoCmd.close acForm, obj.Name
    LblAboveNextObj:
    Next obj
    End Function

    Then just call the function (ie.

    Call CloseForms() anywhere in your code.

    or

    Public Function closeforms()
    Dim X As Long
    For X = Forms.Count To 1 Step -1
    DoCmd.Close acForm, Forms(X - 1).Name
    Next X
    End Function
    Last edited by pkstormy; 08-13-08 at 05:53.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  7. #7
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'd certainly remove the On Error Resume Next and replace it with proper error handling constructs, but the idea it pretty much there.

    You should wrap the close command in an If statement too, where the If statement compares the form names to the ones you wish to leave open.
    George
    Home | Blog

  8. #8
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    On Error Resume Next FTW!!!!

    And yes, I meant loop through the forms collection...

    I have the code for doing this somewhere too... I'll try dig it up.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  9. #9
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Too easy A little different from the others posted.

    Close all forms:
    Code:
    Dim frm As Form
    For Each frm In Forms
        DoCmd.Close acForm, frm.Name, acSaveYes
    Next
    Close all forms but one:
    Code:
    Dim frm As Form
    For Each frm In Forms
        If frm.Name <> "Main Menu" Then DoCmd.Close acForm, frm.Name, acSaveYes
    Next
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  10. #10
    Join Date
    Jun 2008
    Posts
    163
    I'd certainly remove the On Error Resume Next and replace it with proper error handling constructs, but the idea it pretty much there.

    You should wrap the close command in an If statement too, where the If statement compares the form names to the ones you wish to leave open.
    You give my coding skills too much credit to figure that out =p
    Just kidding, I'll look it up and learn what I can.

    Anyway, you guys are awesome. I'll let you know which works for me later. Thanks heaps! =D

  11. #11
    Join Date
    Jun 2008
    Posts
    163
    I tried the module, I'll try figuring it out more later. For now, I'm using:

    Dim frm As Form
    For Each frm In Forms
    If frm.Name <> "Main Menu" Then DoCmd.Close acForm, frm.Name, acSaveYes
    Next
    hmm, how do I properly use "goto"?

    if frm.name <> 'main' then goto...

    I want it to jump to:
    if frm.name <> 'bgfrm' then DoCmd.Close acForm, frm.Name, acSaveYes

    Will that leave two forms open? Thanks again! =D

  12. #12
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    You may also want to glance at the IsFormLoaded function/module which can be found in the MSAccess Sybex books or in one of the many examples in the code bank (or I've copied it below). Instead of just trying to close the form regardless of whether it's open or not, you could test to see if the form is actually open before closing it.

    Public Function isloadedForm(strName As String, Optional lngtype As AcObjectType = acForm) As Boolean
    isloadedForm = (SysCmd(acSysCmdGetObjectState, acForm, strName) <> 0)
    End Function
    Public Function isloadedReport(strName As String, Optional lngtype As AcObjectType = acReport) As Boolean
    isloadedReport = (SysCmd(acSysCmdGetObjectState, acReport, strName) <> 0)
    End Function

    and called like this: if isloadedform("MainForm") = true then docmd.close acform, "MainForm"

    And yes, George and StarTrekker are correct on ditching the on error resume next (I did mention that I dug it up from old archives.) Ideally then (without the on error resume next and using the isloadedform command), the code I supplied would then become:

    Note: you could do the same type of procedure to close open reports but use Application.Reports and use the isloadedReport function.

    Public Function closeforms()
    Dim obj As Object
    For Each obj In Application.Forms
    if isloadedform(obj.Name) = true then
    if obj.Name = "MainFormBackground" then goto LblAboveNextObj
    if obj.Name = "SomeOtherFormNotToClose" then goto LblAboveNextObj
    DoCmd.close acForm, obj.Name
    end if
    LblAboveNextObj:
    Next obj
    End Function

    I personally think the code above will work pretty good for you and do exactly what you want it to. This would be the route I would go!

    (StarTrekker - is your code to loop through the forms collection that much different than that above?)

    In regards to using the "goto", see the coding example above and how I used it.
    Last edited by pkstormy; 08-14-08 at 01:57.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  13. #13
    Join Date
    Jun 2008
    Posts
    163
    I haven't gotten as far as reports yet, I was under the impression that they would be controlled the same way as forms. Thanks for the help PK! (aside from the question at hand, this made for a good reference for coding too)

  14. #14
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Seriously, check out the code bank. There are dozens of examples which many of them can help you out.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  15. #15
    Join Date
    Jun 2008
    Posts
    163
    They do actually! =D

    I've already downloaded several; bespokedialog, demostartup021, excelautomation_v4, search, securitydemo, etc. Most of the code I use are frankensteined from the codebank, wizard codes (ducks), board replies and random stuff I find online.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •