saving data from table into a text file

  • Thread starter Thread starter Guedj54
  • Start date Start date
G

Guedj54

Hi,
I have two tables table a and table b, both got a field called ID
number.
I need to save in a txt file the data of those two tables regarding a
given ID number.
How do I do that?
for example
Table a:
ID name grade
2 Jon 7
3 Dan 6

Table B:
P ID Location
1 2 London
2 3 New York
3 2 London

I will need in the text file the following for ID 2 for example


Table A
ID name grade
2 Jon 7
Table B:
P ID Location
1 2 London
3 2 London


Many thanks.
R
 
Try something like:

Dim rst As DAO.Recordset
Dim fname As String, strSQL As String
Dim vID as Long

vID = 2
fname = "TargetPathAndFileName" & vID & ".txt"

Open fname For Output As #1
Print #1, "Table A"

strSQL = "SELECT ID, name, grade FROM [Table A] WHERE ID = " & vID
Set rst = CurrentDB.OpenRecordset (strSQL)
rst.MoveFirst
Print #1, rst.Fields(0).Name, rst.Fields(1).Name, rst.Fields(2).Name
Do Until rst.EOF
Print #1, rst.Fields(0), rst.Fields(1), rst.Fields(2)
rst.MoveNext
Loop
rst.Close

strSQL = "SELECT P, ID, Location FROM [Table B] WHERE ID = " & vID
Set rst = CurrentDB.OpenRecordset (strSQL)
rst.MoveFirst
Print #1, rst.Fields(0).Name, rst.Fields(1).Name, rst.Fields(2).Name
Do Until rst.EOF
Print #1, rst.Fields(0), rst.Fields(1), rst.Fields(2)
rst.MoveNext
Loop
rst.Close

Set rst = Nothing
Close #1


Just make sure you have changed the table and field names as required to
match the actual ones, and include DAO in your references if not already
there (DAO 3.51 for A97, DAO 3.6 for A2K or later).

HTH,
Nikos
 
Back
Top