473,378 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

data not fixed length vba programming on text file

232 100+
i want to print report through vba programming on text file but the problem i find that the data is not of fixed length on report according to data structure length. i want that for detail field of data type text and field size 5 will always occupy space of 5 characters even if it is blank or of 2 character.if i export it in dbf format its possible through foxpro but not in vba.
kindly help to print report through vba
thanx in advance
Aug 8 '12 #1
11 3543
twinnyfo
3,653 Expert Mod 2GB
kkshansid,

Could you give us an example of what you are looking for? For example, do you want leading or trailing characters displayed to fill up extra empty spaces? Please explain and show us what you have tried so far....
Aug 8 '12 #2
kkshansid
232 100+
Expand|Select|Wrap|Line Numbers
  1. Sub WriteToATextFile()
  2. 'first set a string which contains the path to the file you want to create.
  3. 'this example creates one and stores it in the root directory
  4. MyFile = "c:\" & "EXPENDITURE.txt"
  5. 'set and open file for output
  6. fnum = FreeFile()
  7. Open MyFile For Output As fnum
  8.  
  9. Dim r1 As ADODB.Recordset
  10. Set r1 = New ADODB.Recordset
  11. r1.ActiveConnection = CurrentProject.Connection
  12. r1.CursorType = adOpenDynamic
  13. r1.LockType = adLockOptimistic
  14. r1.Open ("SELECT * from  FIN_MAY12")
  15. If Not (r1.EOF And r1.BOF) Then
  16.     Do While Not r1.EOF
  17.  
  18.          Print #fnum, r1("chk_Date") & " |" & r1("chk_no") & " |" & r1("particulars") & " |" & r1("debit") & " |" & Space(5) & r1("credit") & " |" & Space(5)
  19.          Print #fnum, String(50, "-")
  20.          Chr (12)
  21.          r1.MoveNext
  22.     Loop
  23. End If
  24. Close #fnum
  25. End Sub
i want to fill up empty spaces with empty spaces so that formatting will not get disturbed.
Aug 9 '12 #3
zmbd
5,501 Expert Mod 4TB
Are you tring to do this:
http://office.microsoft.com/en-us/ac...010006905.aspx
-z
Aug 9 '12 #4
zmbd
5,501 Expert Mod 4TB
Ahh.. I see you posted just before I hit enter...
try the following:
http://msdn.microsoft.com/en-us/libr.../ff835958.aspx
Use the transfer type as "acExportFixed"

Ignore the code offered at the bottom... don't know why they'd offer a mail-merge to word automation code... but it is nice to review... could be useful for some other project!

Please let us know if this works...

-z

[EDIT = 2012-08-08-2335CST]
You'll more than likely need the Schema.ini file...
This link has the format example... scroll down a tad
http://support.microsoft.com/kb/241477
You'll need to place it in the SAME folder as you will be exporting the text file to... (ie: if you're exporting to: "%userprofile%\textout\" then the schema.ini will need to be there and you will need to specify the file-name in the code as indicated.)

-z
Aug 9 '12 #5
kkshansid
232 100+
i want to implement it in similar manner as in foxpro same file when exporting to dbf becomes character data type and and prints very well in foxpro by same method without any disturbance for example cheque no will always occupy 10 characters event if its empty data but i cant do it in vba programming.vba provide space not according to data structure but according to data
Aug 9 '12 #6
zmbd
5,501 Expert Mod 4TB
Another solution is to read the table field properties and then append spaces.

I have a simple table "tbl_customproperty"
and a few fields in it

Expand|Select|Wrap|Line Numbers
  1. Sub Listtblformat()
  2. Dim zdbs As DAO.Database, ztbldef As DAO.TableDef, zfld As DAO.Field
  3. '
  4. 'setup for the database and look at just one table
  5. Set zdbs = CurrentDb
  6. Set ztbldef = zdbs.TableDefs("tbl_customproperty")
  7. '
  8. 'get the information for the table fields
  9. 'and print it to the debug window
  10. For Each zfld In ztbldef.Fields
  11.     Debug.Print "Field name ::: Field Size ::: Field Type"
  12.     Debug.Print zfld.Name & " ::: " & zfld.Size & " ::: " & zfld.Type
  13.     Debug.Print "field attributes::: " & zfld.Attributes
  14.     Debug.Print "-----"
  15. Next
  16. '
  17. 'clean up
  18. Set zdbs = Nothing
  19. Set ztbldef = Nothing
  20. '
  21. End Sub
  22. '
  23. 'In the immediate window the following will print:
  24. '
  25. 'Field name ::: Field Size ::: Field Type
  26. 'CustomPropertyDB ::: 50 ::: 10
  27. 'field attributes::: 2
  28. '-----
  29. 'Field name ::: Field Size ::: Field Type
  30. 'CustomPropertyName ::: 50 ::: 10
  31. 'field attributes::: 2
  32. '-----
  33. 'Field name ::: Field Size ::: Field Type
  34. 'CustomPropertyType ::: 50 ::: 10
  35. 'field attributes::: 2
  36. '-----
  37. 'Field name ::: Field Size ::: Field Type
  38. 'CustomPropertyValue ::: 50 ::: 10
  39. 'field attributes::: 2
  40. '-----
they're text fields and they are set for 50 characters long.... you should now be able to read this information... store it (array, enumeration, custom class whatever)... read your data for the fields in question... compare the length and either cut it down or append/prepend the correct number of spaces to make the length...

I'll leave that coding for you to do.

-z
Aug 9 '12 #7
kkshansid
232 100+
if its that much difficult in vba and require so many lines of code then i will prefer to print it through foxpro
Aug 9 '12 #8
zmbd
5,501 Expert Mod 4TB
there really isn't that many lines of code there... lines 23 thru 40 were just the output to the debugger window... there's less actual code in #7 (like 6 lines) that do the work then you posted in #3.

FoxPro... which version? Remember the older databases used fixed field files and that may be why you finding this easier in FP...

-z
Aug 9 '12 #9
kkshansid
232 100+
ITS FOXPRO 2.6 . I dont need to export once but to make text file and write on this text file in desired format through vba program with every field length according to data structure.if i do every thing in access by sql query then i just also want to print it also through vba should i always use foxpro for printing purpose?
Aug 9 '12 #10
twinnyfo
3,653 Expert Mod 2GB
kkshansid,

I think you also want to make sure that if a field has fewer than the maximum number of characters that it fills the field correctly for alligning the columns, yes?

Wouldn't you need to find the length of each individual field and add spaces as necessary for that? It sounds a bit involved just to be able to export to a text file. However, if you use those text files often, then it is worth the effort (and the extra lines of code that may be required).

Another challenge of communicating between applications...
Aug 9 '12 #11
zmbd
5,501 Expert Mod 4TB
Twinny & kkshansid:

- The code I posted in #7 reads the table field widths.
- Also in #7 I suggested the pre/post-append
- Code in #7 can also be used to create the Schema.ini file for used with the method in suggested in #5 by outputting to a text file the information as given in the second link as posted in #5.
- FoxPro can basically ODBC into the Access DB depending on version... which from kkshansid, is able to do.
- At this point kkshansid needs to decide what is the best way for his application to work. Several solutions have been offered. Unfourtunately I don't have the luxury/time to write the application for kkshansid as I do this inbetween my lab tests and other coding responibilities.

-z
Aug 9 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ken Hall | last post by:
I have the following code to read the following file (very simple). I'm trying to extract only the date, but with this code I'm extracting the entire record. What am I doing wrong? What am I...
5
by: Neil Robbins | last post by:
I have a text file that stores a number of records that I need to access in a vb.net application. Each of the fields that make up a record are of a fixed number of bytes. So for instance there is...
14
by: Manish | last post by:
The project I am developing doesn't involves database. I want to parse the mailbox file (.mbx) and store the summary in the text file for fast retrieval and display of information in the Inbox...
5
by: RadhakrishnanR | last post by:
Hi, By using VB6.0, I want to export database table data into (i.e based on the selected file type(xls or txt)) excel file or text file with a tab delimited text file. My User interface has: ...
1
by: bvdrsganesh1981 | last post by:
Hello !!! Can u please suggest me , the way to Export the data from all the tables of the Data Base at a time in text files individually with file names similar to the table names . ...
0
by: nanaalwi | last post by:
hi there, im doing a project right now to download a data from EEPROM of a camcorder. i used a 'data grid view' to display all the data downloaded from the camcorder. however, i have to save the...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
0
by: grant | last post by:
I have a transfer data task that gets some data from a table and outputs it to a pipe delimitd text file. How can I configure a transfer data task so it outputs the field names from the table in...
1
by: silpa | last post by:
I have a comma delimited text file which has data like this. 1, 11, "abc" ,False 2, 12, "def" ,False 3, 13, "ghi" ,False 4, 14, "jkl" ,False The name of this file is somedata.txt. Only one...
0
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.