Opening a form only from a button click - openargs??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Celal
    New Member
    • Mar 2007
    • 18

    Opening a form only from a button click - openargs??

    Hello, I'm still using Access 2003 as I find it more "developer friendly" than "user friendly".

    I really didn't use openargs much, only to pass variables but not to do an action. Now I have a question:

    I have a listbox on a form which passes the value to the subform (dont ask me why I use listbox instead of comdo). I have a command button to add a list box item if it is not in the list. The list box is bound to a table, and the button fires up the "addGroup" form as
    Code:
    DoCmd.OpenForm "frm_addGroup", acNormal, "", "", acAdd, acDialog
    It works OK but I want this form opened only when it is accessed by the forms that call it. I mean, the user should NOT be able to open that form on it's own, it should only be opened by the command button that calls it. I guessed passing a value in openArgs as like "allow" and testing that in the addGroup form onOpen event would do the thing but I get errors.

    So, on form1, I have a cmd button to open a form2 to add records to another table. I want that form to be accessible ONLY by that button. The user should not open that form otherwise.

    Can anyone help me on this please???
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Access applications can range from the very simple system used by a single user (usually the developer) to very complex systems.

    Generally speaking the more complicated the system, the more you lockdown the application. The usual approach is to disable the navigation pane, remove the menu bars, and distribute the application a MDE (cannot be edited) connected to a network backend.

    Sometimes however the above is somewhat overkill compared to what it is used for.

    With the above in mind, if we simply want to stop the form from being opened if its not opened from the correct location we can use the following approach:
    Add a bit of info to the openargs argument when the form is opened, for instance the opening forms name:
    Code:
    DoCmd.OpenForm "frm_addGroup", acNormal, "", "", acAdd, acDialog,Me.Name
    And in the Open event of your frm_AddGroup form, we add:
    Code:
    Private Sub Form_Open(Cancel as Integer)
      If me.OpenArgs<>"YourFormName" Then
        Cancel=True
        Msgbox "You Naughty little boy" 'This line can be omitted
        Exit Sub
      End If
    End Sub

    Comment

    • Celal
      New Member
      • Mar 2007
      • 18

      #3
      Many thanks for your reply SmileyCoder.

      Your code didn't work at first so I tried to debug just to know what OpenArgs is by "MsgBox Me.OpenArgs". I got an error 94 invalid use of null which is not a surprise because if you try to open the form directly, there is no OpenArgs. When I opened it clicking the button on the calling form though, it did give me the correct form name. But strangely, it still does not cancel and opens the form. Guess something with the "Null"???

      So I just did this:
      Code:
      Private Sub Form_Open(Cancel As Integer)
          If IsNull(Me.OpenArgs) Then
              Cancel = True
              MsgBox "GO AWAY!"
              Exit Sub
          End If
          
      End Sub

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        That is because Null<>string will not return TRUE, it will return Null.
        It was my bad, I thought that OpenArgs would be an empty string if not supplied, but it is null. Glad to hear you got it resolved yourself.

        Comment

        Working...