Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    forbid opening a form (Access 2000)

    I want to find the shortest and easiest code that forbids a form to be opened by itself
    and be opened only through the main form.
    Can somebody help me?

  2. #2
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Newbury, Berkshire, England
    Posts
    712
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: forbid opening a form (Access 2000)

    I think I would use the open arguments for this.

    Roger provides a down loadable example here.

    File name:
    OpenArgs.mdb (30 KB) ( intermediate ) Access 97

    Description:
    This application illustrates how to use OpenArgs to solve a variety of problems. Eg. 1) how to use OpenArgs to change the Record Source of a form in this case the order the fields are presented. 2) how to use OpenArgs to change the Title, various properties, and report to print on a 'Parameter Form'. 3) how to use OpenArgs to send multiple values to controls on another form.

    Roger doesn

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: forbid opening a form (Access 2000)

    First, you need a method for determining whether the main form is loaded. There are several ways to do this, all requiring code. Perhaps the simplest is a function like the following, which you would paste into a standard module in your database. Be sure to name the module anything except IsLoaded because modules can't have the same name as a function or subroutine.

    <pre>Public Function IsLoaded(ByVal strObjNm As String, _
    Optional intObjType As Integer = acForm) As Boolean
    Dim intOpen As Integer
    Dim blnOpen As Boolean

    intOpen = SysCmd(acSysCmdGetObjectState, intObjType, strObjNm)
    Select Case intOpen
    Case acObjStateOpen, acObjStateDirty, acObjStateOpen + acObjStateDirty
    blnOpen = True
    Case Else
    blnOpen = False
    End Select
    IsLoaded = blnOpen
    End Function</pre>


    In the property dialog of the form you need to restrict , set the Open event to [Event Procedure] put something like this into the form's module:

    <pre>Private Sub Form_Open(Cancel As Integer)
    If Not IsLoaded("frmMain") Then
    Cancel = True
    MsgBox "This form can only be opened through the main menu."
    End If
    End Sub</pre>


    This tells Access that if the main form is not loaded, it should cancel opening this form. I'm assuming that your main form is called frmMain. You should change that to whatever the name actually is.
    Charlotte

  4. #4
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: forbid opening a form (Access 2000)

    >>I want to find the shortest and easiest code that forbids a form to be opened by itself and be opened only through the main form.<<

    Do you mean that it can only be opened as a subform on the main form, or that it can only be opened from the main form?
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: forbid opening a form (Access 2000)

    Thank you for the reply.Yeas, i want to be opened only as a subform on the main form.What line of code can i write in the subform so that it cannot be opened as a
    standalone form but only as a subform?
    Best regards

  6. #6
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: forbid opening a form (Access 2000)

    >>i want to be opened only as a subform on the main form.<<

    I would just check for the form in the forms collection. Put code something like this in your form's Open event (I haven't checked to verify it would work there, you might need to try Load event):
    <pre> On Error goto Exit_Sub:
    Forms!yourFormName.name
    msgbox "You can't open this form!",vbcritical
    DoCmd.Close
    Exit_Sub:
    exit sub</pre>

    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: forbid opening a form (Access 2000)

    The simplest way is to test the Parent property of the form. If it returns an error or some form besides the main form, cancel the open of the form.
    Charlotte

  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: forbid opening a form (Access 2000)

    you reply looks to me interesting.Could you help me further, what exactly should i put as code in the OnOpen event of the Subform, in order to be opened only when the MainForm is opened but not as a standalone form
    Thank you.
    Best regards

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: forbid opening a form (Access 2000)

    This is aircode, but try something like this in the Open event:

    Dim frm As Form
    Dim strMsg As String
    On Error Resume Next

    Set frm = Me.Parent
    If frm Is Nothing Or err = 2452 Then
    Cancel = True
    MsgBox strMsg
    End If

    Set frm = Nothing

    You would need to populate strMsg with whatever you wanted to tell the user about opening this form. Error 2452 is an invalid reference to the Parent property. You would have to handle other errors appropriately as well.
    Charlotte

Posting Permissions

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