Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Microsoft: Access Modules (VBA Coding) FAQ

FTP in Access

How do I FTP files in MS Access part 2
Posted: 2 Feb 07

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

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close