Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Import a Data File to a Control on a Form


View products that this article applies to.

This article was previously published under Q210452
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Summary

This article illustrates how to write the contents of a text data file to a control on a form.

↑ Back to the top


More information

The following sample code reads the contents of a text file into a text box on a form.
  1. Start Notepad and create a new text document by typing a list of at least 10 words. Save the file as MyText.txt.
  2. Start Microsoft Access and open a new blank database.
  3. Create a new form not based on any table.
  4. Add a text box to the form. Set its Name property to MyText1. Size the text box so that it's large enough to fit the contents of the text file that it will contain.
  5. Add a command button to the form. Set the button's properties as follows:
    OnClick: =WriteFile()
    Caption: Import Text
  6. Save the form as Form1.
  7. Create a new module and type the following line in the Declarations section if it is not already there:
    Option Explicit
  8. Type or paste the following procedure:
    Function WriteFile ()
       ' These variables store the text of the file.
       Dim Linedata as String
       Dim WholeFile as String
       ' Open the file.  Replace "<pathname>" with the correct path
       ' to MyText.txt.
       Open "C:\<pathname>\MyText.txt" For Input As #1
       ' Continue reading until end-of-file.
       Do While Not EOF(1)
          ' Read a line of data.
          Line Input #1, Linedata
          ' Store each line of data in the WholeFile variable.
          WholeFile = WholeFile + Chr(13) & Chr(10) + Linedata
       Loop
       ' Transfer contents of file to text box on form.
       Forms!Form1![MyText1] = WholeFile
       ' Close the data file.
       Close #1
    End Function
    					
  9. On the File menu, click Close and Return to Microsoft Access.
  10. Open Form1 in Form view and click the Import Text button.

    Note that the contents of the MyText.txt file appear in the text box.
You can erase the contents of the text box by setting it to an empty variable, such as the following:
Forms!Form1![My_Text1] = Null
You can call this expression from the Immediate window or you can assign it to another button on the form.

↑ Back to the top


Keywords: KB210452, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210452
Revision : 2
Created on : 6/30/2004
Published on : 6/30/2004
Exists online : False
Views : 325