Solved

Exporting an Access Table to a Text File in a Visual Basic Program

Posted on 2000-05-02
4
195 Views
Last Modified: 2013-11-25
Hello!  
I have an Access 97 database called Dogs, and I have a table called Breeds.  In my Visual Basic 6.0 proggie, I have a menu item called mnuFileExport.  When this menu item is clicked, the table Breeds should be exported to a text file called BreedsUpdate.txt on the A: drive.  How would I do this?  

Here is what I have so far and it doesn't work.
Private Sub mnuFileExport_Click()
Open "C:\Windows\Desktop\Dogs.mdb" For Input As #1
Open "A:\BreedsUpdate.txt" For Output As #2
Print #1, txtType.Text; txtDescription.Text; txtPrice.Text
Close #1
Close #2
End Sub

Thanks :)
0
Comment
Question by:zippy221
4 Comments
 
LVL 2

Accepted Solution

by:
manoj_r earned 20 total points
ID: 2770821
You can open a recordset and go through the records and print them in the file.

Try the code given below.

Dim db As Database
Dim rst As Recordset
Open "C:\BreedsUpdate.txt" For Output As #2
    Set db = OpenDatabase("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
    Set rst = db.OpenRecordset("Select * from customers")
   
    Do While Not rst.EOF
        Print #2, rst.Fields(0) & vbTab & rst.Fields(1)
        rst.MoveNext
    Loop
   
Close #2

0
 

Expert Comment

by:alamar
ID: 2771026
Well, let's see if we can make this a little more readable.
Try somthing like this:

'--------------------------------------------------------

Dim dbDogs As Database
Dim rsBreeds As Recordset
Dim i as integer         'Counter
Dim strOut as string  'output record string

'Clear out variables
strOut = ""
i=0

'Open file for output
Open "C:\BreedsUpdate.txt" For Output As #1

'Open the database
Set dbDogs = OpenDatabase("C:\Windows\Desktop\Dogs.mdb")

'build your recordset
Set rsBreeds = dbDogs.OpenRecordset("Select * from Breeds")

'go through each record, and output each record                                
Do While Not rsBreeds.EOF
    'First, build your output string
      for i=0 to rsBreeds.fields.count-1
           
          strOut = strOut & rsBreeds.fields(i) & ", "           'Make comma the delimiter
      next i
       'get rid of the last comma
        strOut = mid(strOut, 1, len(strOut)-2)

  Print #1, strOut

'Clear strOut
  strOut = ""
  rsBreeds.MoveNext
Loop
                                 
Close #1

'close files
rsBreeds.close
dbDogs.close

'--------------------------------------------------------
Hope this helps!


Alamar the WwonderLlama
0
 
LVL 2

Expert Comment

by:Simonac
ID: 2774816
U can use the following function:

Dim appAccess As Access.Application

appAccess.DoCmd.TransferText [parameters]

The equivalent also exist to tansfer to excel, Lotus1-2-3, etc...
0
 

Author Comment

by:zippy221
ID: 2778560
Thanks that was really clear and to the point.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 93
MS Date Picker 64 bit 32 bit issue 12 53
How to measure sizes and angles in scanned images ? 3 63
How to make an ADE file by code? 11 83
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question