Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I FTP files in MS Access part 2

FTP in Access

How do I FTP files in MS Access part 2

by  AlexCuse  Posted    (Edited  )
Hi All,

I have had these functions working now for a few months, so I figure they are good enough to share with the world. If you are interested in this topic, I am sure you have read 1DMF's FAQs on FTP'ing files from access. This is a great function, but I ran into problems uploading encrypted files (and sometimes zipped Access db's) using it. There also is not a function to download, which I needed.

So, I had most of the work done for me, but needed to come up with two more functions (one to upload files using FTPPutFile rather then InternetWriteFile, and one to download the files). One of these days I plan to add one for deleting files as well.

In case you have not read 1DMF's FAQ on this subject, look here for the declaration section:

faq705-5904

And now, on to the functions (download first):
Code:
Function FTPDownloadFile(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


Set fso = CreateObject("Scripting.FileSystemObject")
'check for existence of destination file
If fso.FileExists(LocalFileName) Then

    'delete it if found
    VBA.Kill LocalFileName
        
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
FTPDownloadFile = True

    ' Download File
    If FTPGetFile(hConnection, RemoteFileName, & _
        LocalFileName, False, 1, 0, 1) = False Then
        MsgBox "Download - Failed At FTPGetFile!"
        FTPDownloadFile = False

    End If
   
'ensure file is not saved as read-only
SetAttr LocalFileName, vbNormal + vbArchive


' Close Internet Connection
Call InternetCloseHandle(hOpen)
Call InternetCloseHandle(hConnection)

End Function

And now the upload function:
Code:
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 Upload Flag to True
FTPUploadFile = True


      
    ' Download File
    If FtpPutFile(hConnection, LocalFileName,  & _
        RemoteFileName, 0, 1) = False Then
        MsgBox "Download - Failed At FTPPutFile!"
        FTPUploadFile = False

    End If


' Close Internet Connection
Call InternetCloseHandle(hOpen)
Call InternetCloseHandle(hConnection)

End Function

Much thanks go to 1DMF for his FAQs, had I not read them I would still be creating FTP scripts on the fly and running them through command window (this was not very reliable).

Please let me know if you have any suggestions to improve these functions or questions.

Thanks,

Alex
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top