使用Multiple button.visible = False访问VBA

时间:2017-03-14 16:10:16

标签: access-vba access

我有一个登录屏幕,用于控制对主导航表单中按钮的访问。我希望用户安全级别确定哪些按钮可见。我的问题是,如果我尝试添加多个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

1 个答案:

答案 0 :(得分:0)

我自己是Access和VBA的新手,所以如果没有看到导航表格的设置,我不太清楚为什么当您打开这样的表格时,后续按钮仍然可见。但是,您可以尝试将Select Case UserSecurity代码移动到导航表单的Form_Load子中 - 我在Form_Load和Form_Current subs中执行与我的表单类似的操作,并且我有多个命令按钮,文本字段等变得可见或不可见,具体取决于不同标准,包括用户安全级别。这是我在一个表单上使用的子示例 -

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表您可以直接访问它们而无需dlookup。

希望有所帮助!