Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
FTP in Access
From Wiki
One thing that drove me crazy in Access for quite a while was automating FTP uploads/downloads. I inherited a lot of old apps that actually would construct an FTP script on the fly, and execute this script through the shell. Needless to say, this was not very reliable, and made it difficult for users at times (because errors tended to pop up in odd places farther down in the process).
When I stumbled upon the Wininet.dll, I did not know how much easier it would make this. While this is nowhere near as good (IMO) as some of the managed FTP libraries available in .net, I have found using this library far superior to the old way we were doing things. The original sample function I found did not provide everything I needed (It was download-only, with extras like a status bar that I did not need), but I have kept its Declaration section and ShowError sub, primarily because of that old "If it ain't broke" thing.
Anyways, you can build the module as follows (I call mine WininetFTPFunctions):
Declaration section:
- Option Explicit
- ' Set Constants
- Const FTP_TRANSFER_TYPE_ASCII = &H1
- Const FTP_TRANSFER_TYPE_BINARY = &H2
- Const INTERNET_DEFAULT_FTP_PORT = 21
- Const INTERNET_SERVICE_FTP = 1
- Const INTERNET_FLAG_PASSIVE = &H8000000
- Const PassiveConnection As Boolean = True
- ' Declare wininet.dll API Functions
- Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
- (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
- Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" _
- (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Boolean
- Public Declare Function InternetWriteFile Lib "wininet.dll" _
- (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToWite As Long, _
- dwNumberOfBytesWritten As Long) As Integer
- Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" _
- (ByVal hFtpSession As Long, ByVal sBuff As String, ByVal Access As Long, ByVal Flags As Long, ByVal Context As Long) As Long
- Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
- (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
- ByVal lpszRemoteFile As String, _
- ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
- Public Declare Function FtpDeleteFile Lib "wininet.dll" _
- Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
- ByVal lpszFileName As String) As Boolean
- Public Declare Function InternetCloseHandle Lib "wininet.dll" _
- (ByVal hInet As Long) As Long
- Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
- (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
- ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
- Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
- (ByVal hInternetSession 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
- Public Declare Function FTPGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
- (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
- ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, _
- ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
- Declare Function InternetGetLastResponseInfo Lib "wininet.dll" _
- Alias "InternetGetLastResponseInfoA" _
- (ByRef lpdwError As Long, _
- ByVal lpszErrorBuffer As String, _
- ByRef lpdwErrorBufferLength As Long) As Boolean
Download Function:
- Function FTPDownFile(ByVal HostName As String, _
- ByVal UserName As String, _
- ByVal Password As String, _
- ByVal LocalFileName As String, _
- ByVal RemoteFileName As String, _
- ByVal sDir As String, _
- ByVal sMode As String) As Boolean
- ' Declare variables
- Dim hConnection, hOpen ' Used For Handles
- Dim fso As Object
- 'check for file existence, delete if necessary
- Set fso = CreateObject("Scripting.FileSystemObject")
- If fso.FileExists(LocalFileName) Then
- VBA.Kill LocalFileName
- Set fso = Nothing
- End If
- ' Open Internet Connecion
- hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
- ' Connect to FTP
- hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
- ' Change Directory
- Call FtpSetCurrentDirectory(hConnection, sDir)
- ' Set Download Flag to True
- FTPDownFile = True
- ' Download File
- If FTPGetFile(hConnection, RemoteFileName, LocalFileName, False, 1, 0, 1) = False Then
- FTPDownFile = False
- End If
- SetAttr LocalFileName, vbNormal + vbArchive
- ' Close Internet Connection
- Call InternetCloseHandle(hOpen)
- Call InternetCloseHandle(hConnection)
- End Function
Upload Function:
- Function FTPUploadFile(ByVal HostName As String, _
- ByVal UserName As String, _
- ByVal Password As String, _
- ByVal LocalFileName As String, _
- ByVal RemoteFileName As String, _
- ByVal sDir As String, _
- ByVal sMode As String) As Boolean
- 'If this function is not working, try playing with Flags and Context
- '(2nd and 3rd parameters in FTPPutFile
- ' Declare variables
- Dim hConnection, hOpen ' Used For Handles
- ' Open Internet Connecion
- hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
- ' Connect to FTP
- hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
- ' Change Directory
- Call FtpSetCurrentDirectory(hConnection, sDir)
- ' Set Download Flag to True
- FTPUploadFile = True
- ' Download File
- If FtpPutFile(hConnection, LocalFileName, RemoteFileName, 0, 1) = False Then
- FTPUploadFile = False
- End If
- 'Debug.Print LocalFileName
- SetAttr LocalFileName, vbNormal + vbArchive
- ' Close Internet Connection
- Call InternetCloseHandle(hOpen)
- Call InternetCloseHandle(hConnection)
- End Function
Error Notification (This can be used when you wish to deliver a more detailed error message, for the most part I do not use it, and just notify the user that file download/upload failed by looking at the function's return value):
- Sub ShowError()
- Dim lErr As Long, sErr As String, lenBuf As Long
- 'get the required buffer size
- InternetGetLastResponseInfo lErr, sErr, lenBuf
- 'create a buffer
- sErr = String(lenBuf, 0)
- 'retrieve the last respons info
- InternetGetLastResponseInfo lErr, sErr, lenBuf
- 'show the last response info
- MsgBox "Last Server Response : " + sErr, vbOKOnly + vbCritical
- End Sub
Finally, an example to call the function (from your form or another module, not the module you created to hold these functions):
- 'Attempt to Upload file
- If WininetFTPFunctions.FTPDownFile("ftp.domain.com", "myUserName", "myPassword", "Full path and Filename of local file", "Target Filename without path", "Directory on FTP server", "Upload Mode - Binary (&H2) or ASCII (&H1)") Then
- 'notify user of completion
- MsgBox "Upload - Complete!"
- 'or failure
- MsgBox "Upload - Failed!"
- End If
I hope this helps. It certainly has been invaluable to me.
Contributed by: AlexCuse



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.