2014-09-24 25 views
0

我一直在拖網堆棧溢出的一些支持和協助,並找到了下面的代碼。我已將其更改爲我的規格。我想要VBA代碼做的事情是,當我點擊登錄時,我希望它搜索名爲「訪問列表」的工作表並確認用戶是否在工作表中找到。如果找到用戶,則顯示錶單(拉式打印,推式打印,USB,閾值和站點觸點)。一旦它完成了這項任務,我就會喜歡它隱藏「歡迎」頁面。Excel VBA登錄並顯示一組頁數

我已經能夠編碼這部分,但我正在努力的部分是,如果用戶離開密碼字段的用戶名&空白,他們只需點擊登錄。我看到的消息是

對象不支持此屬性或方法

是否有人可以幫助我嗎?

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(txtlogin)) = 0 Then 
    txtlogin.SetFocus 
    MsgBox "Username cannot be empty" 
    Exit Sub 
End If 

If Len(Trim(txtpassword)) = 0 Then 
    txtpassword.SetFocus 
    MsgBox "Password cannot be empty" 
    Exit Sub 
End If 

Application.ScreenUpdating = False 

Set ws = Worksheets("Access List") 
Id = LCase(Me.txtlogin) 

Set aCell = ws.Columns(1).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 
    Else 
    Sheets("Home").Visible = xlSheetHidden 
    Sheets("Pull Print").Visible = True 
    Sheets("Push Print").Visible = True 
    Sheets("USB").Visible = True 
    Sheets("Thresholds").Visible = True 
    Sheets("Site Contacts").Visible = True 
    Sheets("Access List").Visible = xlSheetHidden 

     MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly 
    End If 

Else '<~~ If not found 

    MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly 

End If 
CleanExit: 
Set ws = Nothing 
Application.ScreenUpdating = True 
Exit Sub 
ErrorHandler: 
MsgBox Err.Description 
Resume CleanExit 
End Sub 

回答

0

您不一致地訪問txtlogin字段。有時使用Me.,有時不使用。

假設你是一個工作簿這條線內,並在窗體模塊更換:

Id = LCase(Me.txtlogin) 

要這樣:

Id = LCase(txtlogin) 

如果不是,改變你的if語句是這樣的:

If Len(Trim(Me.txtlogin)) = 0 Then 
    ... 
If Len(Trim(Me.txtpassword)) = 0 Then 
    .... 

您可以刪除您的On Error行來找出哪條線路正在拋出錯誤。

+0

我現在已經改變了我的代碼,以反映這一點,當我點擊登錄,它仍然沒有取消隱藏 – 2014-09-25 08:36:03

0

如果按鈕和文本框駐留在窗體中,則不應出現此錯誤。
假設那些居住在工作表中,所以:

代替:

txtlogin.SetFocus 
txtpassword.SetFocus 

使用這些:

txtlogin.Activate 
txtpassword.Activate 

追加答:
因此,假設先前的錯誤後消失使用上面,這裏是完整的東西(標記爲答案,如果這是你在找什麼):

Private Sub CommandButton1_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 txtlogin.Value = "" Then 
txtlogin.Activate 
MsgBox "cant be empty" 
Exit Sub 
End If 

If txtpassword.Value = "" Then 
txtpassword.Activate 
MsgBox "cant be empty" 
Exit Sub 
End If 

Application.ScreenUpdating = False 

Set ws = Worksheets("Access List") 
Id = LCase(Me.txtlogin) 
'MsgBox Id 
pw = Me.txtpassword 

Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

'If UserID match found 
If Not aCell Is Nothing Then 
RowNo = aCell.Row 
If pw = aCell.Offset(, 1) Then 
MsgBox "user : " & Id & " logged in" 
txtpassword.Value = "" 
txtlogin.Value = "" 

'do your sheet visibility on off here, like these: 
Sheets("USB").Visible = True 
Sheets("Access List").Visible = xlSheetHidden 
Else 
txtpassword.Value = "" 
MsgBox "Hello " & Id & " ,Password did not match, Please try again", vbOKOnly 
txtpassword.Activate 
End If 

Else 'If no match found 

MsgBox "Unable to match UserID, Please try again", vbOKOnly 
txtlogin.Value = "" 
txtpassword.Value = "" 
txtlogin.Activate 
'MsgBox "Wrong Password, Please try again", vbOKOnly 
End If 
CleanExit: 
Set aCell = Nothing 
Set ws = Nothing 
Application.ScreenUpdating = True 
Exit Sub 
ErrorHandler: 
MsgBox Err.Description 
Resume CleanExit 
End Sub 
+0

現在我已經實現這一點,但現在,當我加人到我的訪問列表,它仍然沒有在登錄他們的網頁 – 2014-09-25 11:37:33