Solved

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

Posted on 2000-05-02
4
192 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now