FTP to Upload documents (1 Viewer)

Harris@Z

Registered User.
Local time
Today, 14:30
Joined
Oct 28, 2019
Messages
73
Hi, I wonder if anyone can assist me please.

The below code works well in my Access Database at work, but somehow not in the same database from home.
I modified the code to allow 64bit systems (computer at work), whereas one at home is 32 bit.

The file to upload's directory is changed from my work computer to my home computer.
I have stripped the username, password and user directory from the code below

(Code thanks to https://analystcave.com/vba-downloading-files-from-ftp-using-vba/)

Code:
Option Compare Database

Private Const FTP_TRANSFER_TYPE_UNKNOWN     As Long = 0
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Private Declare PtrSafe Function InternetOpenA Lib "wininet.dll" ( _
    ByVal sAgent As String, _
    ByVal lAccessType As Long, _
    ByVal sProxyName As String, _
    ByVal sProxyBypass As String, _
    ByVal lFlags As Long) As Long


Private Declare PtrSafe Function InternetConnectA Lib "wininet.dll" ( _
    ByVal hInternetSession As Long, _
    ByVal sServerName As String, _
    ByVal nServerPort As Long, _
    ByVal sUsername As String, _
    ByVal sPassword As String, _
    ByVal lService As Long, _
    ByVal lFlags As Long, _
    ByVal lcontext As Long) As Long

Private Declare PtrSafe Function FtpPutFileA _
   Lib "wininet.dll" _
_
       (ByVal hFtpSession As Long, _
        ByVal lpszLocalFile As String, _
        ByVal lpszRemoteFile As String, _
        ByVal dwFlags As Long, _
        ByVal dwContext As Long) As Boolean

Private Declare PtrSafe Function InternetCloseHandle Lib "wininet" ( _
    ByVal hInet As Long) As Long

Sub FtpUpload(ByVal strLocalFile As String, ByVal strRemoteFile As String, ByVal strHost As String, ByVal lngPort As Long, ByVal strUser As String, ByVal strPass As String)

    Dim hOpen   As Long
    Dim hConn   As Long

    hOpen = InternetOpenA("FTPGET", 1, vbNullString, vbNullString, 1)
    hConn = InternetConnectA(hOpen, strHost, lngPort, strUser, strPass, 1, 0, 2)

    If FtpPutFileA(hConn, strLocalFile, strRemoteFile, FTP_TRANSFER_TYPE_UNKNOWN Or INTERNET_FLAG_RELOAD, 0) Then
        Debug.Print "Success"
    Else
        Debug.Print "Fail"
    End If

    'Close connections
    InternetCloseHandle hConn
    InternetCloseHandle hOpen

End Sub

Sub TestUpload()
   FtpUpload "H:\My Documents\AccessDatabases\Exports\Setup.pdf", "/xxxx.com/public_html/Reports/Setup.pdf", _
            "userwebsite", 21, "username", "userpassword"
End Sub
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:30
Joined
Feb 28, 2001
Messages
27,186
I modified the code to allow 64bit systems (computer at work), whereas one at home is 32 bit.

Clarify this statement, please. In order to help you make this run at home, we need to know a bit about the environments.

Do you mean that you are running 32-bit Access on a 64-bit machine at home, or is the home machine an older 32-bit CPU which, of necessity, is running 32-bit Access? And is the version running at the office Access 64-bit?

Also, though it isn't necessarily a big thing, when you show code segments, use the code tags to help them stand out properly. In the post's menu bar, the code tags look like < / > and you can indent things in a way that will stick around - unlike the normal text areas of the forum.
 

Harris@Z

Registered User.
Local time
Today, 14:30
Joined
Oct 28, 2019
Messages
73
Apologies for the way I posted the code! And thanks for pointing this out.
The database is primarily developed on my home computer, Access 2016 and 32-bit Access and Windows 10.
At work runs on Microsoft Office 365 Access, 64-bit machine - Windows 11 Pro

H
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:30
Joined
Sep 21, 2011
Messages
14,305
I believe you would have to use compiler directives, so it runs on both bitnesses.
#VBA7 is the one you need I think.
Never had to use it myself.
 

GPGeorge

Grover Park George
Local time
Today, 04:30
Joined
Nov 25, 2004
Messages
1,867
Apologies for the way I posted the code! And thanks for pointing this out.
The database is primarily developed on my home computer, Access 2016 and 32-bit Access and Windows 10.
At work runs on Microsoft Office 365 Access, 64-bit machine - Windows 11 Pro

H
The bitness of the Operating System does not determine the bitness of the Office/Access application. Therefore, the fact that "At work [it] runs on Microsoft Office 365 Access, 64-bit machine - Windows 11 Pro" does not mean that the Access application is also 64 bit. It may or may not be.
In other words, that may be a red herring in this problem.

Show us this property from both Access applications please. Go to File-->Account-->About Access
1696884130387.png
 

Harris@Z

Registered User.
Local time
Today, 14:30
Joined
Oct 28, 2019
Messages
73
Thanks for your guidance! I am not a professional developer so learning fast!

Home computer: Microsoft® Access® 2016 MSO (Version 2302 Build 16.0.16130.20754) 32-bit
Office computer: Microsoft® Access® for Microsoft 365 MSO (Version 2309 Build 16.0.16827.20130) 64-bit
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:30
Joined
Sep 21, 2011
Messages
14,305
This is what I was thinking of.
 

Harris@Z

Registered User.
Local time
Today, 14:30
Joined
Oct 28, 2019
Messages
73
Thanks everyone for your input.
After trying a range of variations, I eventually discovered that the antivirus software/firewall was blocking the code from executing!
Hard lesson learnt, and a possible solution to others having this problem in the future.
 

Users who are viewing this thread

Top Bottom