Results 1 to 7 of 7

Thread: Linking to a Work sheet from a User form

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6

    Linking to a Work sheet from a User form

    I have a user form with a command button on it and a to text boxes for a User name and a password.

    I want to be able to get the userform to go to a sheet in the workbook and check to see if the data entered in the texboxes matches an entry in the cells, and if it does to open a new user form with the .show command


    Also



    I want to stop a user from Closing a VBA User Form in Excel. Is there a way of continuingly opening the window until the user hits a certain button. If they close it down and the button is not clicked then it reopens


    Can some one help me?

  2. #2
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    First part is simple.

    Code for your button click event
    VB Code:
    1. Private Sub CommandButton1_Click()
    2. Dim username As String
    3. Dim password As String
    4. username = Sheets(1).Range("A1")
    5. password = Sheets(1).Range("B1")
    6.  
    7. If TextBox1.Text = username Then
    8.      If TextBox2.text = password Then
    9.      me.hide
    10.      newform.show
    11.      End If
    12. End If
    13. End Sub


    As to your second part, I don't think there is any method that stops the user from closing the form in VBA. I'd be interested if anyone knows how this would be done.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  3. #3
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    This partly answers your second question, which is to stop the user form from being cancelled.

    This code will disable the X button on the top of the form.


    VB Code:
    1. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    2. If CloseMode = 0 Then Cancel = True
    3. End Sub


    Unfortunately does bring on a system message box telling the user to use the cancel button. Anyone know how we can change or eliminate this message?
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6
    Il try out the second part of coding and see what happens:


    Meanwhile this is what Iv been trying to do:

    [Highlight=VB]
    Sub Form_Unload()
    If Sheets(2).Range("G4") = "No" Then
    Login_Form.Show
    Else
    Login_Form.Hide
    End If
    End Sub
    [Highlight=VB]

    I have set up a module with the above code in it and the Reference No refers to a Work book sheet. I have an auto loading User form that inputs "No" when it loads to G4 so my idea is that this module could check to see if the value is No and if the form in unloaded it reopens the User Form unless the value is "Yes", which will keep the form closed.

    At the moment the code does not work, I dont think the Unload part is correct. Any ideas

  5. #5
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    I don't think there is a form unload event to start with.
    Excel supports a form deactivate and a form terminate event.
    You might want to give these a try and see what you come up with. I

    Also - if this form is just a login screen, you really should clear it from memory and not simply hide it. Otherwise it will still gobble up some precious bites on your system
    Rather than
    Form.hide
    Use
    Unload Me
    Last edited by TheFIDDLER; Mar 7th, 2004 at 12:40 PM.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6
    Ok. Will that improve performance?


    I heard i may have to code it using API calls, sounds hard

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6
    That No close code works brilliantly. no message box pops up and it is secure nice one

Posting Permissions

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



Click Here to Expand Forum to Full Width