Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

CREATE TEXT FILE FROM MSACCESS

Creating Text File from Microsoft Access

The FileSystemObject Object provides access to the computer's file system.

The following code illustrates how the FileSystemObject is used to return a TextStream object that can be read from or written to:

Syntax : Scripting.FileSystemObject

Example:

Sub CreateTextFile()
Dim fs As Object, txt
On Error goto CreateTextFile_Err
Set fs = CreateObject("Scripting.FileSystemObject")
Set txt = fs.CreateTextFile("C:\mytest.txt", True)
txt.writeline ("This is a test.")
txt.Close

CreateTextFile_Exit:
Exit Sub

CreateTextFile_Err:
Msgbox Err.Description,,"CreateTextFile"
Resume CreateTextFile_Exit
End Sub 

In the code shown above, the CreateObject function returns the FileSystemObject (fs). The CreateTextFile method then creates the file as a TextStream object txt, and the WriteLine method writes a line of text to the created text file. The Close method flushes the buffer and closes the file.


Reading From Text File

Reading Text File using FileSystemObject Example:

Sub ReadTextFile()
Dim fs As Object, txt, txtline

On Error Goto ReadTextFile_Err

Set fs = CreateObject("Scripting.FileSystemObject")
Set txt = fs.opentextfile("C:\mytest.txt")
txtline = txt.readline
txt.Close

MsgBox "C:\mytest.txt File contents : " & txtline

ReadTextFile_Exit:
Exit Sub

ReadTextFile_Err:
Msgbox Err.Description,,"ReadTextFile"
Resume ReadTextFile_Exit
End Sub 

Next >> Rename File

Share:

5 comments:

  1. This post is invaluable, I completely liked it, I will be back for much more!

    ReplyDelete
  2. Can you use this same a.writeline to write more than one line of code. I am trying to make a text file and merge in values from a form into the text document im creating. I wanted to use reports as its easier, but i cant find a function to spit out a .txt file of the document. All im trying to do is create a html page based on table values and save it to location on my C:drive

    ReplyDelete
  3. Yes, you can repeat the a.Writeline() statement with values to write into the text file as many times as you like. Each item will be written on a separate line.

    ReplyDelete
  4. this is a stupid webssiteeeee, if ur gunna make a website atleast make it useful,u freakss..
    this did not help at all....
    ill make sure no one sees this site again
    all i wanted to kno is how to create a textfile step by step and u cant even show me that
    uselessss!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  5. If you are trying write a text file based on information in a form:
    Go to the form design view
    Insert a new button
    Right-click the button, go to the event tab and select the ... for "On Click"
    Select the code builder option and enter the code:

    Dim fs As Object, a, txtline
    On Error Goto ReadTextFile_Err
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.opentextfile("C:\mytest.txt")
    txtline = a.writeline
    a.Close
    MsgBox "C:\mytest.txt File contents : " & txtline

    ReadTextFile_Exit:
    Exit Sub

    ReadTextFile_Err:
    Msgbox Err.Description,,"ReadTextFile"
    Resume ReadTextFile_Exit
    End Sub

    The line txtline = a.writeline is where you would but the body of your message enclosed in parenthesis (). You can then insert text inside of " " or field using the & Field Name &. If you want a new line enter vbCrLf _ at the end of the line. Each new line must start with &

    For Example:
    & "This survey was completed by " & Contact & " (" & Contact_Title & ") on: " & Date_Called & vbCrLf & vbCrLf _

    ReplyDelete

Comments are subject to moderation before publishing.

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

http://clicky.com/101199826
Blog Directory Visit blogadda.com to discover Indian blogs

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms How Tos Functions MS-Access Security Reports msaccess forms Animations msaccess animation Class Module Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array Queries msaccess reports External Links msaccess tips Accesstips Downloads Objects Property Menus and Toolbars Controls Event MsaccessLinks Process Controls Art Work Collection Object WithEvents msaccess How Tos Combo Boxes Graph Charts List Boxes VBA msaccessQuery Command Buttons Command Button Data Dictionary Object Emails and Alerts Form Query RaiseEvent Report Calculation Custom Wizards DOS Commands Data Type ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item Object Reference msaccessprocess security advanced Access Security Add Custom Functions Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Copy Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database ms-access msaccess wizards reference text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands function hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload

Featured Post

WithEvents and All Form Control Types

Introduction So far we have worked extensively with only one type of Control: the TextBoxes on Form, enabled with Events to Raise and captu...

Labels

Blog Archive

Recent Posts