Link to home
Start Free TrialLog in
Avatar of sujay_sil_cal
sujay_sil_cal

asked on

formated output to a text file

i am using visual basic for my application development.
i want to write reports through visual basic codding. how i can write the report output in a text formatted file with row/column specification as we can do in visual foxpro. and how i can redirect that file to the printer for printing.
Avatar of RodStephens
RodStephens

You can use Print to write into a file. Use FreeFile and Open to open the file for output. Use Print to write into it and use Close to close it when you are finished.

You can use Format$ to make sure every entry in a column has the same width. For example:

Right justify:
    Print #file_num, Format$(value, "@@@@@@@@@@")

Left justify:
    Print #file_num, Format$(value, "!@@@@@@@@@@")

Now you can print this using a monospaced font such as Courier New.

For a nicer looking report with aligned columns and a variable width font such as Times New Roman, you can print directly to the Printer object. Or use the DataReport object or some other tool like Crystal Reports.
Dear  sujay_sil_cal,
here is the simple example for u.
This code fetches data from a table and output to a text file.

Dim finalData As String
'This line is create a empty file for output
    Open "c:\myfolder\DestinationFile.txt" For Output As #1
    Close #1
Open "c:\myfolder\DestinationFile.txt" For Append As #1

Set con = CreateObject("adodb.connection")
con.Open "mydata"   'Your DSN Name
Set rs = CreateObject("adodb.recordset")
'Here addtest is Table Name
rs.Open "select * from Tablename", con
While Not rs.EOF
'Fields are stored in array
finalData = rs(0) & rs(1) & rs(2) & rs(3) & rs(4)
'Print finalData
Print #1, finalData
rs.MoveNext
Wend


I think this will help u
Sridhar

Use FileSystemObject with udt to format your file.

Public Type udtMyType ' Definition of a Public UDT
   birthDate As Date * 10
   lastName As String * 20
   firstName As String * 20
   address As String * 50
End Type

'-------------------------------------------------
Sub OpenTextFileTest
  Const ForReading = 1, ForWriting = 2, ForAppending = 8
  Dim fso, f
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
  f.Write udtMyType.birthdate & vbtab & _
          udtMyType.lastName & vbtab & _
          udtMyType.firstName & vbtab & _
          udtMyType.address
  f.Close
End Sub

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

for the linking to the printer, I'm not sure how u want to print the document u created. Do u want to print it within the text editor or straight away u want to print it once u have formatted the data? if it's the latter, then just apply the method above by those experts.

good luck.
ASKER CERTIFIED SOLUTION
Avatar of pramodkumarsingh
pramodkumarsingh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
Hi sujay_sil_cal,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days.  I will suggest to:

    Accept RodStephens's comment(s) as an answer.

sujay_sil_cal, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will followup.

EXPERTS: Please post closing recommendations.
==========
DanRollins -- EE database cleanup volunteer
I think pramodkumarsingh & I provided the solution as well.
if possible, shall share the points among 3 of us.

pramodkumarsingh , ianouii & RodStephens.

thanks.
Suggested disposition:

    split points to pramodkumarsingh and ianouii and RodStephens

DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator

ianouii: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20322465
RodStephens: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20322464