Option Compare Database
Option Explicit
Dim ftpDataExpected As Boolean
Private Sub cmdChangeDirectory_Click()
' Change directory to txtRemotePath.
Inet1.Execute txtURL.Text, "CD " ' & Me.txtRemotePath.Text
End Sub
Private Sub cmdDELETE_Click()
' Delete the directory in txtRemotePath.
Inet1.Execute txtURL.Text, "DELETE " '& Me.txtRemotePath.Text
End Sub
Private Sub cmdDIR_Click()
Inet1.Execute txtURL.Text, "DIR FindThis.txt"
End Sub
Private Sub cmdGET_Click()
Inet1.Execute txtURL.Text, _
"GET GetThis.txt C:\MyDocuments\GotThis.txt"
End Sub
Private Sub cmdSEND_Click()
Inet1.Execute txtURL.Text, "SEND C:\MyDocuments\Send.txt SentDocs\Sent.txt"
End Sub
Private Sub CopyBtn_Click()
Dim i As Integer
Dim MyTargetDirectory As String
Dim MySource As String, MyTarget As String
Dim MyCommandLine As String
MyTargetDirectory = "C:\Database\USPSftp\Downloads\"
' MsgBox Me.ListFtpFile.ListCount
' MsgBox Me.ListFtpFile.ListIndex
If Me.ListFtpFile.ListCount = 0 Then
MsgBox "Nothing to process"
Exit Sub
End If
For i = 0 To 3 'Me.ListFtpFile.ListCount - 1
' MsgBox Me.ListFtpFile.ItemData(i)
MyCommandLine = "GET " & Me.ListFtpFile.ItemData(i) & " " & MyTargetDirectory & Me.ListFtpFile.ItemData(i)
'Copy File from ftp to local drive
Me.Inet1.Execute , MyCommandLine
Next i
End Sub
Private Sub DirBtn_Click()
On Error GoTo Err_DirBtn_Click
Me.TextData = ""
ftpDataExpected = True
With Inet1
' .URL = "ftp://107.107.107.107"
' .UserName = "test"
' .Password = "testpass"
.Execute , "DIR" ' Returns Directory
' .Execute "CLOSE"
End With
Exit_DirBtn_Click:
Exit Sub
Err_DirBtn_Click:
MsgBox Err.Description
Resume Exit_DirBtn_Click
End Sub
Private Sub Form_Load()
ftpDataExpected = False
Me.errText = "Begin at " & Now
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
' Retrieve server response using the GetChunk
' method when State = 12.
Dim vtData As Variant ' Data variable.
Dim vtDataMore As Variant ' Data variable.
Dim bDone As Boolean: bDone = False
Me.errText = Me.errText & Chr$(13) & Chr$(10) & "ftp state: " & Str$(State) & " at " & Now
If Inet1.ResponseCode <> 0 Then
Me.errText = Me.errText & Chr$(13) & Chr$(10) & " response code =" & Inet1.ResponseCode & ":" & Inet1.ResponseInfo
End If
Select Case State
Case icError ' 11 These are expected errors like file exists 80
' In case of error, return ResponseCode and
' ResponseInfo.
vtData = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
Me.TextData = vtData
Case icResponseCompleted ' 12
vtData = Inet1.GetChunk(1024)
Do While Not bDone
' Get next chunk.
vtDataMore = ""
vtDataMore = Inet1.GetChunk(1024)
If Len(vtDataMore) = 0 Then
bDone = True
Else
'Save and go back for more
vtData = vtData & vtDataMore
End If
Loop
Me.TextData = vtData
If ftpDataExpected = True Then
ParseDataToList vtData
ftpDataExpected = False
End If
Case Else
Beep
' ... Other cases not needed.
End Select
End Sub
Private Function ParseDataToList(MyDataIn As Variant) As Integer
Dim iStart As Long, i As Long
Dim MySearch As String
Dim MyParsedData As String
Dim MyWorkData As String
MyParsedData = ""
MySearch = Chr$(13) & Chr$(10)
MyWorkData = Trim$(MyDataIn & " ")
i = InStr(MyWorkData, MySearch)
Do While i > 0
If Len(MyParsedData) > 0 Then
MyParsedData = MyParsedData & ";" & Mid$(MyWorkData, 1, i - 1)
Else
MyParsedData = Mid$(MyWorkData, 1, i - 1)
End If
MyWorkData = Mid$(MyWorkData, i + 2)
i = InStr(MyWorkData, MySearch)
Loop
If Len(MyParsedData) > 0 Then
Me.ListFtpFile.RowSource = MyParsedData
Else
Me.ListFtpFile.RowSource = ""
End If
End Function
Private Sub Inet1_StateChanged_Works(ByVal State As Integer)
' Retrieve server response using the GetChunk
' method when State = 12.
Dim vtData As Variant ' Data variable.
Select Case State
' ... Other cases not shown.
Case icError ' 11
' In case of error, return ResponseCode and
' ResponseInfo.
vtData = Inet1.ResponseCode & ":" & _
Inet1.ResponseInfo
Case icResponseCompleted ' 12
vtData = Inet1.GetChunk(1024)
End Select
Me.TextData = vtData
End Sub
Private Sub CloseFtpBtn_Click()
On Error GoTo Err_CloseFtpBtn_Click
With Inet1
.URL = ""
.UserName = ""
.Password = ""
.Execute "CLOSE"
End With
Exit_CloseFtpBtn_Click:
Exit Sub
Err_CloseFtpBtn_Click:
MsgBox Err.Description
Resume Exit_CloseFtpBtn_Click
End Sub
Private Sub openFtpBtn_Click()
On Error GoTo Err_openFtpBtn_Click
ftpDataExpected = True
Me.TextData = ""
With Inet1
.URL = "ftp://107.107.107.107"
.UserName = "test"
.Password = "testpass"
.Execute , "DIR" ' Returns Directory
' .Execute "CLOSE"
End With
Exit_openFtpBtn_Click:
Exit Sub
Err_openFtpBtn_Click:
MsgBox Err.Description
Resume Exit_openFtpBtn_Click
End Sub