Solved

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

Posted on 2000-05-02
4
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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