473,747 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capturing FTP responses in VBA?

Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.
Dec 11 '05 #1
17 4333
I imagine someone has an FTP client that works as a COM object to do what you
are suggesting.

On Sat, 10 Dec 2005 18:15:07 -0600, Lauren Wilson <no****@none.co m> wrote:
Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.


Dec 11 '05 #2
On Sat, 10 Dec 2005 17:15:35 -0800, Steve Jorgensen
<no****@nospam. nospam> wrote:
I imagine someone has an FTP client that works as a COM object to do what you
are suggesting.
That would be cool. Does anyone know where I can find such an object?


On Sat, 10 Dec 2005 18:15:07 -0600, Lauren Wilson <no****@none.co m> wrote:
Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.

Dec 11 '05 #3
On Sat, 10 Dec 2005 18:15:07 -0600, Lauren Wilson <no****@none.co m> wrote:
Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.


I have used this with good effect over the years.
Post the following code into a new CLASS module.

'
''''''''''''''' ''''''''''''''' '
' FTPClient '
''''''''''''''' ''''''''''''''' '
' Author Stuart McCall '
' 10*********@com puserve.com '
' sm*****@smsb.de mon.co.uk '
' http://www.smsb.demon.co.uk '
''''''''''''''' ''''''''''''''' '
' July 1998 '
''''''''''''''' ''''''''''''''' '
'
''''''''''''''' ''''''''''''''' '
'Member Variables
''''''''''''''' ''''''''''''''' '
Private m_ProxyName As String
Private m_RemoteDir As String
Private m_RemoteFile As String
Private m_NewFileName As String
Private m_LocalFile As String
Private m_ServerName As String
Private m_UserName As String
Private m_Password As String
Private m_TransferType As Long
Private m_FileSpec As String
'
''''''''''''''' ''''''''''''''' '
'Collections
''''''''''''''' ''''''''''''''' '
Public FileNames As New Collection
'
''''''''''''''' ''''''''''''''' '
'Private Variables
''''''''''''''' ''''''''''''''' '
Private m_hFTP As Long 'Handle to the FTP session
Private m_hCon As Long 'Handle to the server connection
'
''''''''''''''' ''''''''''''''' '
'Private Constants
''''''''''''''' ''''''''''''''' '
Private Const mc_AGENTNAME = "FTP Class"
'
''''''''''''''' ''''''''''''''' '
'Error values (See the RaiseError routine)
''''''''''''''' ''''''''''''''' '
Private Const errOpenFTP As String = "1;Call to InternetOpen failed."
Private Const errOpenCon As String = "2;Call to InternetConnect failed."
Private Const errGetFile As String = "3;Call to FtpGetFile failed."
Private Const errPutFile As String = "4;Call to FtpPutFile failed."
Private Const errDelFile As String = "5;Call to FtpDeleteFile failed."
Private Const errRenFile As String = "6;Call to FtpRenameFile failed."
Private Const errGetDir As String = "7;Call to FtpGetCurrentDi rectory failed."
Private Const errSetDir As String = "8;Call to FtpSetCurrentDi rectory failed."
Private Const errCreateDir As String = "9;Call to FtpCreateDirect ory failed."
Private Const errFindFirst As String = "10;Call to FtpFindFirstFil e failed."
Private Const errFindNext As String = "11;Call to InternetFindNex tFile failed."
Private Const errDelDir As String = "12;Call to FtpRemoveDirect ory failed."
Private Const errNotOpen As String = "13;FTP session not open. Call OpenFTP first."
Private Const errNotConnected As String = "14;Not connected to a server. Call OpenServer first."
Private Const errNoServer As String = "15;No Server Name specified."
Private Const errNoLocalFile As String = "16;No Local File specified."
Private Const errNoRemoteFile As String = "17;No Remote File specified."
'
''''''''''''''' ''''''''''''''' '
'API Declarations
''''''''''''''' ''''''''''''''' '
Private Const MAX_PATH = &H104
'
Private Const INTERNET_INVALI D_PORT_NUMBER = &H0
Private Const INTERNET_SERVIC E_FTP = &H1
Private Const INTERNET_OPEN_T YPE_DIRECT = &H1
Private Const INTERNET_OPEN_T YPE_PROXY = &H3
Private Const INTERNET_FLAG_R ELOAD = &H80000000
Private Const INTERNET_FLAG_P ASSIVE = &H8000000
'
Private Const FTP_TRANSFER_TY PE_ASCII = &H0
Private Const FTP_TRANSFER_TY PE_BINARY = &H1
'
Private Const NO_ERROR = &H0
Private Const ERROR_NO_MORE_F ILES = &H12
Private Const ERROR_INTERNET_ EXTENDED_ERROR = &H2EE3
'
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'
Private Type WIN32_FIND_DATA
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
'
Private Declare Function FtpCreateDirect ory Lib "wininet.dl l" _
Alias "FtpCreateDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dl l" _
Alias "FtpDeleteFileA " (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpFindFirstFil e Lib "wininet.dl l" Alias "FtpFindFirstFi leA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA , ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function FtpGetCurrentDi rectory Lib "wininet.dl l" Alias "FtpGetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttri butes As Long, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpRemoveDirect ory Lib "wininet.dl l" _
Alias "FtpRemoveDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dl l" Alias "FtpRenameFileA " _
(ByVal hFtpSession As Long, ByVal lpszExistFile As String, ByVal lpszNewFile As String) As Boolean
Private Declare Function FtpSetCurrentDi rectory Lib "wininet.dl l" Alias "FtpSetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" _
(ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" _
(ByVal hInternetSessio n As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetFindNex tFile Lib "wininet.dl l" Alias "InternetFindNe xtFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA ) As Long
Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l" Alias "InternetGetLas tResponseInfoA" _
(lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function InternetOpen Lib "wininet.dl l" Alias "InternetOp enA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

''''''''''''''' ''''''''''''''' '
'Properties
''''''''''''''' ''''''''''''''' '
Public Property Get ProxyName() As String
ProxyName = m_ProxyName
End Property
Public Property Let ProxyName(NewDa ta As String)
m_ProxyName = NewData
End Property

Public Property Get RemoteDir() As String
RemoteDir = m_RemoteDir
End Property
Public Property Let RemoteDir(NewDa ta As String)
m_RemoteDir = NewData
End Property

Public Property Get RemoteFile() As String
RemoteFile = m_RemoteFile
End Property
Public Property Let RemoteFile(NewD ata As String)
m_RemoteFile = NewData
End Property

Public Property Get LocalFile() As String
LocalFile = m_LocalFile
End Property
Public Property Let LocalFile(NewDa ta As String)
m_LocalFile = NewData
End Property

Public Property Let NewFileName(New Data As String)
m_NewFileName = NewData
End Property

Public Property Get ServerName() As String
ServerName = m_ServerName
End Property
Public Property Let ServerName(NewD ata As String)
m_ServerName = NewData
End Property

Public Property Get UserName() As String
UserName = m_UserName
End Property
Public Property Let UserName(NewDat a As String)
m_UserName = NewData
End Property

Public Property Get Password() As String
Password = m_Password
End Property
Public Property Let Password(NewDat a As String)
m_Password = NewData
End Property

Public Property Get TransferType() As String
TransferType = IIf(m_TransferT ype = FTP_TRANSFER_TY PE_BINARY, "BINARY", "ASCII")
End Property
Public Property Let TransferType(Ne wData As String)
m_TransferType = IIf(UCase(Left( NewData, 3)) = "BIN", FTP_TRANSFER_TY PE_BINARY, FTP_TRANSFER_TY PE_ASCII)
End Property

Public Property Get FileSpec() As String
FileSpec = m_FileSpec
End Property
Public Property Let FileSpec(NewDat a As String)
m_FileSpec = NewData
End Property

''''''''''''''' ''''''''''''''' '
'Methods
''''''''''''''' ''''''''''''''' '
Public Sub OpenFTP(Optiona l pProxyName)
'Initiate FTP session

'Handle optional parameters
If Not IsMissing(pProx yName) Then m_ProxyName = pProxyName
'
If Len(m_ProxyName ) Then
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_PROXY, _
m_ProxyName, vbNullString, 0)
Else
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_DIRECT, _
vbNullString, vbNullString, 0)
End If
If m_hFTP = 0 Then RaiseError errOpenFTP

End Sub

Public Sub CloseFTP()
'Terminate FTP session

If m_hCon Then Me.CloseServer
If m_hFTP Then InternetCloseHa ndle m_hFTP
m_hCon = 0
m_hFTP = 0

End Sub

Public Sub OpenServer(Opti onal pServerName, Optional pUserName, Optional pPassword)
'Establish connection to server

'If FTP session not initiated
If m_hFTP = 0 Then RaiseError errNotOpen
'
'Handle optional parameters
If Not IsMissing(pServ erName) Then m_ServerName = pServerName
If Not IsMissing(pUser Name) Then m_UserName = pUserName
If Not IsMissing(pPass word) Then m_Password = pPassword
'
'Handle empty properties
If Len(m_ServerNam e) = 0 Then RaiseError errNoServer
'
'The following are translated to:
' UserName: Anonymous
' Password: default email address
'by the API, if nulls passed
If Len(m_UserName) = 0 Then m_UserName = vbNullString
If Len(m_Password) = 0 Then m_Password = vbNullString
'
m_hCon = InternetConnect (m_hFTP, m_ServerName, INTERNET_INVALI D_PORT_NUMBER, _
m_UserName, m_Password, INTERNET_SERVIC E_FTP, _
INTERNET_FLAG_P ASSIVE, 0)
'If m_hCon = 0 Then RaiseError errOpenCon
If m_hCon = 0 Then
booIsConnected = False
Exit Sub
Else
booIsConnected = True
End If
End Sub

Public Sub CloseServer()
'Terminate connection to server

If m_hCon Then InternetCloseHa ndle m_hCon
m_hCon = 0

End Sub

Public Sub GetFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Retrieve a file from server
'pTransferType accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpGetFile(m_hC on, m_RemoteFile, m_LocalFile, False, _
INTERNET_FLAG_R ELOAD, m_TransferType, 0) = False Then
'RaiseError errGetFile
booDownloadSucc eeded = False
Else
booDownloadSucc eeded = True
End If

End Sub

Public Sub PutFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Transmit a file to server
'pTransferType accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpPutFile(m_hC on, m_LocalFile, m_RemoteFile, m_TransferType, 0) = False Then
'RaiseError errPutFile
booUploadSuccee ded = False
Else
booUploadSuccee ded = True
End If

End Sub

Public Sub DelFile(Optiona l pRemoteDir, Optional pRemoteFile)
'Delete a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpDeleteFile(m _hCon, m_RemoteFile) = False Then
'RaiseError errDelFile
booDownloadSucc eeded = False
End If

End Sub

Public Sub RenFile(Optiona l pOldName, Optional pNewName)
'Rename a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pOldN ame) Then m_RemoteFile = pOldName
If Not IsMissing(pNewN ame) Then m_NewFileName = pNewName
'
'Handle empty properties
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_NewFileNa me) = 0 Then m_NewFileName = m_RemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpRenameFile(m _hCon, m_RemoteFile, m_NewFileName) = False Then
RaiseError errRenFile
End If

End Sub

Public Function GetDir() As String
'Determine current directory on server

Dim Buffer As String
Dim BufLen As Long
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
BufLen = MAX_PATH
Buffer = String(BufLen, 0)
If FtpGetCurrentDi rectory(m_hCon, Buffer, BufLen) = False Then
RaiseError errGetDir
End If
GetDir = Left(Buffer, BufLen)

End Function

Public Sub SetDir(Optional pRemoteDir)
'Change current directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpSetCurrentDi rectory(m_hCon, m_RemoteDir) = False Then
RaiseError errSetDir
End If

End Sub

Public Sub CreateDir(Optio nal pRemoteDir)
'Create directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpCreateDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errCreateDir
End If

End Sub

Public Sub DelDir(Optional pRemoteDir)
'Delete directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpRemoveDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errDelDir
End If

End Sub

Public Sub GetFileNames(Op tional pRemoteDir, Optional pFileSpec)
'Fill the FileNames collection with list
'of files matching pFileSpec from server's
'current directory

Dim hFind As Long
Dim LastErr As Long
Dim fData As WIN32_FIND_DATA
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pFile Spec) Then m_FileSpec = pFileSpec
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_FileSpec) = 0 Then m_FileSpec = "*.*"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
'Find first file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
'Obtain search handle if successful
hFind = FtpFindFirstFil e(m_hCon, m_FileSpec, fData, 0, 0)
LastErr = Err.LastDllErro r
If hFind = 0 Then
'Bail out if reported error isn't end-of-file-list
If LastErr <> ERROR_NO_MORE_F ILES Then
RaiseError errFindFirst
End If
'Must be no more files
Exit Sub
End If
'
'Reset variable for next call
LastErr = NO_ERROR
'
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
Do
'Find next file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
If InternetFindNex tFile(hFind, fData) = False Then
LastErr = Err.LastDllErro r
If LastErr = ERROR_NO_MORE_F ILES Then
'Bail out if no more files
Exit Do
Else
'Must be a 'real' error
InternetCloseHa ndle hFind
RaiseError errFindNext
End If
Else
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
End If
Loop
'
'Release the search handle
InternetCloseHa ndle hFind

End Sub

Public Sub ClearFileNames( )
'Clear contents of FileNames collection

Dim itm As Long
'
With FileNames
For itm = 1 To .Count
.Remove 1
Next
End With

End Sub

Private Sub Class_Initializ e()
'Set property defaults

m_RemoteDir = "."
m_RemoteFile = vbNullString
m_LocalFile = vbNullString
m_NewFileName = vbNullString
m_UserName = vbNullString
m_Password = vbNullString
m_ProxyName = vbNullString
m_ServerName = vbNullString
m_TransferType = FTP_TRANSFER_TY PE_BINARY

End Sub

Private Sub Class_Terminate ()
Me.ClearFileNam es
End Sub

''''''''''''''' ''''''''''''''' '
'Utility Routines
''''''''''''''' ''''''''''''''' '
Private Sub RaiseError(ByVa l ErrValue As String)
'Extracts the value to be added to the vbObjectError
'constant from the 1st section of ErrValue, and
'the error description from the 2nd section
'(Sections delimited with ';')
'Appends the last internet response string

Dim ptr As Integer
Dim InetErr As Long
'
'If we have a session handle, destroy the session
If m_hCon <> 0 Or m_hFTP <> 0 Then Me.CloseFTP
'
ptr = InStr(1, ErrValue, ";")
InetErr = Err.LastDllErro r
'Err.Raise vbObjectError + Val(Left$(ErrVa lue, ptr - 1)), _
"FTP Class", _
Mid$(ErrValue, ptr + 1) & ". (OS error code = " & InetErr & ")" & _
vbCrLf & "Internet Response: " & LastResponse(In etErr)

End Sub

Private Function LastResponse(By Val ErrNum As Long) As String
'Obtains the last response string issued by server

Dim Buffer As String
Dim BufLen As Long
'
If ErrNum = ERROR_INTERNET_ EXTENDED_ERROR Then
ErrNum = 0
InternetGetLast ResponseInfo ErrNum, vbNullString, BufLen
Buffer = String(BufLen + 1, 0)
InternetGetLast ResponseInfo ErrNum, Buffer, BufLen
LastResponse = Left(Buffer, BufLen)
End If

End Function
Wayne Gillespie
Gosford NSW Australia
Dec 11 '05 #4

WOW! Wayne, you are a prince! Thanks very much. You just helped me
make a crash and burn deadline.

Your place in heaven is now secure. :-)

On Sun, 11 Dec 2005 06:44:08 GMT, Wayne Gillespie
<be*****@NOhotm ailSPAM.com.au> wrote:
On Sat, 10 Dec 2005 18:15:07 -0600, Lauren Wilson <no****@none.co m> wrote:
Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.


I have used this with good effect over the years.
Post the following code into a new CLASS module.

'
'''''''''''''' ''''''''''''''' ''
' FTPClient '
'''''''''''''' ''''''''''''''' ''
' Author Stuart McCall '
' 10*********@com puserve.com '
' sm*****@smsb.de mon.co.uk '
' http://www.smsb.demon.co.uk '
'''''''''''''' ''''''''''''''' ''
' July 1998 '
'''''''''''''' ''''''''''''''' ''
'
'''''''''''''' ''''''''''''''' ''
'Member Variables
'''''''''''''' ''''''''''''''' ''
Private m_ProxyName As String
Private m_RemoteDir As String
Private m_RemoteFile As String
Private m_NewFileName As String
Private m_LocalFile As String
Private m_ServerName As String
Private m_UserName As String
Private m_Password As String
Private m_TransferType As Long
Private m_FileSpec As String
'
'''''''''''''' ''''''''''''''' ''
'Collections
'''''''''''''' ''''''''''''''' ''
Public FileNames As New Collection
'
'''''''''''''' ''''''''''''''' ''
'Private Variables
'''''''''''''' ''''''''''''''' ''
Private m_hFTP As Long 'Handle to the FTP session
Private m_hCon As Long 'Handle to the server connection
'
'''''''''''''' ''''''''''''''' ''
'Private Constants
'''''''''''''' ''''''''''''''' ''
Private Const mc_AGENTNAME = "FTP Class"
'
'''''''''''''' ''''''''''''''' ''
'Error values (See the RaiseError routine)
'''''''''''''' ''''''''''''''' ''
Private Const errOpenFTP As String = "1;Call to InternetOpen failed."
Private Const errOpenCon As String = "2;Call to InternetConnect failed."
Private Const errGetFile As String = "3;Call to FtpGetFile failed."
Private Const errPutFile As String = "4;Call to FtpPutFile failed."
Private Const errDelFile As String = "5;Call to FtpDeleteFile failed."
Private Const errRenFile As String = "6;Call to FtpRenameFile failed."
Private Const errGetDir As String = "7;Call to FtpGetCurrentDi rectory failed."
Private Const errSetDir As String = "8;Call to FtpSetCurrentDi rectory failed."
Private Const errCreateDir As String = "9;Call to FtpCreateDirect ory failed."
Private Const errFindFirst As String = "10;Call to FtpFindFirstFil e failed."
Private Const errFindNext As String = "11;Call to InternetFindNex tFile failed."
Private Const errDelDir As String = "12;Call to FtpRemoveDirect ory failed."
Private Const errNotOpen As String = "13;FTP session not open. Call OpenFTP first."
Private Const errNotConnected As String = "14;Not connected to a server. Call OpenServer first."
Private Const errNoServer As String = "15;No Server Name specified."
Private Const errNoLocalFile As String = "16;No Local File specified."
Private Const errNoRemoteFile As String = "17;No Remote File specified."
'
'''''''''''''' ''''''''''''''' ''
'API Declarations
'''''''''''''' ''''''''''''''' ''
Private Const MAX_PATH = &H104
'
Private Const INTERNET_INVALI D_PORT_NUMBER = &H0
Private Const INTERNET_SERVIC E_FTP = &H1
Private Const INTERNET_OPEN_T YPE_DIRECT = &H1
Private Const INTERNET_OPEN_T YPE_PROXY = &H3
Private Const INTERNET_FLAG_R ELOAD = &H80000000
Private Const INTERNET_FLAG_P ASSIVE = &H8000000
'
Private Const FTP_TRANSFER_TY PE_ASCII = &H0
Private Const FTP_TRANSFER_TY PE_BINARY = &H1
'
Private Const NO_ERROR = &H0
Private Const ERROR_NO_MORE_F ILES = &H12
Private Const ERROR_INTERNET_ EXTENDED_ERROR = &H2EE3
'
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'
Private Type WIN32_FIND_DATA
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
'
Private Declare Function FtpCreateDirect ory Lib "wininet.dl l" _
Alias "FtpCreateDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dl l" _
Alias "FtpDeleteFileA " (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpFindFirstFil e Lib "wininet.dl l" Alias "FtpFindFirstFi leA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA , ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function FtpGetCurrentDi rectory Lib "wininet.dl l" Alias "FtpGetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttri butes As Long, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpRemoveDirect ory Lib "wininet.dl l" _
Alias "FtpRemoveDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dl l" Alias "FtpRenameFileA " _
(ByVal hFtpSession As Long, ByVal lpszExistFile As String, ByVal lpszNewFile As String) As Boolean
Private Declare Function FtpSetCurrentDi rectory Lib "wininet.dl l" Alias "FtpSetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" _
(ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" _
(ByVal hInternetSessio n As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetFindNex tFile Lib "wininet.dl l" Alias "InternetFindNe xtFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA ) As Long
Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l" Alias "InternetGetLas tResponseInfoA" _
(lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function InternetOpen Lib "wininet.dl l" Alias "InternetOp enA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

'''''''''''''' ''''''''''''''' ''
'Properties
'''''''''''''' ''''''''''''''' ''
Public Property Get ProxyName() As String
ProxyName = m_ProxyName
End Property
Public Property Let ProxyName(NewDa ta As String)
m_ProxyName = NewData
End Property

Public Property Get RemoteDir() As String
RemoteDir = m_RemoteDir
End Property
Public Property Let RemoteDir(NewDa ta As String)
m_RemoteDir = NewData
End Property

Public Property Get RemoteFile() As String
RemoteFile = m_RemoteFile
End Property
Public Property Let RemoteFile(NewD ata As String)
m_RemoteFile = NewData
End Property

Public Property Get LocalFile() As String
LocalFile = m_LocalFile
End Property
Public Property Let LocalFile(NewDa ta As String)
m_LocalFile = NewData
End Property

Public Property Let NewFileName(New Data As String)
m_NewFileName = NewData
End Property

Public Property Get ServerName() As String
ServerName = m_ServerName
End Property
Public Property Let ServerName(NewD ata As String)
m_ServerName = NewData
End Property

Public Property Get UserName() As String
UserName = m_UserName
End Property
Public Property Let UserName(NewDat a As String)
m_UserName = NewData
End Property

Public Property Get Password() As String
Password = m_Password
End Property
Public Property Let Password(NewDat a As String)
m_Password = NewData
End Property

Public Property Get TransferType() As String
TransferType = IIf(m_TransferT ype = FTP_TRANSFER_TY PE_BINARY, "BINARY", "ASCII")
End Property
Public Property Let TransferType(Ne wData As String)
m_TransferType = IIf(UCase(Left( NewData, 3)) = "BIN", FTP_TRANSFER_TY PE_BINARY, FTP_TRANSFER_TY PE_ASCII)
End Property

Public Property Get FileSpec() As String
FileSpec = m_FileSpec
End Property
Public Property Let FileSpec(NewDat a As String)
m_FileSpec = NewData
End Property

'''''''''''''' ''''''''''''''' ''
'Methods
'''''''''''''' ''''''''''''''' ''
Public Sub OpenFTP(Optiona l pProxyName)
'Initiate FTP session

'Handle optional parameters
If Not IsMissing(pProx yName) Then m_ProxyName = pProxyName
'
If Len(m_ProxyName ) Then
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_PROXY, _
m_ProxyName, vbNullString, 0)
Else
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_DIRECT, _
vbNullString, vbNullString, 0)
End If
If m_hFTP = 0 Then RaiseError errOpenFTP

End Sub

Public Sub CloseFTP()
'Terminate FTP session

If m_hCon Then Me.CloseServer
If m_hFTP Then InternetCloseHa ndle m_hFTP
m_hCon = 0
m_hFTP = 0

End Sub

Public Sub OpenServer(Opti onal pServerName, Optional pUserName, Optional pPassword)
'Establish connection to server

'If FTP session not initiated
If m_hFTP = 0 Then RaiseError errNotOpen
'
'Handle optional parameters
If Not IsMissing(pServ erName) Then m_ServerName = pServerName
If Not IsMissing(pUser Name) Then m_UserName = pUserName
If Not IsMissing(pPass word) Then m_Password = pPassword
'
'Handle empty properties
If Len(m_ServerNam e) = 0 Then RaiseError errNoServer
'
'The following are translated to:
' UserName: Anonymous
' Password: default email address
'by the API, if nulls passed
If Len(m_UserName) = 0 Then m_UserName = vbNullString
If Len(m_Password) = 0 Then m_Password = vbNullString
'
m_hCon = InternetConnect (m_hFTP, m_ServerName, INTERNET_INVALI D_PORT_NUMBER, _
m_UserName, m_Password, INTERNET_SERVIC E_FTP, _
INTERNET_FLAG_P ASSIVE, 0)
'If m_hCon = 0 Then RaiseError errOpenCon
If m_hCon = 0 Then
booIsConnected = False
Exit Sub
Else
booIsConnected = True
End If
End Sub

Public Sub CloseServer()
'Terminate connection to server

If m_hCon Then InternetCloseHa ndle m_hCon
m_hCon = 0

End Sub

Public Sub GetFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Retrieve a file from server
'pTransferTy pe accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpGetFile(m_hC on, m_RemoteFile, m_LocalFile, False, _
INTERNET_FLAG_R ELOAD, m_TransferType, 0) = False Then
'RaiseError errGetFile
booDownloadSucc eeded = False
Else
booDownloadSucc eeded = True
End If

End Sub

Public Sub PutFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Transmit a file to server
'pTransferTy pe accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpPutFile(m_hC on, m_LocalFile, m_RemoteFile, m_TransferType, 0) = False Then
'RaiseError errPutFile
booUploadSuccee ded = False
Else
booUploadSuccee ded = True
End If

End Sub

Public Sub DelFile(Optiona l pRemoteDir, Optional pRemoteFile)
'Delete a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpDeleteFile(m _hCon, m_RemoteFile) = False Then
'RaiseError errDelFile
booDownloadSucc eeded = False
End If

End Sub

Public Sub RenFile(Optiona l pOldName, Optional pNewName)
'Rename a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pOldN ame) Then m_RemoteFile = pOldName
If Not IsMissing(pNewN ame) Then m_NewFileName = pNewName
'
'Handle empty properties
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_NewFileNa me) = 0 Then m_NewFileName = m_RemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpRenameFile(m _hCon, m_RemoteFile, m_NewFileName) = False Then
RaiseError errRenFile
End If

End Sub

Public Function GetDir() As String
'Determine current directory on server

Dim Buffer As String
Dim BufLen As Long
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
BufLen = MAX_PATH
Buffer = String(BufLen, 0)
If FtpGetCurrentDi rectory(m_hCon, Buffer, BufLen) = False Then
RaiseError errGetDir
End If
GetDir = Left(Buffer, BufLen)

End Function

Public Sub SetDir(Optional pRemoteDir)
'Change current directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpSetCurrentDi rectory(m_hCon, m_RemoteDir) = False Then
RaiseError errSetDir
End If

End Sub

Public Sub CreateDir(Optio nal pRemoteDir)
'Create directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpCreateDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errCreateDir
End If

End Sub

Public Sub DelDir(Optional pRemoteDir)
'Delete directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpRemoveDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errDelDir
End If

End Sub

Public Sub GetFileNames(Op tional pRemoteDir, Optional pFileSpec)
'Fill the FileNames collection with list
'of files matching pFileSpec from server's
'current directory

Dim hFind As Long
Dim LastErr As Long
Dim fData As WIN32_FIND_DATA
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pFile Spec) Then m_FileSpec = pFileSpec
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_FileSpec) = 0 Then m_FileSpec = "*.*"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
'Find first file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
'Obtain search handle if successful
hFind = FtpFindFirstFil e(m_hCon, m_FileSpec, fData, 0, 0)
LastErr = Err.LastDllErro r
If hFind = 0 Then
'Bail out if reported error isn't end-of-file-list
If LastErr <> ERROR_NO_MORE_F ILES Then
RaiseError errFindFirst
End If
'Must be no more files
Exit Sub
End If
'
'Reset variable for next call
LastErr = NO_ERROR
'
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
Do
'Find next file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
If InternetFindNex tFile(hFind, fData) = False Then
LastErr = Err.LastDllErro r
If LastErr = ERROR_NO_MORE_F ILES Then
'Bail out if no more files
Exit Do
Else
'Must be a 'real' error
InternetCloseHa ndle hFind
RaiseError errFindNext
End If
Else
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
End If
Loop
'
'Release the search handle
InternetCloseHa ndle hFind

End Sub

Public Sub ClearFileNames( )
'Clear contents of FileNames collection

Dim itm As Long
'
With FileNames
For itm = 1 To .Count
.Remove 1
Next
End With

End Sub

Private Sub Class_Initializ e()
'Set property defaults

m_RemoteDir = "."
m_RemoteFile = vbNullString
m_LocalFile = vbNullString
m_NewFileName = vbNullString
m_UserName = vbNullString
m_Password = vbNullString
m_ProxyName = vbNullString
m_ServerName = vbNullString
m_TransferType = FTP_TRANSFER_TY PE_BINARY

End Sub

Private Sub Class_Terminate ()
Me.ClearFileNam es
End Sub

'''''''''''''' ''''''''''''''' ''
'Utility Routines
'''''''''''''' ''''''''''''''' ''
Private Sub RaiseError(ByVa l ErrValue As String)
'Extracts the value to be added to the vbObjectError
'constant from the 1st section of ErrValue, and
'the error description from the 2nd section
'(Sections delimited with ';')
'Appends the last internet response string

Dim ptr As Integer
Dim InetErr As Long
'
'If we have a session handle, destroy the session
If m_hCon <> 0 Or m_hFTP <> 0 Then Me.CloseFTP
'
ptr = InStr(1, ErrValue, ";")
InetErr = Err.LastDllErro r
'Err.Raise vbObjectError + Val(Left$(ErrVa lue, ptr - 1)), _
"FTP Class", _
Mid$(ErrValue, ptr + 1) & ". (OS error code = " & InetErr & ")" & _
vbCrLf & "Internet Response: " & LastResponse(In etErr)

End Sub

Private Function LastResponse(By Val ErrNum As Long) As String
'Obtains the last response string issued by server

Dim Buffer As String
Dim BufLen As Long
'
If ErrNum = ERROR_INTERNET_ EXTENDED_ERROR Then
ErrNum = 0
InternetGetLast ResponseInfo ErrNum, vbNullString, BufLen
Buffer = String(BufLen + 1, 0)
InternetGetLast ResponseInfo ErrNum, Buffer, BufLen
LastResponse = Left(Buffer, BufLen)
End If

End Function
Wayne Gillespie
Gosford NSW Australia

Dec 11 '05 #5

Hi Wayne,

This code won't compile. It croaks on: booUploadSuccee ded = False
with "variable not defined". There are other examples of the same
thing in other procs. Any clues?

Thanks,
-- LW

Public Sub PutFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Transmit a file to server
'pTransferType accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType =
pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpPutFile(m_hC on, m_LocalFile, m_RemoteFile, m_TransferType,
0) = False Then
'RaiseError errPutFile
booUploadSuccee ded = False
Else
booUploadSuccee ded = True
End If

End Sub


On Sun, 11 Dec 2005 06:44:08 GMT, Wayne Gillespie
<be*****@NOhotm ailSPAM.com.au> wrote:
On Sat, 10 Dec 2005 18:15:07 -0600, Lauren Wilson <no****@none.co m> wrote:
Does anyone know if it is possible to capture FTP responses to various
FTP commands when managing an FTP session from a VBA procedure?

For example, if we try to login to an FTP server and the login info is
incorrect, FTP replies with a message to that effect. I need to
capture those kinds of responses and advise the user of an Access app
what is happening.

Thanks for all responses.


I have used this with good effect over the years.
Post the following code into a new CLASS module.

'
'''''''''''''' ''''''''''''''' ''
' FTPClient '
'''''''''''''' ''''''''''''''' ''
' Author Stuart McCall '
' 10*********@com puserve.com '
' sm*****@smsb.de mon.co.uk '
' http://www.smsb.demon.co.uk '
'''''''''''''' ''''''''''''''' ''
' July 1998 '
'''''''''''''' ''''''''''''''' ''
'
'''''''''''''' ''''''''''''''' ''
'Member Variables
'''''''''''''' ''''''''''''''' ''
Private m_ProxyName As String
Private m_RemoteDir As String
Private m_RemoteFile As String
Private m_NewFileName As String
Private m_LocalFile As String
Private m_ServerName As String
Private m_UserName As String
Private m_Password As String
Private m_TransferType As Long
Private m_FileSpec As String
'
'''''''''''''' ''''''''''''''' ''
'Collections
'''''''''''''' ''''''''''''''' ''
Public FileNames As New Collection
'
'''''''''''''' ''''''''''''''' ''
'Private Variables
'''''''''''''' ''''''''''''''' ''
Private m_hFTP As Long 'Handle to the FTP session
Private m_hCon As Long 'Handle to the server connection
'
'''''''''''''' ''''''''''''''' ''
'Private Constants
'''''''''''''' ''''''''''''''' ''
Private Const mc_AGENTNAME = "FTP Class"
'
'''''''''''''' ''''''''''''''' ''
'Error values (See the RaiseError routine)
'''''''''''''' ''''''''''''''' ''
Private Const errOpenFTP As String = "1;Call to InternetOpen failed."
Private Const errOpenCon As String = "2;Call to InternetConnect failed."
Private Const errGetFile As String = "3;Call to FtpGetFile failed."
Private Const errPutFile As String = "4;Call to FtpPutFile failed."
Private Const errDelFile As String = "5;Call to FtpDeleteFile failed."
Private Const errRenFile As String = "6;Call to FtpRenameFile failed."
Private Const errGetDir As String = "7;Call to FtpGetCurrentDi rectory failed."
Private Const errSetDir As String = "8;Call to FtpSetCurrentDi rectory failed."
Private Const errCreateDir As String = "9;Call to FtpCreateDirect ory failed."
Private Const errFindFirst As String = "10;Call to FtpFindFirstFil e failed."
Private Const errFindNext As String = "11;Call to InternetFindNex tFile failed."
Private Const errDelDir As String = "12;Call to FtpRemoveDirect ory failed."
Private Const errNotOpen As String = "13;FTP session not open. Call OpenFTP first."
Private Const errNotConnected As String = "14;Not connected to a server. Call OpenServer first."
Private Const errNoServer As String = "15;No Server Name specified."
Private Const errNoLocalFile As String = "16;No Local File specified."
Private Const errNoRemoteFile As String = "17;No Remote File specified."
'
'''''''''''''' ''''''''''''''' ''
'API Declarations
'''''''''''''' ''''''''''''''' ''
Private Const MAX_PATH = &H104
'
Private Const INTERNET_INVALI D_PORT_NUMBER = &H0
Private Const INTERNET_SERVIC E_FTP = &H1
Private Const INTERNET_OPEN_T YPE_DIRECT = &H1
Private Const INTERNET_OPEN_T YPE_PROXY = &H3
Private Const INTERNET_FLAG_R ELOAD = &H80000000
Private Const INTERNET_FLAG_P ASSIVE = &H8000000
'
Private Const FTP_TRANSFER_TY PE_ASCII = &H0
Private Const FTP_TRANSFER_TY PE_BINARY = &H1
'
Private Const NO_ERROR = &H0
Private Const ERROR_NO_MORE_F ILES = &H12
Private Const ERROR_INTERNET_ EXTENDED_ERROR = &H2EE3
'
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
'
Private Type WIN32_FIND_DATA
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
'
Private Declare Function FtpCreateDirect ory Lib "wininet.dl l" _
Alias "FtpCreateDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dl l" _
Alias "FtpDeleteFileA " (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpFindFirstFil e Lib "wininet.dl l" Alias "FtpFindFirstFi leA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA , ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function FtpGetCurrentDi rectory Lib "wininet.dl l" Alias "FtpGetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dl l" Alias "FtpGetFile A" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttri butes As Long, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dl l" Alias "FtpPutFile A" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpRemoveDirect ory Lib "wininet.dl l" _
Alias "FtpRemoveDirec toryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dl l" Alias "FtpRenameFileA " _
(ByVal hFtpSession As Long, ByVal lpszExistFile As String, ByVal lpszNewFile As String) As Boolean
Private Declare Function FtpSetCurrentDi rectory Lib "wininet.dl l" Alias "FtpSetCurrentD irectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" _
(ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dl l" Alias "InternetConnec tA" _
(ByVal hInternetSessio n As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetFindNex tFile Lib "wininet.dl l" Alias "InternetFindNe xtFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA ) As Long
Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l" Alias "InternetGetLas tResponseInfoA" _
(lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLengt h As Long) As Boolean
Private Declare Function InternetOpen Lib "wininet.dl l" Alias "InternetOp enA" _
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

'''''''''''''' ''''''''''''''' ''
'Properties
'''''''''''''' ''''''''''''''' ''
Public Property Get ProxyName() As String
ProxyName = m_ProxyName
End Property
Public Property Let ProxyName(NewDa ta As String)
m_ProxyName = NewData
End Property

Public Property Get RemoteDir() As String
RemoteDir = m_RemoteDir
End Property
Public Property Let RemoteDir(NewDa ta As String)
m_RemoteDir = NewData
End Property

Public Property Get RemoteFile() As String
RemoteFile = m_RemoteFile
End Property
Public Property Let RemoteFile(NewD ata As String)
m_RemoteFile = NewData
End Property

Public Property Get LocalFile() As String
LocalFile = m_LocalFile
End Property
Public Property Let LocalFile(NewDa ta As String)
m_LocalFile = NewData
End Property

Public Property Let NewFileName(New Data As String)
m_NewFileName = NewData
End Property

Public Property Get ServerName() As String
ServerName = m_ServerName
End Property
Public Property Let ServerName(NewD ata As String)
m_ServerName = NewData
End Property

Public Property Get UserName() As String
UserName = m_UserName
End Property
Public Property Let UserName(NewDat a As String)
m_UserName = NewData
End Property

Public Property Get Password() As String
Password = m_Password
End Property
Public Property Let Password(NewDat a As String)
m_Password = NewData
End Property

Public Property Get TransferType() As String
TransferType = IIf(m_TransferT ype = FTP_TRANSFER_TY PE_BINARY, "BINARY", "ASCII")
End Property
Public Property Let TransferType(Ne wData As String)
m_TransferType = IIf(UCase(Left( NewData, 3)) = "BIN", FTP_TRANSFER_TY PE_BINARY, FTP_TRANSFER_TY PE_ASCII)
End Property

Public Property Get FileSpec() As String
FileSpec = m_FileSpec
End Property
Public Property Let FileSpec(NewDat a As String)
m_FileSpec = NewData
End Property

'''''''''''''' ''''''''''''''' ''
'Methods
'''''''''''''' ''''''''''''''' ''
Public Sub OpenFTP(Optiona l pProxyName)
'Initiate FTP session

'Handle optional parameters
If Not IsMissing(pProx yName) Then m_ProxyName = pProxyName
'
If Len(m_ProxyName ) Then
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_PROXY, _
m_ProxyName, vbNullString, 0)
Else
m_hFTP = InternetOpen(mc _AGENTNAME, INTERNET_OPEN_T YPE_DIRECT, _
vbNullString, vbNullString, 0)
End If
If m_hFTP = 0 Then RaiseError errOpenFTP

End Sub

Public Sub CloseFTP()
'Terminate FTP session

If m_hCon Then Me.CloseServer
If m_hFTP Then InternetCloseHa ndle m_hFTP
m_hCon = 0
m_hFTP = 0

End Sub

Public Sub OpenServer(Opti onal pServerName, Optional pUserName, Optional pPassword)
'Establish connection to server

'If FTP session not initiated
If m_hFTP = 0 Then RaiseError errNotOpen
'
'Handle optional parameters
If Not IsMissing(pServ erName) Then m_ServerName = pServerName
If Not IsMissing(pUser Name) Then m_UserName = pUserName
If Not IsMissing(pPass word) Then m_Password = pPassword
'
'Handle empty properties
If Len(m_ServerNam e) = 0 Then RaiseError errNoServer
'
'The following are translated to:
' UserName: Anonymous
' Password: default email address
'by the API, if nulls passed
If Len(m_UserName) = 0 Then m_UserName = vbNullString
If Len(m_Password) = 0 Then m_Password = vbNullString
'
m_hCon = InternetConnect (m_hFTP, m_ServerName, INTERNET_INVALI D_PORT_NUMBER, _
m_UserName, m_Password, INTERNET_SERVIC E_FTP, _
INTERNET_FLAG_P ASSIVE, 0)
'If m_hCon = 0 Then RaiseError errOpenCon
If m_hCon = 0 Then
booIsConnected = False
Exit Sub
Else
booIsConnected = True
End If
End Sub

Public Sub CloseServer()
'Terminate connection to server

If m_hCon Then InternetCloseHa ndle m_hCon
m_hCon = 0

End Sub

Public Sub GetFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Retrieve a file from server
'pTransferTy pe accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpGetFile(m_hC on, m_RemoteFile, m_LocalFile, False, _
INTERNET_FLAG_R ELOAD, m_TransferType, 0) = False Then
'RaiseError errGetFile
booDownloadSucc eeded = False
Else
booDownloadSucc eeded = True
End If

End Sub

Public Sub PutFile(Optiona l pRemoteDir, Optional pRemoteFile, _
Optional pLocalFile, Optional pTransferType)
'Transmit a file to server
'pTransferTy pe accepts "ASCII" or "BINARY"

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
If Not IsMissing(pLoca lFile) Then m_LocalFile = pLocalFile
If Not IsMissing(pTran sferType) Then Me.TransferType = pTransferType
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_LocalFile ) = 0 Then RaiseError errNoLocalFile
If Len(m_TransferT ype) = 0 Then Me.TransferType = "BINARY"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpPutFile(m_hC on, m_LocalFile, m_RemoteFile, m_TransferType, 0) = False Then
'RaiseError errPutFile
booUploadSuccee ded = False
Else
booUploadSuccee ded = True
End If

End Sub

Public Sub DelFile(Optiona l pRemoteDir, Optional pRemoteFile)
'Delete a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pRemo teFile) Then m_RemoteFile = pRemoteFile
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpDeleteFile(m _hCon, m_RemoteFile) = False Then
'RaiseError errDelFile
booDownloadSucc eeded = False
End If

End Sub

Public Sub RenFile(Optiona l pOldName, Optional pNewName)
'Rename a file on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pOldN ame) Then m_RemoteFile = pOldName
If Not IsMissing(pNewN ame) Then m_NewFileName = pNewName
'
'Handle empty properties
If Len(m_RemoteFil e) = 0 Then RaiseError errNoRemoteFile
If Len(m_NewFileNa me) = 0 Then m_NewFileName = m_RemoteFile
'
'Change directory on server
Me.SetDir m_RemoteDir
'
If FtpRenameFile(m _hCon, m_RemoteFile, m_NewFileName) = False Then
RaiseError errRenFile
End If

End Sub

Public Function GetDir() As String
'Determine current directory on server

Dim Buffer As String
Dim BufLen As Long
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
BufLen = MAX_PATH
Buffer = String(BufLen, 0)
If FtpGetCurrentDi rectory(m_hCon, Buffer, BufLen) = False Then
RaiseError errGetDir
End If
GetDir = Left(Buffer, BufLen)

End Function

Public Sub SetDir(Optional pRemoteDir)
'Change current directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpSetCurrentDi rectory(m_hCon, m_RemoteDir) = False Then
RaiseError errSetDir
End If

End Sub

Public Sub CreateDir(Optio nal pRemoteDir)
'Create directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpCreateDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errCreateDir
End If

End Sub

Public Sub DelDir(Optional pRemoteDir)
'Delete directory on server

'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
'
If FtpRemoveDirect ory(m_hCon, m_RemoteDir) = False Then
RaiseError errDelDir
End If

End Sub

Public Sub GetFileNames(Op tional pRemoteDir, Optional pFileSpec)
'Fill the FileNames collection with list
'of files matching pFileSpec from server's
'current directory

Dim hFind As Long
Dim LastErr As Long
Dim fData As WIN32_FIND_DATA
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
'Handle optional parameters
If Not IsMissing(pRemo teDir) Then m_RemoteDir = pRemoteDir
If Not IsMissing(pFile Spec) Then m_FileSpec = pFileSpec
'
'Handle empty properties
If Len(m_RemoteDir ) = 0 Then m_RemoteDir = "."
If Len(m_FileSpec) = 0 Then m_FileSpec = "*.*"
'
'Change directory on server
Me.SetDir m_RemoteDir
'
'Find first file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
'Obtain search handle if successful
hFind = FtpFindFirstFil e(m_hCon, m_FileSpec, fData, 0, 0)
LastErr = Err.LastDllErro r
If hFind = 0 Then
'Bail out if reported error isn't end-of-file-list
If LastErr <> ERROR_NO_MORE_F ILES Then
RaiseError errFindFirst
End If
'Must be no more files
Exit Sub
End If
'
'Reset variable for next call
LastErr = NO_ERROR
'
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
Do
'Find next file matching FileSpec
fData.cFileName = String(MAX_PATH , 0)
If InternetFindNex tFile(hFind, fData) = False Then
LastErr = Err.LastDllErro r
If LastErr = ERROR_NO_MORE_F ILES Then
'Bail out if no more files
Exit Do
Else
'Must be a 'real' error
InternetCloseHa ndle hFind
RaiseError errFindNext
End If
Else
'Add filename to the collection
FileNames.Add Left(fData.cFil eName, _
InStr(1, fData.cFileName , vbNullChar, vbBinaryCompare ) - 1)
End If
Loop
'
'Release the search handle
InternetCloseHa ndle hFind

End Sub

Public Sub ClearFileNames( )
'Clear contents of FileNames collection

Dim itm As Long
'
With FileNames
For itm = 1 To .Count
.Remove 1
Next
End With

End Sub

Private Sub Class_Initializ e()
'Set property defaults

m_RemoteDir = "."
m_RemoteFile = vbNullString
m_LocalFile = vbNullString
m_NewFileName = vbNullString
m_UserName = vbNullString
m_Password = vbNullString
m_ProxyName = vbNullString
m_ServerName = vbNullString
m_TransferType = FTP_TRANSFER_TY PE_BINARY

End Sub

Private Sub Class_Terminate ()
Me.ClearFileNam es
End Sub

'''''''''''''' ''''''''''''''' ''
'Utility Routines
'''''''''''''' ''''''''''''''' ''
Private Sub RaiseError(ByVa l ErrValue As String)
'Extracts the value to be added to the vbObjectError
'constant from the 1st section of ErrValue, and
'the error description from the 2nd section
'(Sections delimited with ';')
'Appends the last internet response string

Dim ptr As Integer
Dim InetErr As Long
'
'If we have a session handle, destroy the session
If m_hCon <> 0 Or m_hFTP <> 0 Then Me.CloseFTP
'
ptr = InStr(1, ErrValue, ";")
InetErr = Err.LastDllErro r
'Err.Raise vbObjectError + Val(Left$(ErrVa lue, ptr - 1)), _
"FTP Class", _
Mid$(ErrValue, ptr + 1) & ". (OS error code = " & InetErr & ")" & _
vbCrLf & "Internet Response: " & LastResponse(In etErr)

End Sub

Private Function LastResponse(By Val ErrNum As Long) As String
'Obtains the last response string issued by server

Dim Buffer As String
Dim BufLen As Long
'
If ErrNum = ERROR_INTERNET_ EXTENDED_ERROR Then
ErrNum = 0
InternetGetLast ResponseInfo ErrNum, vbNullString, BufLen
Buffer = String(BufLen + 1, 0)
InternetGetLast ResponseInfo ErrNum, Buffer, BufLen
LastResponse = Left(Buffer, BufLen)
End If

End Function
Wayne Gillespie
Gosford NSW Australia

Dec 11 '05 #6
Wayne Gillespie <be*****@NOhotm ailSPAM.com.au> wrote in
news:lh******** *************** *********@4ax.c om:
I have used this with good effect over the years.
Post the following code into a new CLASS module.

'
''''''''''''''' ''''''''''''''' '
' FTPClient '
''''''''''''''' ''''''''''''''' '
' Author Stuart McCall '
' 10*********@com puserve.com '
' sm*****@smsb.de mon.co.uk '
' http://www.smsb.demon.co.uk '
''''''''''''''' ''''''''''''''' '
' July 1998 '
''''''''''''''' ''''''''''''''' '


You haven't used it in the exact form in which you posted it, as it
won't compile with OPTION EXPLICIT, since it's lacking a declaration
for booIsConnected. It's not clear to me what purpose the variable
serves, except to replace the Raise Error (apparently for
compatibility in VBA, though I believe Raise Error is supported from
A2K on, no?). And it's not clear where it should be used, either as
a public class member, a public property, or as a return value for
the subroutine in which the variable is used (either converting it
to a function or passing a ByRef parameter).

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Dec 11 '05 #7
On Sun, 11 Dec 2005 14:43:50 -0600, Lauren Wilson <no****@none.co m> wrote:
Hi Wayne,

This code won't compile. It croaks on: booUploadSuccee ded = False
with "variable not defined". There are other examples of the same
thing in other procs. Any clues?

Thanks,
-- LW


These are public variables I have probably added to pass the result of an action to other parts of the app.
Either rem out the offending lines if you don't need this functionality, or declare the variables in a separate standard
module.
Wayne Gillespie
Gosford NSW Australia
Dec 11 '05 #8
On Sun, 11 Dec 2005 21:18:30 GMT, Wayne Gillespie
<be*****@NOhotm ailSPAM.com.au> wrote:
On Sun, 11 Dec 2005 14:43:50 -0600, Lauren Wilson <no****@none.co m> wrote:
Hi Wayne,

This code won't compile. It croaks on: booUploadSuccee ded = False
with "variable not defined". There are other examples of the same
thing in other procs. Any clues?

Thanks,
-- LW
These are public variables I have probably added to pass the result of an action to other parts of the app.
Either rem out the offending lines if you don't need this functionality, or declare the variables in a separate standard
module.


Thanks Wayne. I wasn't sure to make them public global or private.

Wayne Gillespie
Gosford NSW Australia

Dec 11 '05 #9
On Sun, 11 Dec 2005 14:59:33 -0600, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:
Wayne Gillespie <be*****@NOhotm ailSPAM.com.au> wrote in
news:lh******* *************** **********@4ax. com:
I have used this with good effect over the years.
Post the following code into a new CLASS module.

'
''''''''''''''' ''''''''''''''' '
' FTPClient '
''''''''''''''' ''''''''''''''' '
' Author Stuart McCall '
' 10*********@com puserve.com '
' sm*****@smsb.de mon.co.uk '
' http://www.smsb.demon.co.uk '
''''''''''''''' ''''''''''''''' '
' July 1998 '
''''''''''''''' ''''''''''''''' '


You haven't used it in the exact form in which you posted it, as it
won't compile with OPTION EXPLICIT, since it's lacking a declaration
for booIsConnected. It's not clear to me what purpose the variable
serves, except to replace the Raise Error (apparently for
compatibilit y in VBA, though I believe Raise Error is supported from
A2K on, no?). And it's not clear where it should be used, either as
a public class member, a public property, or as a return value for
the subroutine in which the variable is used (either converting it
to a function or passing a ByRef parameter).


Indeed. I had the same questions. However, I don't want to detract
from Wayne's kindness.
Dec 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2359
by: Drew Kellas | last post by:
Hi all! OK I admit it I'm new at java & java scripts. I have been asked to create a registration form for a club page on the web. I have been able to create my input boxes and am now trying to find a way to get the material entered in the boxes to populate an email to myself. Ideally, I want this to be done without the registrant having to see it. Can anyone help me out here?
6
2566
by: Michael Winter | last post by:
I'll be frank: what is the point? The use, and usefulness, of a set of capturing parentheses in a regular expression is clear. However, isn't an expression such as "/(?:x)/", exactly the same as "/x/", or is the former a better way of including a literal substring within an expression? I know there must be a valid reason for including them, but I'm not seeing it at the moment. Mike
4
12010
by: Jay Xx | last post by:
I have an IFrame in design mode. I've tried a bunch of things to capture key presses in that IFrame, but I can't seem to get it. I can capture key presses outside the IFrame fine. I have this problem in Firefox, not IE. I do know it's possible because Blogger.com's rich text editor does it, but their code is cryptic and separated into like 20 files, so I'm going through hell trying to decipher it. (I need to capture the keypress in order...
33
5636
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if p.pattern.groups > 100: raise AssertionError( "sorry, but this version only supports 100 named groups"
14
2546
by: Brent Burkart | last post by:
I am trying to capture the Windows Authenticated username, but I want to be able to capture the login name that exists in IIS, not Windows. In order to enter my company's intranet through the internet, they have to login. I want to be able to capture that login versus their Windows login because I need to know who they are from any computer rather than only their computer. Any ideas? Thanks
2
2696
by: Stampede | last post by:
Hi guys & girls, I would like to write some kind of "Head-Tracking-Software", which uses a WebCam to react to the movement of the head of the person sitting in front of the monitor. I searched the web and found some information on writing such a software with the help of the DirectShow library of DirectX9. But as it was all in C++ and as I'm not very good in C++ I thought someone here might give me a link or some other information on how...
6
2265
by: Ed Leafe | last post by:
I've been approached by a local business that has been advised that they need to start capturing and archiving their instant messaging in order to comply with Sarbanes-Oxley. The company is largely PC, but has a significant number of Macs running OS X, too. Googling around quickly turns up IM Grabber for the PC, which would seem to be just what they need. But there is no equivalent to be found for OS X. So if anyone knows of any such...
2
2816
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07, 2007 reference #: 0415 from: operating account
0
8979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.