2013-11-21 30 views
1

好的,我對隱藏和顯示用戶窗體有疑問。
This link已經爲我回答了。
爲什麼用戶窗體隱藏並多次顯示後會凍結

問題是我遇到另一個問題。
當我回到Userform1它凍結,我什麼都做不了。
爲什麼?我是否需要在代碼中添加內容?

繼承人我使用的代碼的summay:
此代碼提示用戶輸入用戶名和密碼

Option Explicit 
Private Sub CBu_Login_Click() 
Dim ws As Worksheet, rng As Range, lrow As Long, find_value As String 
Dim cel As Range 

Set ws = ThisWorkbook.Sheets("UserName") 
lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row 
Set rng = ws.Range("A2:A" & lrow) 
find_value = Me.TB_Username.Value 
Set cel = rng.Find(What:=find_value, After:=ws.Range("A2"), LookIn:=xlFormulas, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False) 
If Not cel Is Nothing Then 
    If Me.TB_Password.Value = cel.Offset(0, 1).Value Then 
     Me.Hide 
     UF_Encoding.L_User.Caption = "Welcome " & cel.Offset(0, 2).Value & "!" & " You are logged in." 
     UF_Encoding.TB_Operator.Text = cel.Offset(0, 2).Value 
     UF_Encoding.TB_ESN_IMEI.Value = "" 
     UF_Encoding.CB_PrimaryCode.Value = "" 
     UF_Encoding.CB_SecondaryCode.Value = "" 
     UF_Encoding.TB_Remarks.Value = "" 
     UF_Encoding.TB_ESN_IMEI.SetFocus 
     UF_Encoding.Show 
    Else 
     MsgBox "Invalid Username/Password" 
    End If 
Else 
    MsgBox "Invalid Username/Password" 
End If 

End Sub 

該代碼可用於註銷:
我用列表框在這裏所以用戶可以選擇要採取的操作。

Private Sub LB_Options_AfterUpdate() 

If Me.LB_Options.Value = "Log out" Then 
    Me.Hide 
    Me.LB_Options.Visible = False 
    UF_Login.TB_Username.Value = "" 
    UF_Login.TB_Password.Value = "" 
    UF_Login.Show 

ElseIf Me.LB_Options.Value = "Change Password" Then 
    Me.Hide 
    Me.LB_Options.Visible = False 
    UF_Changepass.TB_User.Value = "" 
    UF_Changepass.TB_Newpass.Value = "" 
    UF_Changepass.TB_Oldpass.Value = "" 
    UF_Changepass.Show 

ElseIf Me.LB_Options.Value = "Exit" Then 
    Me.Hide 
    wbDbase.Save 
    wbDbase.Close 
    wbEncoding.Save 
    wbEncoding.Close 
    Unload UF_Login 
    Unload UF_Changepass 
    Unload Me 
End If 

那麼這就是我想要的。登錄,註銷,更改通行證並退出。
但正如我所說的,表單在第一次執行後會凍結。

實施例:
1.我初始化UF_Login然後UF_Encoding出現。
2.它的作品,所有的命令按鈕和文本框的作品。
3.然後我使用列表框註銷。
4.當我再次登錄時,它會顯示UF_Encoding,但是當我嘗試使用commanb按鈕和文本框時,它不起作用。
5.奇怪的是,與註銷列表框,更改通過和退出工程。

我真的很難搞清楚爲什麼。
任何幫助表示讚賞。

+0

您的問題的一部分是,您從表單中調用表單,這可能會很快變得棘手,通常會導致同一表單的多個實例 - 通常會出現意外的結果。在常規模塊中從宏調用表單可確保更好的控制。參見[This](http://peltiertech.com/Excel/PropertyProcedures.html)獲取一些指導。 – DaveU

+0

是的,這就是我的想法。但我希望也許有辦法解決它。我避免在reg模塊上做一個宏,因爲我試圖完成的只是向用戶顯示錶單而不是工作表。 – L42

+0

即便如此,您應該可以通過控制您的表單流量來實現這一點。 – DaveU

回答

0

要允許UserForms之間的多個開關,最好卸載未使用的表單,然後在需要重新使用的時候重新加載它。喜歡的東西:

Me.Hide '/* hide the initiating form first */ 
Load UF_Login '/* loads the form, but not shown */ 
With UF_Login 
    .TB_Username.Value = "" 
    .TB_Password.Value = "" 
    .Show 
End With 
Unload Me '/* unload the initiating form */ 

UF_Login,一個代碼,查看UF_Encoding將被添加到使它看起來像你實際上是登錄和註銷形式。

Private Sub CB_Login_Click() 

    '/* code to check log-in credentials goes here */ 
    Me.Hide 
    Load UF_Encoding 
    UF_Encoding.Show 
    Unload Me 
End Sub 
0

試試看。這是「粗糙」的代碼,當然需要改進,但它很有用,並且應該給你一些想法。

這正好UF_Encoding

Option Explicit 
'UF_Encoding form 
Private msLBox As String 
Private msUser As String 
Public Property Let psUser(s As String) 
    msUser = s 
End Property 
Public Property Get psLBox() As String 
    psLBox = msLBox 
End Property 

Private Sub LB_Options_AfterUpdate() 
msLBox = Me.LB_Options.Value 

If Me.LB_Options.Value = "Log out" Then 
    Me.Hide 
    Me.LB_Options.Visible = False 
    UF_Login.TB_Username.Value = "" 
    UF_Login.TB_Password.Value = "" 

ElseIf Me.LB_Options.Value = "Change Password" Then 
    Me.Hide 
    Me.LB_Options.Visible = False 
    UF_Changepass.TB_User.Value = "" 
    UF_Changepass.TB_Newpass.Value = "" 
    UF_Changepass.TB_Oldpass.Value = "" 

ElseIf Me.LB_Options.Value = "Exit" Then 
    Me.Hide 
    ' wbDbase.Save 
    ' wbDbase.Close 
    ' wbEncoding.Save 
    ' wbEncoding.Close 
    ' Unload UF_Login 
    ' Unload UF_Changepass 
    ' Unload Me 
    End If 
End Sub 

Private Sub UserForm_Activate() 
    Me.L_User.Caption = "Welcome " & msUser & "!" & " You are logged in." 
    Me.TB_Operator.Text = msUser 
    msLBox = "" 'reset each time form re-entered 
    Me.LB_Options.Visible = True 
End Sub 

Private Sub UserForm_Initialize() 
    Me.LB_Options.AddItem "Log out" 
    Me.LB_Options.AddItem "Change Password" 
    Me.LB_Options.AddItem "Exit" 

    Me.TB_ESN_IMEI.Value = "" 
    Me.CB_PrimaryCode.Value = "" 
    Me.CB_SecondaryCode.Value = "" 
    Me.TB_Remarks.Value = "" 
    Me.TB_ESN_IMEI.SetFocus 
End Sub 

這正好UF_Login

Option Explicit 
'UF_Login form 
Private msUser As String 
Public Property Get psUser() As String 
    psUser = msUser 
End Property 

Private Sub CBu_Login_Click() 
    Dim ws As Worksheet, rng As Range, lrow As Long, find_value As String 
    Dim cel As Range 

    Set ws = ThisWorkbook.Sheets("UserName") 
    lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row 
    Set rng = ws.Range("A2:A" & lrow) 
    find_value = Me.TB_Username.Value 
    Set cel = rng.Find(What:=find_value, After:=ws.Range("A2"), LookIn:=xlFormulas, _ 
     LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 
    If Not cel Is Nothing Then 
     If Me.TB_Password.Value = cel.Offset(0, 1).Value Then 
      msUser = cel.Offset(0, 2).Value 'save user name 
      Me.Hide 
     Else 
      MsgBox "Invalid Username/Password" 
     End If 
    Else 
     MsgBox "Invalid Username/Password" 
    End If 

End Sub 

這正好UF_Changepass

Option Explicit 
'UF_Changepass form 
Private Sub CMDok_Click() 
    Dim ws As Worksheet, rng As Range 
    Set ws = ThisWorkbook.Sheets("UserName") 
    ws.Columns("B:B").Find(Me.TB_Oldpass, , xlValues, xlWhole, , , False).Value = Me.TB_Newpass 
    Me.Hide 
End Sub 

Private Sub UserForm_Click() 

這個代碼放在一個普通模塊

Option Explicit 
    Dim fLogin As UF_Login 
    Dim fEnc As UF_Encoding 
    Dim fChg As UF_Changepass 

Sub main() 
    Dim s As String 
' initialize all 3 forms 
    Set fLogin = New UF_Login 
    Set fEnc = New UF_Encoding 
    Set fChg = New UF_Changepass 

    fLogin.Show '1st time 

' re-display main form until done 
    Do 
     fEnc.psUser = fLogin.psUser 'pass user name to main form 
     fEnc.Show 
     s = fEnc.psLBox 'get listbox value 
     If s <> "Exit" Then showAuxForms s 
    Loop Until s = "Exit" 

' done with forms 
    Unload fLogin 
    Unload fEnc 
    Unload fChg 
    Set fLogin = Nothing 
    Set fEnc = Nothing 
    Set fChg = Nothing 
End Sub 

Sub showAuxForms(s As String) 
    If s = "Log out" Then 
     fLogin.Show 
    ElseIf s = "Change Password" Then 
     fChg.Show 
    End If 
End Sub 
+0

是的,我會嘗試這一個。仍然無法通過觀察來掌握代碼。也是我第一次嘗試「公共財產」。這是我瞭解最少的地方,並且很難找到代碼。無論如何,我會做額外的搜索,並儘快給你反饋。 – L42

+0

Property Let&Property Get是一種在窗體和宏之間傳遞數據的方法。你可以通過聲明一個公共變量來實現,但是這種方法可以讓你更好地控制。您可能會發現[THIS](http://peltiertech.com/Excel/PropertyProcedures.html)有幫助。 – DaveU

相關問題