By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,662 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,662 IT Pros & Developers. It's quick & easy.

Posting to a website

100+
P: 435
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.
Jan 12 '14 #1
Share this Question
Share on Google+
13 Replies


zmbd
Expert Mod 5K+
P: 5,287
Really not enough information; thus, the answer is maybe.

It depends on how you are uploading the files.
Jan 12 '14 #2

100+
P: 435
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
Jan 12 '14 #3

zmbd
Expert Mod 5K+
P: 5,287
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?
Jan 13 '14 #4

100+
P: 435
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.
Jan 13 '14 #5

100+
P: 435
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
Jan 13 '14 #6

zmbd
Expert Mod 5K+
P: 5,287
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
Jan 13 '14 #7

100+
P: 435
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:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. ' Set Constants
  5. Const FTP_TRANSFER_TYPE_ASCII = &H1
  6. Const FTP_TRANSFER_TYPE_BINARY = &H2
  7. Const INTERNET_DEFAULT_FTP_PORT = 21
  8. Const INTERNET_SERVICE_FTP = 1
  9. Const INTERNET_FLAG_PASSIVE = &H8000000
  10. Const GENERIC_WRITE = &H40000000
  11. Const BUFFER_SIZE = 100
  12. Const PassiveConnection As Boolean = True
  13.  
  14. ' Declare wininet.dll API Functions
  15. Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
  16.     (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
  17.  
  18. Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" _
  19.    (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Boolean
  20.  
  21. Public Declare Function InternetWriteFile Lib "wininet.dll" _
  22. (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToWite As Long, _
  23. dwNumberOfBytesWritten As Long) As Integer
  24.  
  25. Public Declare Function FtpOpenFile Lib "wininet.dll" Alias "FtpOpenFileA" _
  26. (ByVal hFtpSession As Long, ByVal sBuff As String, ByVal Access As Long, ByVal Flags As Long, ByVal Context As Long) As Long
  27.  
  28. Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
  29. (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
  30.       ByVal lpszRemoteFile As String, _
  31.       ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
  32.  
  33. Public Declare Function FtpDeleteFile Lib "wininet.dll" _
  34.     Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
  35.     ByVal lpszFileName As String) As Boolean
  36. Public Declare Function InternetCloseHandle Lib "wininet.dll" _
  37. (ByVal hInet As Long) As Long
  38.  
  39. Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
  40. (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
  41. ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
  42.  
  43. Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
  44. (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
  45. ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
  46. ByVal lFlags As Long, ByVal lContext As Long) As Long
  47.  
  48.  
  49. Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
  50. (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
  51.       ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, _
  52.       ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
  53.  
  54. Declare Function InternetGetLastResponseInfo Lib "wininet.dll" _
  55.       Alias "InternetGetLastResponseInfoA" _
  56.        (ByRef lpdwError As Long, _
  57.        ByVal lpszErrorBuffer As String, _
  58.        ByRef lpdwErrorBufferLength As Long) As Boolean
  59.  
  60. Function FTPFile(ByVal HostName As String, _
  61.     ByVal UserName As String, _
  62.     ByVal Password As String, _
  63.     ByVal LocalFileName As String, _
  64.     ByVal RemoteFileName As String, _
  65.     ByVal sDir As String, _
  66.     ByVal sMode As String) As Boolean
  67.  
  68.     On Error GoTo Err_Function
  69.  
  70. ' Declare variables
  71. Dim hConnection, hOpen, hFile  As Long ' Used For Handles
  72. Dim iSize As Long ' Size of file for upload
  73. Dim Retval As Variant ' Used for progress meter
  74. Dim iWritten As Long ' Used by InternetWriteFile to report bytes uploaded
  75. Dim iLoop As Long ' Loop for uploading chuncks
  76. Dim iFile As Integer ' Used for Local file handle
  77. Dim FileData(BUFFER_SIZE - 1) As Byte ' buffer array of BUFFER_SIZE (100) elements 0 to 99
  78.  
  79. ' Open Internet Connecion
  80. hOpen = InternetOpen("FTP", 1, "", vbNullString, 0)
  81.  
  82. ' Connect to FTP
  83. hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
  84.  
  85. ' Change Directory
  86. Call FtpSetCurrentDirectory(hConnection, sDir)
  87.  
  88. ' Open Remote File
  89. hFile = FtpOpenFile(hConnection, RemoteFileName, GENERIC_WRITE, IIf(sMode = "Binary", FTP_TRANSFER_TYPE_BINARY, FTP_TRANSFER_TYPE_ASCII), 0)
  90.  
  91. ' Check for successfull file handle
  92. If hFile = 0 Then
  93.     MsgBox "Internet - Failed!"
  94.     ShowError
  95.     FTPFile = False
  96.     GoTo Exit_Function
  97. End If
  98.  
  99. ' Set Upload Flag to True
  100. FTPFile = True
  101.  
  102. ' Get next file handle number
  103. iFile = FreeFile
  104.  
  105. ' Open local file
  106. Open LocalFileName For Binary Access Read As iFile
  107.  
  108. ' Set file size
  109. iSize = LOF(iFile)
  110.  
  111. ' Iinitialise progress meter
  112. Retval = SysCmd(acSysCmdInitMeter, "Uploading File (" & RemoteFileName & ")", iSize / 1000)
  113.  
  114. ' Loop file size
  115. For iLoop = 1 To iSize \ BUFFER_SIZE
  116.  
  117.     ' Update progress meter
  118.     Retval = SysCmd(acSysCmdUpdateMeter, (BUFFER_SIZE * iLoop) / 1000)
  119.  
  120.     'Get file data
  121.     Get iFile, , FileData
  122.  
  123.     ' Write chunk to FTP checking for success
  124.     If InternetWriteFile(hFile, FileData(0), BUFFER_SIZE, iWritten) = 0 Then
  125.         MsgBox "Upload - Failed!"
  126.         ShowError
  127.         FTPFile = False
  128.        GoTo Exit_Function
  129.     Else
  130.         ' Check buffer was written
  131.         If iWritten <> BUFFER_SIZE Then
  132.             MsgBox "Upload - Failed!"
  133.             ShowError
  134.             FTPFile = False
  135.             GoTo Exit_Function
  136.         End If
  137.     End If
  138.  
  139. Next iLoop
  140.  
  141. ' Handle remainder using MOD
  142.  
  143.     ' Update progress meter
  144.     Retval = SysCmd(acSysCmdUpdateMeter, iSize / 1000)
  145.  
  146.     ' Get file data
  147.     Get iFile, , FileData
  148.  
  149.     ' Write remainder to FTP checking for success
  150.     If InternetWriteFile(hFile, FileData(0), iSize Mod BUFFER_SIZE, iWritten) = 0 Then
  151.         MsgBox "Upload - Failed!"
  152.         ShowError
  153.         FTPFile = False
  154.         GoTo Exit_Function
  155.     Else
  156.         ' Check buffer was written
  157.         If iWritten <> iSize Mod BUFFER_SIZE Then
  158.             MsgBox "Upload - Failed!"
  159.             ShowError
  160.             FTPFile = False
  161.             GoTo Exit_Function
  162.         End If
  163.     End If
  164.  
  165. Exit_Function:
  166.  
  167. ' remove progress meter
  168. Retval = SysCmd(acSysCmdRemoveMeter)
  169.  
  170. 'close remote file
  171. Call InternetCloseHandle(hFile)
  172.  
  173. 'close local file
  174. Close iFile
  175.  
  176. ' Close Internet Connection
  177. Call InternetCloseHandle(hOpen)
  178. Call InternetCloseHandle(hConnection)
  179.  
  180. Exit Function
  181.  
  182. Err_Function:
  183. MsgBox "Error in FTPFile : " & Err.Description
  184. GoTo Exit_Function
  185.  
  186. End Function
  187.  
  188. Sub ShowError()
  189.    Dim lErr As Long, sErr As String, lenBuf As Long
  190.    'get the required buffer size
  191.    InternetGetLastResponseInfo lErr, sErr, lenBuf
  192.    'create a buffer
  193.    sErr = String(lenBuf, 0)
  194.    'retrieve the last respons info
  195.    InternetGetLastResponseInfo lErr, sErr, lenBuf
  196.    'show the last response info
  197.    MsgBox "Last Server Response : " + sErr, vbOKOnly + vbCritical
  198. End Sub
  199.  
Code behind the form.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Upload_Click()
  2.  
  3. On Error GoTo Err_Upload_Click
  4.  
  5. ' Check for Selected file
  6. If Nz(Me!FileName, "") = "" Then
  7.     MsgBox "Please select file to upload first!"
  8.     Exit Sub
  9. End If
  10.  
  11. ' Check for FTP Server
  12. If Nz(Me!FTPDomain, "") = "" Then
  13.     MsgBox "Please enter FTP Domain!"
  14.     Exit Sub
  15. End If
  16.  
  17. ' Check for UserName
  18. If Nz(Me!UserName, "") = "" Then
  19.     MsgBox "Please enter User Name!"
  20.     Exit Sub
  21. End If
  22.  
  23. ' Check for Password
  24. If Nz(Me!FTPPWord, "") = "" Then
  25.     MsgBox "Please enter Password!"
  26.     Exit Sub
  27. End If
  28.  
  29. ' Set Default upload directory to root if nothing supplied
  30. If Nz(Me!ServerDir, "") = "" Then
  31.     Me!ServerDir = "/"
  32.     Me.Refresh
  33. End If
  34.  
  35. ' Upload file
  36. If FTPFile(Me!FTPDomain, Me!UserName, Me!FTPPWord, Me!FileName, Me!ShortName, Me!ServerDir, Me!Mode) Then
  37.     MsgBox "Upload - Complete!"
  38. End If
  39.  
  40. Exit_Upload_Click:
  41.     Exit Sub
  42.  
  43. Err_Upload_Click:
  44.     MsgBox "Error in Upload_Click : " & Err.Description
  45.     Resume Exit_Upload_Click
  46.  
  47. End Sub
  48.  
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.
Jan 14 '14 #8

zmbd
Expert Mod 5K+
P: 5,287
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?
Jan 14 '14 #9

100+
P: 435
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
Jan 14 '14 #10

NeoPa
Expert Mod 15k+
P: 31,271
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.
Jan 14 '14 #11

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

100+
P: 435
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.
Jan 14 '14 #13

NeoPa
Expert Mod 15k+
P: 31,271
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.
Jan 14 '14 #14

Post your reply

Sign in to post your reply or Sign up for a free account.