-2

此代码用于更新数据库,但每次单击“开始更新”按钮时,都会显示“未找到路径”错误。

Dim strEmpFileName As String
Dim strBackSlash As String
Dim intEmpFileNbr As Integer
Dim strEmpFileName1 As String
Dim strBackSlash1 As String
Dim intEmpFileNbr1 As Integer
Dim fPath As New FileSystemObject
Dim strEmpFileName2 As String
Dim strBackSlash2 As String
Dim intEmpFileNbr2 As Integer

Dim strEmpFileName21 As String
Dim strBackSlash21 As String
Dim intEmpFileNbr21 As Integer

Dim strEmpFileName21X As String
Dim strBackSlash21X As String
Dim intEmpFileNbr21X As Integer

Dim strEmpFileName21s As String
Dim strBackSlash21s As String
Dim intEmpFileNbr21s As Integer


strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "\SOURCE\SWA.exe"
txtSource.Text = strEmpFileName

FileCopy txtSource.Text, "\\Mainfile\SSMS_UPDATE\SHIPS ACCOUNTING\SWA.exe"
FileCopy txtSource.Text, "C:\SANKO PROGRAM\SPECIAL WORK\SWA.exe"
4

2 回答 2

1

由于您已经在检查反斜杠

strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "\SOURCE\SWA.exe"

您不应该在开头需要反斜杠"\SOURCE\SWA.exe"

于 2013-07-10T01:16:52.520 回答
-1

您在滥用 IIf 函数。语法, IIf(expr, truepart, falsepart)。您的语句检查反斜杠,如果最后一个字符是“\”,则将变量设置为空字符串。但是如果它不是路径中的最后一个字符,则错误部分会将变量设置为“\”。例如,如果 App.Path = C:\MyApplication您的 IIF 函数将设置 strBackSlash = "\" 并且 strEmpFileName 将是C:\MyApplication\\SOURCE\SWA.exe。对于您想要使用常规If语句的代码用空字符串替换反斜杠字符,然后在构建路径时使用硬编码的反斜杠。

strAppPath = App.Path
If(Right$(strAppPath, 1) = "\" Then
    strAppPath = Left$(strAppPath, Len(strAppPath) - 1)
End Id
strEmpFileName = strAppPath & "\SOURCE\SWA.exe"

完整的MSDN 文档在这里

此外,获取应用程序路径需要做很多工作。我建议您编写自己的函数来执行此操作并将其添加到项目 .bas 文件中。然后从需要的地方调用该函数,并且返回的路径格式(带有或不带有尾随反斜杠)是一致的。我的个人功能确保我有一个尾随反斜杠。

Public Function AppPath() As String
    Dim sAppPath As String

    sAppPath = App.Path
    If Right$(sAppPath, 1) <> "\" Then  'check that I'm not in the root
        sAppPath = sAppPath & "\"
    End If

    AppPath = sAppPath

End Function

用途: strEmpFileName = AppPath() & "SOURCE\SWA.exe"

于 2013-07-10T14:59:32.723 回答