ACC97: Example Using the Internet Transfer ActiveX Control

ID: Q163653


The information in this article applies to:
  • Microsoft Access 97

Moderate: Requires basic macro, coding, and interoperability skills.


SUMMARY

This article shows you two examples of how to use the Microsoft Internet Transfer ActiveX Control that is included with the Microsoft Office Developer Edition 97.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access 97" manual.


MORE INFORMATION

The Internet Transfer ActiveX Control provides you with access to the Internet and the World Wide Web using the two most common protocols: Hypertext Transfer Protocol (HTTP) and File Transfer Protocol (FTP). When you use the internet transfer control with HTTP, you can retrieve HTML documents from the Internet or an intranet. Using the internet transfer control with FTP, you can log on to FTP servers and download or upload files; the control supports many of the most common FTP commands such as GET, DIR, DELETE and CD.

Example 1 - Using the OpenURL Method to Retrieve an HTML Page

The following example uses the internet transfer control with HTTP to create an HTML page based on Microsoft's World Wide Web home page, and to view the header information for the page.
  1. Start Microsoft Access and open the sample database Northwind.mdb.


  2. Create a new form not based on any table or query in Design view:


  3. 
           Form: frmOpenURL
           --------------------------
           Caption: OpenURL Form
    
           Command button:
              Name: cmdWriteFile
              Caption: Write to Disk
    
           Command button:
              Name: cmdGetHeader
              Caption: Get Header 
  4. On the Insert menu, click ActiveX control.


  5. In the Insert ActiveX Control dialog box, select Microsoft Internet Transfer Control, version 5.0, and then click OK.


  6. Set the internet control's Name property to axInetTran.


  7. On the View menu, click Code.


  8. Type the following line in the Declarations section of the form's class module:


  9. 
           Dim objInet as Inet 
  10. Type the following procedures for the form's Load event and for the Click event of each command button:


  11. 
          Private Sub Form_Load()
             ' Set a reference to the internet transfer control.
             Set objInet = Me!axInetTran.Object
          End Sub
    
          Private Sub cmdWriteFile_Click()
             Dim b() as Byte
    
             ' Set the internet transfer control protocol and URL.
             objInet.Protocol = icHTTP
             objInet.URL = "HTTP://www.microsoft.com"
    
             ' Retrieve the HTML data into a byte array.
             b() = objInet.OpenURL(objInet.URL,icByteArray)
    
             ' Create a local file from the retrieved data.
             Open "C:\Homepage.htm" For Binary Access Write As #1
             Put #1, , b()
             Close #1
    
             MsgBox "Done"
          End Sub
    
          Private Sub cmdGetHeader_Click()
             ' Set the internet transfer control protocol and URL.
             objInet.Protocol = icHTTP
             objInet.URL = "HTTP://www.microsoft.com"
    
             ' Open the HTML and display the header information.
             objInet.openURL objInet.URL, icByteArray
             MsgBox objInet.GetHeader
          End Sub 
  12. Save the frmOpenURL form, and switch it to Form view.


  13. Click the Write To Disk button. Use Windows Explorer or File Manager to verify that the file C:\Homepage.htm has been created on your hard drive. You can open the file in your Web browser to view the contents of Microsoft's home page.

    NOTE: No graphics appear on the page; only the text was transferred.


  14. Click the Get Header button. Note the message box that displays the header information from Microsoft's home page on the World Wide Web.


Example 2 - Using FTP to Retrieve a File from an FTP Site

The following example uses the internet transfer control with FTP to retrieve a file called Dirmap.txt from Microsoft's FTP site.

NOTE: The internet transfer control File Transfer Protocol will not work with some Internet proxy servers. If you do not have a direct connection to the Internet through an Internet Service Provider (ISP), verify that your proxy server supports FTP connections.
  1. Start Microsoft Access and open the sample database Northwind.mdb.


  2. Create a new form not based on any table or query in Design view:


  3. 
          Form: frmRetrieveFile
          --------------------------
          Caption: Retrieve FTP File
    
          Command button:
             Name: cmdGetFile
             Caption: Get FTP File
          Text box:
             Name: txtFTPSite
          Text box:
             Name: txtFileName 
  4. On the Insert menu, click ActiveX control.


  5. In the Insert ActiveX Control dialog box, select Microsoft Internet Transfer Control, version 5.0, and then click OK.


  6. Set the internet control's Name property to axFTP.


  7. On the View menu, click Code.


  8. Type the following line in the Declarations section of the form's class module:


  9. 
           Dim objFTP as Inet 
  10. Type the following procedures for the form's Load event, for the Click event of the command button, and for the StateChanged event of the internet transfer control:


  11. 
          Private Sub Form_Load()
             ' Set a reference to the internet transfer control.
             Set objFTP = Me!axFTP.Object
          End Sub
    
          Private Sub cmdGetFile_Click()
             Dim strSite As String
             Dim strFile As String
             strSite = Me!txtFTPSite
             strFile = Me!txtFileName
             objFTP.Protocol = icFTP
             objFTP.URL = strSite
             objFTP.Execute strSite, "Get " & strFile & " C:\" & strFile
          End Sub
    
          Private Sub axFTP_StateChanged(ByVal State As Integer)
             ' Display a message when the transfer is finished.
             If State = 12 Then Msgbox "File Transferred"
          End Sub 
  12. Save the frmRetrieveFile form, and switch it to Form view. Type ftp://ftp.microsoft.com in the txtFTPSite text box, type Dirmap.txt in the txtFileName text box, and then click the Get FTP File button. After Dirmap.txt is transferred to C:\, a message box is displayed.



REFERENCES

For more information about Internet Transfer ActiveX Control, search the Help Index for "Internet Transfer control."

For more information about the Execute method, search the Help Index for "Execute method (Internet Transfer Control)."

For more information about the StateChanged event, search the Help Index for "StateChanged event."

Additional query words: ole custom

Keywords : kbusage kbdta AccCon KbVBA
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: August 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.