2017-03-14 23 views
0

我有一個登錄屏幕,用於控制對主導航窗體中按鈕的訪問。我希望用戶安全級別確定哪些按鈕是可見的。我的問題是,如果我嘗試添加多個button.visible = False,那麼表單會中斷,除了我試圖隱藏的頂部按鈕外,我會看到其他所有按鈕。我在編碼VBA方面很新穎(上週我基本上只是買了一本書),而我是hopihn,我只是缺少一些簡單的東西。誰能幫忙?代碼如下。使用多個button.visible = False訪問VBA

Private Sub Command1_Click() 
Dim User As String 
Dim UserLevel As Integer 
Dim TempPass As String 
Dim ID As Integer 
Dim workerName As String 
Dim TempLoginID As String 
Dim SecLevel As String 

If IsNull(Me.txtUserName) Then 
    MsgBox "Please enter UserName", vbInformation, "Please Enter your HealthcareID this is the same Id you use to log into windows" 
    Me.txtUserName.SetFocus 
ElseIf IsNull(Me.txtPassword) Then 
    MsgBox "Please enter Pin Number", vbInformation, "Please enter your Pin Number" 
    Me.txtPassword.SetFocus 
Else 
    If IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And password = '" & Me.txtPassword.Value & "'")) Then 
     MsgBox "Invalid UserName or Password! Use your HealthcareID as your username and your pin code to access." 
    Else 
     TempLoginID = Me.txtUserName.Value 
     workerName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") 
     UserSecurity = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") 
     TempPass = DLookup("[password]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") 
     Unit = DLookup("[Unit]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") 
     Unit = DLookup("[Unit]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") 
     DoCmd.Close 
      Select Case UserSecurity 
       Case Is = 1 'Admins 
       DoCmd.OpenForm "Navigation Form" 
       Forms![Navigation Form]![TxtUser] = workerName 
       Forms![Navigation Form]![txtName] = TempLoginID 
       Forms![Navigation Form]![txtUnit] = Unit 
       Forms![Navigation Form]![txtUserSecurity] = UserSecurity 
      ' DoCmd.BrowseTo acBrowseToForm, "frmFirstPage", "Navigation Form.NavigationSubForm", , , acFormEdit 
       Case Is = 2 'Managers 
       DoCmd.OpenForm "Navigation Form" 
       Forms![Navigation Form]![TxtUser] = workerName 
       Forms![Navigation Form]![txtName] = TempLoginID 
       Forms![Navigation Form]![txtUnit] = Unit 
       Forms![Navigation Form]!AdminPageButton.Visible = False 
       Forms![Navigation Form]![txtUserSecurity] = UserSecurity 
      ' DoCmd.BrowseTo acBrowseToForm, "frmFirstPage", "Navigation Form.NavigationSubForm", , , acFormEdit 
       Case Is = 3 'Coordinators 
       DoCmd.OpenForm "Navigation Form" 
       Forms![Navigation Form]![TxtUser] = workerName 
       Forms![Navigation Form]![txtName] = TempLoginID 
       Forms![Navigation Form]![txtUnit] = Unit 
       Forms![Navigation Form]![txtUserSecurity] = UserSecurity 
       Forms![Navigation Form]!btnManagersMenu.Visible = False 
       Forms![Navigation Form]!AdminPageButton.Visible = False 
      ' DoCmd.BrowseTo acBrowseToForm, "frmFirstPage", "Navigation Form.NavigationSubForm", , , acFormEdit 
       Case Else 'RCRs 
       DoCmd.OpenForm "Navigation Form" 
       Forms![Navigation Form]![TxtUser] = workerName 
       Forms![Navigation Form]![txtName] = TempLoginID 
       Forms![Navigation Form]![txtUnit] = Unit 
       Forms![Navigation Form]![txtUserSecurity] = UserSecurity 
       Forms![Navigation Form]!AdminPageButton.Visible = False 
       Forms![Navigation Form]!btnManagersMenu.Visible = False 
       Forms![Navigation Form]!btnCoordMenu.Visible = False 
      ' DoCmd.BrowseTo acBrowseToForm, "frmFirstPage", "Navigation Form.NavigationSubForm", , , acFormEdit 
      End Select 
    End If 
End If 
End Sub 

回答

0

我是很新的訪問和VBA自己,所以沒有看到你的導航形式設置,我不明白爲什麼你的後續按鈕保持可見,當你打開表單這樣。但是,您可以嘗試將Select Case UserSecurity代碼移動到導航表單的Form_Load子目錄中 - 我在Form_Load和Form_Current子目錄中執行與我的表單類似的操作,並且我有多個命令按鈕,文本字段等根據不同而變得可見或不可見標準,包括用戶安全級別。這是一分我一個表格上使用的一個例子 -

Private Sub Form_Current() 
On Error GoTo Form_Current_Err 

Me.txtOrderStatus.Requery 'refresh the order status 

'move the cursor to the end of the PO No field 
Me.txtPONo.SetFocus 
If Not IsNull(Me.txtPONo) Then 
Me.txtPONo.SelStart = Len(Me.txtPONo) 
End If 

'make the contract no and project description fields visible only when the PO is related to a contract 
If txtPOType.Value = "Contract" Then 
     Me.txtContractNo.Visible = True 
     Me.txtProjectDescription.Visible = True 
     Me.txtParentPO.Visible = False 
     Me.POTabs.Pages(6).Visible = False 
ElseIf Me.txtPOType.Value = "Call Off Order" Then 'make the parent po visible if this is a call off order 
     Me.txtContractNo.Visible = False 
     Me.txtProjectDescription.Visible = False 
     Me.txtParentPO.Visible = True 
     Me.POTabs.Pages(6).Visible = True 

Else 
     Me.txtContractNo.Visible = False 
     Me.txtProjectDescription.Visible = False 
     Me.txtParentPO.Visible = False 
     Me.POTabs.Pages(6).Visible = False 
End If 

'check if the order is cancelled and hide all action buttons 
If Me.OrderStatus = "Cancelled" Then 
Me.cmdEditPO.Visible = False 
Me.cmdCancelOrder.Visible = False 
Me.txtCancelledOrderFlag.Visible = True 
Me.AmendedOrderFlag.Visible = False 
Else 
Me.cmdEditPO.Visible = True 
Me.txtCancelledOrderFlag.Visible = False 

End If 

'check if this a revised order and hide the Amended Order comment and the PO Revisions subform if it isn't 
If txtPORevision > 0 Then 
    If Me.OrderStatus <> "Cancelled" Then 
    Me.AmendedOrderFlag.Visible = True 
    Me.PO_Revisions.Visible = True 
    Else 
    Me.AmendedOrderFlag.Visible = False 
    Me.PO_Revisions.Visible = True 
    End If 
Else 
    Me.AmendedOrderFlag.Visible = False 
    Me.PO_Revisions.Visible = False 
End If 

'check if the PO needs approval and the user is a PO Approver, 
' and make the approve/cancel buttons visible as appropriate 
If Me.OrderStatus = "For Approval" And _ 
    (DLookup("Role", "dbo_Employees", "EmployeeID = " & TempVars!EmpID) = "Administrator" _ 
    Or DLookup("Role", "dbo_Employees", "EmployeeID = " & TempVars!EmpID) = "Manager") Then 
    'show the Approve PO button, hide the Cancel button 
    Me.cmdApproveOrder.Visible = True 
    Me.cmdCancelOrder.Visible = False 
Else 
'hide the Approve button, show the Cancel button 
    Me.cmdApproveOrder.Visible = False 
    If Me.OrderStatus <> "Cancelled" Then 
    Me.cmdCancelOrder.Visible = True 
    End If 
End If 

'if the order hasn't been approved yet, or is cancelled, hide the email button 
If Me.OrderStatus = "New" Or Me.OrderStatus = "Raised" Or Me.OrderStatus = "For Approval" _ 
Or Me.OrderStatus = "Not Approved" Or Me.OrderStatus = "Cancelled" Then 
    Me.cmdEmailPO.Visible = False 
Else 
    Me.cmdEmailPO.Visible = True 
End If 

'amend the caption of the Revise order button if the order has not been sent yet 
If Me.OrderStatus = "New" Or Me.OrderStatus = "Raised" Then 
Me.cmdEditPO.Caption = "Revise/Send Order" 
Else 
Me.cmdEditPO.Caption = "Revise this Order" 
End If 

'check if a filter is applied and highlight the filter button if it is 
If Me.FilterOn = True Then 
Me.cmdApplyFilter.BackColor = vbYellow 
Else 
Me.cmdApplyFilter.BackThemeColorIndex = 4 
Me.cmdApplyFilter.BackShade = 40 
Me.cmdApplyFilter.Gradient = 12 
End If 

'refresh the subform 
DoCmd.Requery "PO Details" 

還可以看出你在任何情況下多次設置txtUser,txtName的,txtUnit和txtUserSecurity爲相同的值,因此而不是宣稱他們像這樣,您可以將它們設置爲導航窗體上控件的默認值。您可以將TempLoginID聲明爲TempVar,以便您可以在整個數據庫中使用它,然後可以將dlookups放在需要它們的任何位置,或者讓您設置表單的數據以包含User表你可以直接訪問它們而不需要查看。

希望有所幫助!

+0

根據你的回答,我能夠找到我在代碼中犯的一個錯字。謝謝! –

+0

很高興我能幫忙! – JennyW