Posting to a website

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CD Tom
    Contributor
    • Feb 2009
    • 495

    Posting to a website

    In my application I can create PDF's from any reports. I would like to be able to post those PDF's directly to my website, Question is there a way to do this directly from the program.
    Thanks for the help I always can find answers here.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Really not enough information; thus, the answer is maybe.

    It depends on how you are uploading the files.

    Comment

    • CD Tom
      Contributor
      • Feb 2009
      • 495

      #3
      Right now I am using ipswitch WS-FTP I connect to the website and then upload the PDF file. If possible I would like to make this an option in the program so a user could put his web address user name and password into a file where I could let the VB program use it to connect to the given website and upload the file automatically.
      I hope this is a little more of what you need.
      Thanks

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        You didn't mention which version of Access.

        There are some code snippettes about useing FTP out there on the net that might prove usefull to you; however, nothing native that I'm aware of at this time.

        I'll dig a little bit deeper, there may be something in the publishing routines, in ACC2010 they're for sharepoint now; however, who knows?

        Comment

        • CD Tom
          Contributor
          • Feb 2009
          • 495

          #5
          Thanks, I'm using Access 2007 right now. I'll look around on the web and see what I can find. If I find anything I'll post it.

          Comment

          • CD Tom
            Contributor
            • Feb 2009
            • 495

            #6
            I found something on the web that seems like what I was looking for. I've put that information into my program but can't get it to work. I keep getting the message internet failed. The link to the program is http://www.tek-tips.com/faqs.cfm?fid=5904 I don't understand some of this code. Don't know if you can help but thought I'd ask.
            Thanks for any help

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              I suggest you start out here as this will help you catch the most common things: > Before Posting (VBA or SQL) Code

              If that doesn't work, then insert what you have here... the reason I ask that is because there maybe typos or other goodies the Gremlins inserted for you while you typed. (I see them do that to me often - You know I'm a perfect typest so it as to be the Gremlins, right? };-D ).

              Please remember to Format your code with the [CODE/] button

              Comment

              • CD Tom
                Contributor
                • Feb 2009
                • 495

                #8
                Ok I can do that. However I did just copy and paste the code into the system. I did a compile and everything was ok. Anyway here's all the code
                Modules:
                Code:
                Option Compare Database
                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 GENERIC_WRITE = &H40000000
                Const BUFFER_SIZE = 100
                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
                       
                Function FTPFile(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
                    
                    On Error GoTo Err_Function
                        
                ' Declare variables
                Dim hConnection, hOpen, hFile  As Long ' Used For Handles
                Dim iSize As Long ' Size of file for upload
                Dim Retval As Variant ' Used for progress meter
                Dim iWritten As Long ' Used by InternetWriteFile to report bytes uploaded
                Dim iLoop As Long ' Loop for uploading chuncks
                Dim iFile As Integer ' Used for Local file handle
                Dim FileData(BUFFER_SIZE - 1) As Byte ' buffer array of BUFFER_SIZE (100) elements 0 to 99
                
                ' 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)
                
                ' Open Remote File
                hFile = FtpOpenFile(hConnection, RemoteFileName, GENERIC_WRITE, IIf(sMode = "Binary", FTP_TRANSFER_TYPE_BINARY, FTP_TRANSFER_TYPE_ASCII), 0)
                
                ' Check for successfull file handle
                If hFile = 0 Then
                    MsgBox "Internet - Failed!"
                    ShowError
                    FTPFile = False
                    GoTo Exit_Function
                End If
                
                ' Set Upload Flag to True
                FTPFile = True
                
                ' Get next file handle number
                iFile = FreeFile
                
                ' Open local file
                Open LocalFileName For Binary Access Read As iFile
                
                ' Set file size
                iSize = LOF(iFile)
                
                ' Iinitialise progress meter
                Retval = SysCmd(acSysCmdInitMeter, "Uploading File (" & RemoteFileName & ")", iSize / 1000)
                
                ' Loop file size
                For iLoop = 1 To iSize \ BUFFER_SIZE
                        
                    ' Update progress meter
                    Retval = SysCmd(acSysCmdUpdateMeter, (BUFFER_SIZE * iLoop) / 1000)
                        
                    'Get file data
                    Get iFile, , FileData
                      
                    ' Write chunk to FTP checking for success
                    If InternetWriteFile(hFile, FileData(0), BUFFER_SIZE, iWritten) = 0 Then
                        MsgBox "Upload - Failed!"
                        ShowError
                        FTPFile = False
                       GoTo Exit_Function
                    Else
                        ' Check buffer was written
                        If iWritten <> BUFFER_SIZE Then
                            MsgBox "Upload - Failed!"
                            ShowError
                            FTPFile = False
                            GoTo Exit_Function
                        End If
                    End If
                    
                Next iLoop
                
                ' Handle remainder using MOD
                
                    ' Update progress meter
                    Retval = SysCmd(acSysCmdUpdateMeter, iSize / 1000)
                
                    ' Get file data
                    Get iFile, , FileData
                    
                    ' Write remainder to FTP checking for success
                    If InternetWriteFile(hFile, FileData(0), iSize Mod BUFFER_SIZE, iWritten) = 0 Then
                        MsgBox "Upload - Failed!"
                        ShowError
                        FTPFile = False
                        GoTo Exit_Function
                    Else
                        ' Check buffer was written
                        If iWritten <> iSize Mod BUFFER_SIZE Then
                            MsgBox "Upload - Failed!"
                            ShowError
                            FTPFile = False
                            GoTo Exit_Function
                        End If
                    End If
                               
                Exit_Function:
                
                ' remove progress meter
                Retval = SysCmd(acSysCmdRemoveMeter)
                
                'close remote file
                Call InternetCloseHandle(hFile)
                
                'close local file
                Close iFile
                
                ' Close Internet Connection
                Call InternetCloseHandle(hOpen)
                Call InternetCloseHandle(hConnection)
                
                Exit Function
                
                Err_Function:
                MsgBox "Error in FTPFile : " & Err.Description
                GoTo Exit_Function
                
                End Function
                
                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
                Code behind the form.
                Code:
                Private Sub Upload_Click()
                
                On Error GoTo Err_Upload_Click
                
                ' Check for Selected file
                If Nz(Me!FileName, "") = "" Then
                    MsgBox "Please select file to upload first!"
                    Exit Sub
                End If
                
                ' Check for FTP Server
                If Nz(Me!FTPDomain, "") = "" Then
                    MsgBox "Please enter FTP Domain!"
                    Exit Sub
                End If
                
                ' Check for UserName
                If Nz(Me!UserName, "") = "" Then
                    MsgBox "Please enter User Name!"
                    Exit Sub
                End If
                
                ' Check for Password
                If Nz(Me!FTPPWord, "") = "" Then
                    MsgBox "Please enter Password!"
                    Exit Sub
                End If
                
                ' Set Default upload directory to root if nothing supplied
                If Nz(Me!ServerDir, "") = "" Then
                    Me!ServerDir = "/"
                    Me.Refresh
                End If
                
                ' Upload file
                If FTPFile(Me!FTPDomain, Me!UserName, Me!FTPPWord, Me!FileName, Me!ShortName, Me!ServerDir, Me!Mode) Then
                    MsgBox "Upload - Complete!"
                End If
                
                Exit_Upload_Click:
                    Exit Sub
                
                Err_Upload_Click:
                    MsgBox "Error in Upload_Click : " & Err.Description
                    Resume Exit_Upload_Click
                
                End Sub
                I'm not sure how this is supposed to connect to the internet, I noticed the use of the wininet.dll. Wish I knew more about this. Thanks for your help again.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  Sorry, I should have asked you for the exact error and for the method/code you are using to call the procedure.

                  Have you tried stepping thru the code?

                  Comment

                  • CD Tom
                    Contributor
                    • Feb 2009
                    • 495

                    #10
                    OK, I fillin the form an select the Click to upload it calls the FTPFile with all the information. When it gets to the 'Open Internet Connection the hOpen shows 13369348 the next step 'connect to FTP the hConnection shows a 0, the next step 'Open Remote File the hFile shows a 0, the 'check for successful file handle is where the error shows up.
                    I would send you the website I'm trying to connect to but don't want to give out the password. I hope you understand.

                    Thanks

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      Silly question Tom, but have you checked that you can access your FTP site manually (using any other software) at around the same time you run any of your tests?

                      If not, it's worth excluding that possibility from the mix.

                      Comment

                      • CD Tom
                        Contributor
                        • Feb 2009
                        • 495

                        #12
                        Yes I normally use ipswitch to connect and have no problem. In the form it shows FTP Domain(ftp.doma in.com) I've tried that way and ftp:// and still get the same error.

                        Comment

                        • CD Tom
                          Contributor
                          • Feb 2009
                          • 495

                          #13
                          Ok, after I logged onto the Powweb.com website and checked my ftp site I found that I was using the wrong ftp domain name. I changed it to the correct one and everything worked fine. I guess if I knew what I was doing I would have looked at that sooner. Thanks for all you expert help.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            Often, when looking at code and all seems fine except it doesn't work, the answer lies elsewhere. A nudge is often all that's required to get you looking at other possible reasons. You did a good job finding that.

                            Comment

                            Working...