Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed-Width File

Status
Not open for further replies.

vbnetnewby1967

Technical User
Aug 16, 2005
7
US
Can someone please show me how to create a text file where each element that is written to the file is a differnt width.
Example:

ABxxxxLAST_NAMExxxxxxxxxxxFIRST_NAMExxxxx
Were x = spaces.
Any assistance would be appreciated.
 
Use random access files. Each element of the record is designated a length if it's a string. Here's a quick sample:

Code:
Option Explicit
'declare a type to act as record
Private Type myRecordType
   lngID As Long
   strInitials As String * 2
   strLast As String * 25
   strFirst As String * 20
End Type

Private Sub Form_Load()
   'declare variables
   Dim x As Integer              'counter
   Dim myRecord As myRecordType  'record
   
   'open the file
   Open "c:\testfile.txt" For Random As #1
   'write a few dummy records
   For x = 1 To 10
      'grab and make a record
      With myRecord
         .lngID = x
         .strFirst = "FirstName_" & x
         .strLast = "LastName_" & x
         .strInitials = CStr(x)
      End With
      'write the record in place x in the file
      Put 1, x, myRecord
   Next x
   
   'get the file 5th record
   Get 1, 5, myRecord
   MsgBox myRecord.lngID & ", " & myRecord.strFirst
   
   'close the file
   Close #1
End Sub

-Max
 
Hi and welcome to Tek-Tips. To get the best from these forums please read faq222-2244 carefully.

I notice your handle refers to VB.NET so if this is a VB.NET question you may get a more appropriate answer in forum796 (this is the VB5/6 forum)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Sorry Max, that sounded as if I was ignoring your perfectly good answer for VB6 - my apologies [smile]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top