MS Access if click事件声明

时间:2018-11-19 15:31:09

标签: vba ms-access

我正在使用Ms Access表单,并且创建了一个on事件,用于定位文件夹位置,但是现在我想根据不同的条件来定位文件夹位置,但是当我添加if语句时,它需要一个子,函数或属性。以下是一些演示代码。我真的希望有人能解释什么缺失?

    Private Sub Open_Email_Click()
    Dim stAppName As String
    Dim stAppNameA As String
    Dim stAppNameB As String
    stAppName  = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B " & Me.BC & " " & Me.UC & "\"
    stAppNameA = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\A\B " & Me.BC & " " & Me.UC & "\"
    stAppNameB = "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & " DEMO\B\B " & Me.BC & " " & Me.UC & "\"

    If (Me.BC = "60") And Me.UC Like "REF123*" Then stAppNameA
    ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then stAppNameB
    Else: stAppName
    End If

Call Shell(stAppName, 1) 
End Sub

2 个答案:

答案 0 :(得分:3)

我认为您的函数的逻辑可以简化为以下内容,这样可以减少重复的表达,从而提高可读性:

Private Sub Open_Email_Click()
    Dim strTmp As String

    If Me.BC = "60" Then
        If Me.UC Like "REF123*" Then
            strTmp = " DEMO\A\B "
        Else
            strTmp = " DEMO\B\B "
        End If
    Else
        strTmp = " DEMO\B "
    End If
    Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub

或者,使用Select Case语句:

Private Sub Open_Email_Click()
    Dim strTmp As String

    Select Case True
        Case Me.BC <> "60"
            strTmp = " DEMO\B "
        Case Me.UC Like "REF123*"
            strTmp = " DEMO\A\B "
        Case Else
            strTmp = " DEMO\B\B "
    End Select
    Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)
End Sub

要测试生成的路径,请更改:

Call Shell("C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\", 1)

收件人:

Debug.Print "C:\Windows\explorer.exe C:\DEMO\TEST\" & Me.Office & strTmp & Me.BC & " " & Me.UC & "\"

答案 1 :(得分:1)

我认为您的If块在换行符和连续字符(:)的位置有点混乱。尝试像这样重新格式化代码:

    If (Me.BC = "60") And Me.UC Like "REF123*" Then 
        stAppName =stAppNameA
    ElseIf (Me.BC = "60") And Not Me.UC Like "REF123*" Then 
        stAppName = stAppNameB
    Else 
        stAppName =stAppName
    End If

    Call Shell(stAppName, 1)