为什么出现错误消息框而没有错误?

时间:2018-08-14 19:20:31

标签: vba ms-access access-vba

我编写的代码在两个位置查找特定的文件夹。如果找不到该文件夹​​,我希望错误消息让人们知道该文件尚未数字化。但是,即使找到该文件夹​​,也会显示错误消息。如何使最终错误消息仅在出现实际错误时出现。

Private Sub Command128_Click()

    Dim sPath As String

    sPath = "w:\EDU UNDERGRADRecords\Cert Majors\Level 1\"
    sPath = sPath & Screen.ActiveForm![DigitalFile] & "\"

    On Error Resume Next
    Application.FollowHyperlink sPath

    Dim sPath2 As String

    sPath2 = "w:\EDU UNDERGRADRecords\Cert Majors\Level 2\"
    sPath2 = sPath2 & Screen.ActiveForm![DigitalFile] & "\"

    On Error GoTo Error1
    Application.FollowHyperlink sPath2

Exit_Command128_Click:

    Exit Sub


Error1:
     MsgBox "Student File has not been digitized"
     Resume Exit_Command128_Click:

End Sub

1 个答案:

答案 0 :(得分:0)

代码以当前设置的方式正常工作。设置的方法是始终在每种情况下运行,因为在当前设置之间没有中断或退出。

但是,对于这种情况,使用IF语句肯定可以更轻松地控制它。

像这样:

...

sPath2 = "w:\EDU UNDERGRADRecords\Cert Majors\Level 2\"
sPath2 = sPath2 & Screen.ActiveForm![DigitalFile] & "\"

If Dir(sPath2) = "" Then
    MsgBox "Student File has not been digitized"
Else
    Application.FollowHyperlink sPath2
End If

End Sub

此外,如果要对上一个被调用的语句执行相同的操作,请按以下步骤操作:

...
sPath = "w:\EDU UNDERGRADRecords\Cert Majors\Level 1\"
sPath = sPath & Screen.ActiveForm![DigitalFile] & "\"

If Dir(sPath) = "" Then
    MsgBox "Student File has not been digitized"
    'Exit Sub 'An ejection (if required for a single failure)
Else
    Application.FollowHyperlink sPath
End If

仅供参考-您仅声明了sPath,但没有声明sPath2