Consulting

Results 1 to 2 of 2

Thread: trouble selecting sheet

  1. #1

    trouble selecting sheet

    hi im using this code to log into as workbook but im having trouble with selecting a worksheet once the userform is selected.

    when the user form closes i would like to select the home sheet and insert the users name in cell 2,3 and the time in cell 2,4

    could someone let me know where im going wrong please.

    thank you

    Private Sub cmdLogin_Click()
        Dim RowNo As Long
        Dim Id As String, pw As String
        Dim ws As Worksheet
        Dim aCell As Range
    
        On Error GoTo ErrorHandler
    
        If Len(Trim(cboUserName)) = 0 Then
            cboUserName.SetFocus
            MsgBox "Username cannot be empty", _
            vbOKOnly, "Username Error"
            Exit Sub
        End If
    
        If Len(Trim(txtPassword)) = 0 Then
            txtPassword.SetFocus
            MsgBox "Password cannot be empty", _
          vbOKOnly, "Password error"
            Exit Sub
        End If
    
        Application.ScreenUpdating = False
    
        Set ws = Worksheets("Users")
        Id = LCase(Me.cboUserName)
    
        Set aCell = ws.Columns(2).Find(What:=Id, LookIn:=xlValues, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    
        '~~> If match found
        If Not aCell Is Nothing Then
            RowNo = aCell.Row
            If Me.txtPassword = aCell.Offset(, 1) Then
                Unload Me
            Else
                MsgBox "Unable to match User or Password, Please try again", _
                vbOKOnly, "Password Error"
            End If
        Else '<~~ If not found
            MsgBox "Unable to match User or Password, Please try again", _
            vbOKOnly, "Password Error"
        End If
    CleanExit:
        Set ws = Nothing
        Application.ScreenUpdating = True
        Exit Sub
    ErrorHandler:
        MsgBox Err.Description
        Resume CleanExit
        
    Sheets("Home").Select
    
    End Sub
    Last edited by SamT; 03-27-2014 at 04:46 PM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
         '~~> If match found
        If Not aCell Is Nothing Then 
            RowNo = aCell.Row 
            If Me.txtPassword = aCell.Offset(, 1) Then 
                GoTo CleanExit   '<------------------------------------------------------------------
            Else 
                MsgBox "Unable to match User or Password, Please try again", _ 
                vbOKOnly, "Password Error" 
            End If 
        Else '<~~ If not found
            MsgBox "Unable to match User or Password, Please try again", _ 
            vbOKOnly, "Password Error" 
        End If 
    CleanExit: 
        Set ws = Nothing 
        Application.ScreenUpdating = True 
        Exit Sub 
        Sheets("Home").Select   '<----------------------------------------------------------
    
    ErrorHandler: 
        MsgBox Err.Description 
        Resume CleanExit 
         
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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