Hi I am executing a statement which calls ftp.exe. I am unsure how to error handle. It works when the proper user name and user id are entered. But if wrong user id or password is entered I have no error handling. How can I capture the output of the attempted ftp? And determine if the login/file transfer was successful?
Here's the code:
Private Sub cmdUpload_Click()
On Error Resume Next
Dim sResult As String, sFile As String
Dim sSVR As String, sFLD As String
Dim sUID As String, sPWD As String
Dim sLocalFLD As String
Dim Msg1, Msg2, Title
Msg1 = "Enter ftp site user id" ' Set prompt.
Title = "Ftp Login" ' Set title.
sUID = InputBox(Msg1, Title)
Msg2 = "Enter password" ' Set prompt.
sPWD = InputBox(Msg2, Title)
sSVR = ("ftp01")
sFLD = ("lifecomm\Ftp\Finance\INBOUND")
sLocalFLD = Me.lblPath.Caption
sFile = Nz(Me.cboUploadFile, "")
If sFile = "" Or Dir(sLocalFLD & "\" & sFile) = "" Then
MsgBox "The file cannot be found.", vbExclamation, "E R R O R"
Call cmdReadFolder_Click
Me.cboUploadFile.SetFocus
Exit Sub
End If
If sUID = "" Then
MsgBox "You must enter a user ID. Please try again.", vbExclamation, "E R R O R"
Me.cboUploadFile.SetFocus
Exit Sub
ElseIf sPWD = "" Then
MsgBox "You must enter a password. Please try again.", vbExclamation, "E R R O R"
Me.cboUploadFile.SetFocus
Exit Sub
Else
sResult = UploadFTPFile(sFile, sSVR, sFLD, sUID, sPWD, sLocalFLD)
Call cmdReadFolder_Click
End If
End Sub
Public Function UploadFTPFile(sFile As String, sSVR As String, sFLD As String, sUID As String, sPWD As String, sLocalFLD As String) As String
Dim sArchiveFLD As String
Dim sScrFile As String
Dim sSource As String
Dim sArchDest As String
Dim iFile As Integer
Dim sExe As String
Const q As String * 1 = """"
On Error GoTo Err_Handler
sArchiveFLD = sLocalFLD & "\ARCHIVE"
' will break if empty folder exist so error to pass
' must create folder first, so API calls work
On Error Resume Next
If Dir(sLocalFLD & "\") = "" Then MkDir (sLocalFLD)
On Error GoTo Err_Handler
sSource = q & sLocalFLD & "\" & sFile & q
sArchDest = q & sArchiveFLD & "\" & sFile & q
sScrFile = sLocalFLD & "\upload.scr"
If Dir(sScrFile) <> "" Then Kill sScrFile
' Open a new text file to hold the FTP script and load it with
' the appropriate commands.
iFile = FreeFile
Open sScrFile For Output As iFile
Print #iFile, "open " & sSVR
Print #iFile, sUID
Print #iFile, sPWD
Print #iFile, "cd " & sFLD
Print #iFile, "lcd " & q & sLocalFLD & q
Print #iFile, "put " & sFile
Print #iFile, "bye"
Close #iFile
sExe = Environ$("COMSPEC")
sExe = Left$(sExe, Len(sExe) - Len(Dir(sExe)))
sExe = sExe & "ftp.exe -s:" & q & sScrFile & q
ShellWait sExe, vbHide
DoEvents
Exit_Here:
DoCmd.Hourglass False
Call MoveAndRenameMyFile(sLocalFLD & "\", sFile, sArchiveFLD & "\" & sFile)
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "E R R O R"
Resume Exit_Here
End Function
Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
On Error GoTo Err_Handler
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "E R R O R"
Resume Exit_Here
End Sub
Here's the code:
Private Sub cmdUpload_Click()
On Error Resume Next
Dim sResult As String, sFile As String
Dim sSVR As String, sFLD As String
Dim sUID As String, sPWD As String
Dim sLocalFLD As String
Dim Msg1, Msg2, Title
Msg1 = "Enter ftp site user id" ' Set prompt.
Title = "Ftp Login" ' Set title.
sUID = InputBox(Msg1, Title)
Msg2 = "Enter password" ' Set prompt.
sPWD = InputBox(Msg2, Title)
sSVR = ("ftp01")
sFLD = ("lifecomm\Ftp\Finance\INBOUND")
sLocalFLD = Me.lblPath.Caption
sFile = Nz(Me.cboUploadFile, "")
If sFile = "" Or Dir(sLocalFLD & "\" & sFile) = "" Then
MsgBox "The file cannot be found.", vbExclamation, "E R R O R"
Call cmdReadFolder_Click
Me.cboUploadFile.SetFocus
Exit Sub
End If
If sUID = "" Then
MsgBox "You must enter a user ID. Please try again.", vbExclamation, "E R R O R"
Me.cboUploadFile.SetFocus
Exit Sub
ElseIf sPWD = "" Then
MsgBox "You must enter a password. Please try again.", vbExclamation, "E R R O R"
Me.cboUploadFile.SetFocus
Exit Sub
Else
sResult = UploadFTPFile(sFile, sSVR, sFLD, sUID, sPWD, sLocalFLD)
Call cmdReadFolder_Click
End If
End Sub
Public Function UploadFTPFile(sFile As String, sSVR As String, sFLD As String, sUID As String, sPWD As String, sLocalFLD As String) As String
Dim sArchiveFLD As String
Dim sScrFile As String
Dim sSource As String
Dim sArchDest As String
Dim iFile As Integer
Dim sExe As String
Const q As String * 1 = """"
On Error GoTo Err_Handler
sArchiveFLD = sLocalFLD & "\ARCHIVE"
' will break if empty folder exist so error to pass
' must create folder first, so API calls work
On Error Resume Next
If Dir(sLocalFLD & "\") = "" Then MkDir (sLocalFLD)
On Error GoTo Err_Handler
sSource = q & sLocalFLD & "\" & sFile & q
sArchDest = q & sArchiveFLD & "\" & sFile & q
sScrFile = sLocalFLD & "\upload.scr"
If Dir(sScrFile) <> "" Then Kill sScrFile
' Open a new text file to hold the FTP script and load it with
' the appropriate commands.
iFile = FreeFile
Open sScrFile For Output As iFile
Print #iFile, "open " & sSVR
Print #iFile, sUID
Print #iFile, sPWD
Print #iFile, "cd " & sFLD
Print #iFile, "lcd " & q & sLocalFLD & q
Print #iFile, "put " & sFile
Print #iFile, "bye"
Close #iFile
sExe = Environ$("COMSPEC")
sExe = Left$(sExe, Len(sExe) - Len(Dir(sExe)))
sExe = sExe & "ftp.exe -s:" & q & sScrFile & q
ShellWait sExe, vbHide
DoEvents
Exit_Here:
DoCmd.Hourglass False
Call MoveAndRenameMyFile(sLocalFLD & "\", sFile, sArchiveFLD & "\" & sFile)
Exit Function
Err_Handler:
MsgBox Err.Description, vbExclamation, "E R R O R"
Resume Exit_Here
End Function
Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
On Error GoTo Err_Handler
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "E R R O R"
Resume Exit_Here
End Sub