Results 1 to 11 of 11
  1. #1
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Closing a form through code (XP,2000)

    I have constructed my own menu form and password system so that when a certain text box is clicked, another text control (text 7) becomes visible asking for a password. I intended for a correct password (Steve) to close the main menu form and open the "system administration" form. However, it is not working quite right. The System administration form opens correctly but the "main menu" form does not close. Here is my code and it is an after update event of text 7 (the password text box).

    Private Sub Text7_AfterUpdate()
    'Attached to On Click event of go to sys admin


    Dim strPasswd
    strPasswd = Text7

    'Check to see if there is any entry made to input box, or if
    'cancel button is pressed. If no entry made then exit sub.

    If strPasswd = "" Or strPasswd = Empty Then
    MsgBox "No Input Provided", vbOKOnly, "Important Information"
    Exit Sub
    End If

    'If correct password is entered open System Administration form
    'If incorrect password entered give message and exit sub

    If strPasswd = "Steve" Then
    DoCmd.Close acForm, "main menu", acSaveNo

    DoCmd.OpenForm "system administration", acNormal
    Else
    MsgBox "Sorry, you do not have access to this form", vbOKOnly, "Important Information"
    Exit Sub
    End If

    End Sub

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Closing a form through code (XP,2000)

    It seems to me you are expending a fair bit of energy to duplicate that capabilities that Access User Security provides. Is there some capability you are trying to provide that is not part of the User Security feature?
    Wendell

  3. #3
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a form through code (XP,2000)

    Not necessarily. At this point in my Access life, I don't yet fully understand user level security so I am trying to do something that will be a temporary fix until I am comfortable with my understanding of user level security.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing a form through code (XP,2000)

    1. Access security is robust, while most home-grown security is easily bypassed or cracked. Access security isn't all that hard. Moderator <!profile=WendellB>WendellB<!/profile> has a tutorial on his website - you will find a link in his profile; look for Support > Tutorials > The Secrets of Security. It contains a short description of the essentials, and lots of useful links. I recommend the Security FAQ (the first link in the tutorial), it provides both an overview and lots of details.

    2. If you pass a non-existent name to DoCmd.Close, you won't get an error message. Make sure that you use the exact name of the form as it is in the database window. If it is the active form you are closing, you can also use

    DoCmd.Close acForm, Me.Name, acSaveNo

    That way, you always close the form that runs the code, even if you change its name.

  5. #5
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a form through code (XP,2000)

    Three things -
    1) Once again, my clumsy fingers typed the form name incorrectly so that is fixed.

    2) Thanks for the lead to Wendell's site - I am definitely implementing user level security

    3) In the meantime, I want to make this work if for no other reason than the learning experience plus the fact that I do intend to have some homegrown security that I do not believe is provided by Access' user level security. Specifically, the ability to cascade delete a record and all its associated data will require a password (known only to a supervisor) simply because there is so much data and it would be such a loss to delete it by mistake. This is why I decided to use a text control wioth a passwoed input mask. It works well except that if the user enters the wrong password, I want it to release focus and become non visible again. I am having trouble making the text control release focus (conceptually as releasefocus is not applicable to a text control).

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing a form through code (XP,2000)

    You "release focus" by setting focus to another control:

    Me.txtOtherTextbox.SetFocus
    Me.txtThisTextbox.Visible = False

  7. #7
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Closing a form through code (XP,2000)

    Sorry Hans, I should have mentioned that I knew how to set the focus to another textbox control then make the original one not visible. The problem I found is that my form only has labels visible. I did put in one textbox to become visible as needed to receive the password. Problem is that in order to change the focus to another textbox, it must be visible which I do not want. I tried to change the focus to a textbox that was not visible but I cannot. I also tried setting the focus to a label but I cannot (I didn't think it would work but tried anyway). So... what I have done is to make another textbox w/o a label, make it small and send it behind the menu label. Not elegant but it seems to give me the effect I want. Is there a better way?

    Also, Am I correct about user level security not being able to protect a command button (record deletion, e.g.)?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing a form through code (XP,2000)

    1. You *must* set focus to something else before making the text box invisible, and since labels can't receive the focus, you must indeed put another text box on the form. The solution you hit on is the only feasible one, I think.

    2. User level security can/revoke assign permissions at the level of forms, queries, tables etc., not at the level of individual controls on forms. But you *can* assign/revoke permissions to edit data in a table or query, and to insert records, and to delete records. There is no way to get around this. And you can write code to enable/disable command buttons based on the login name of the user.

  9. #9
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Closing a form through code (XP,2000)

    In addition to our tutorial, one of the Access Loungers has written a longer document that somewhat clearer than the Microsoft KBase articles - I'll try to dig it up. I had intended to revise our tutorial to include a link to it, but apparently it fell by the wayside.

    In the meantime, if you want to prevent unauthorized people from deleting records, security can be your solution too. Delete permissions on a table by table basis are part of the process. In fact, unless you really lock up your application, people can always delete from the table level, so having a passworded form isn't a real solid solution. Cascade deletes are handled by the database engine, so if the user has the ability to delete in both tables, then the delete will happen. Otherwise, the user gets an error message.
    Wendell

  10. #10
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Closing a form through code (XP,2000)

    The detailed exploration of Access User Security can be found at http://www.geocities.com/jacksonmacd - follow the links to the Access user-level security methods - you will need the Adobe PDF Reader or equivalent.
    Wendell

  11. #11
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Closing a form through code (XP,2000)

    In response to issue of setting focus so you can hide or disable textbox or other control, I recently had similar problem with dialog form used to export data to a Word document. When export sub runs, all visible command buttons need to be disabled so user cannot click them while export sub is running (a label next to image is updated to inform user of status of export procedure). I tried adding a tiny textbox to move the focus to, but it was still noticeable & looked like crap. So instead, simply added a tab control and cut & pasted all controls in form's detail section onto the tab control. When sub runs & buttons are disabled, focus is moved to the Page control. The code used looks like this:

    Private Sub Export_btn_Click()

    With Me
    .Page1.SetFocus
    .EXPORT_btn.Enabled = False
    .CANCEL_btn.Enabled = False
    .Options_btn.Enabled = False
    ExportDoc 'Export sub
    End With

    End Sub

    See attached pic for example of what form looks like when "Export" button is clicked and focus is set to Page1 of tab control. This looks a lot better than the textbox did. Depending on how your form is designed, this may or may not be practical solution for this issue.

    HTH
    Attached Images Attached Images

Posting Permissions

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