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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get absolute field lengths 3

Status
Not open for further replies.

willybgw

Programmer
May 30, 2003
55
US
I need to concatenate several fields (all text) with a total length of 94 bytes to a single field of 94 bytes.

The output field needs to be spaced correctly, for example if the first of the concatenated fields is 20 bytes but only has 10 characters I want the second field to start in position 21 of the output field. Some fields work as expected others don't. Even if the field is blank I need it to put the defined number of blanks in the output field.

Any help will be greatly appreciated.

willybgw
 
try left(field1 & spc(20),20)
this is good for trailing spaces
right (spc(20)& field1 ,20)
for leading spaces
 
Hi,

Is the question, how can you programatically determine how long a text field definition is,

or

knowing the field definition length, how to format a variable to that length?

I am guessing the latter. My example has 2 fields, one or 10 and the other if 5 bytes.
Code:
[Field1]&Left("          ", 10-Len(Field1))&[Field2]&Left("          ", 5-Len(Field2))


Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
Space

sorry it should be space(20)

try left(field1 & space(20),20)
this is good for trailing spaces
right (space(20)& field1 ,20)
for leading spaces
 
Thanks for the responce!

lupins46: To answer your question "both" for example if the field is 11 bytes and there is a character in the 6th position I need 5 blanks before and 5 after.

SkipVought: Field length as defined in the table design "field size".

Sorry I took so long to respond, I was in a meeting.

willybgw
 
What?
How is anyone supposed to know what position the text is in? If there is only one character present then your options are first ar last.
 
A similar question has been asked in vb6 forum thread222-1037387 may be useful to keep info from there also.

________________________________________
Zameer Abdulla
Visit Me
Hold your child's hand every chance you get.
A time will come when he or she won't let you.
 
willy, willy, willy

Are you sure that you're not trying to kluge a CENTER FORMAT into your field spec? Ya gotta separate DATA STORAGE and REPORT FORMATTING -- two different animals.

You STORE your one character field of data in a one charter length and then FORMAT it to center under the Field Name so's it ken be pretty.

Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
Or something like:
Code:
strOutput = spc(94)  ' Full string
Mid(strOutput(1, 20) = [myfirstfield]   ' field 1
Mid(strOutput(21, 10) = [mysecondfield]  ' field 2
etc. etc.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
traingamer:
yor example is good for reading the fields not joining the fields

SkipVought:
CENTER FORMAT cna be done something like this
left(space(10-(int(len(field1)/2)) & field1 & space(10-(int(len(field1)/2))& space(1),20)
i know that i still have not coverd all senairoes
 
Ok my main programming experence is on "less" sophisticated AS/400 computers where a field is a field and whatever is in that field is what is there, blank or otherwise.

What I am trying to accomplish is to build a file for payroll direct deposit. There is no leeway in the format the bank will accept. The file contains 5 different record types and the field sizes are not of my choice. Some of the data in these fields are not the same length, such as employee bank account number, name, etc. Also because of the 5 different records there is no way to export a fixed width file.

Skippy:This has noting to do with report formatting.

willybgw
 
from my experience with these types of files
string are left aligned and you should use
numbers are right aligned and the decmail counts as a character and should be paded with zeros
you should use
left(stringfield1 & space(20),20)& left(stringfield1 & space(20),20)& right (String(10,cstr(0)) & field1 ,10)....
 
from my experience with these types of files
string are left aligned and you should use
numbers are right aligned and the decmail counts as a character and should be paded with zeros
you should use
left(stringfield1 & space(20),20)& left(stringfield2 & space(20),20)& right (String(10,cstr(0)) & numberfield1 ,10)....
 
willybgw,
It sounds like you *may* be looking to create a fixed-width record (like you would use in a random-access file).

To do that, you can create a user-defined data type:

Type myRec
strFName As String * 15
strMI As String * 1
strLName As String * 20
strAmount As String * 10
strDate As String * 10
End Type

And an output record type:

Type OutRec
strOut As String * 56
End Type

BOTH OF THESE TYPES NEED TO BE DECLARED IN THE "PUBLIC" PART OF THE MODULE--NOT IN A SUB OR FUNCTION.

Then you can use some code like this to populate the fixed-length fields, and then force the record into a string for printing, or whatever:

Private Sub testtype()
Dim emp As myRec
Dim strOutput As OutRec
emp.strFName = "George"
emp.strMI = "W"
emp.strLName = "Bush"
RSet emp.strAmount = Format(127.58, "000000.00")
emp.strDate = "2005/03/05"
LSet strOutput = emp
Debug.Print strOutput.strOut
End Sub

Note the "LSet" and "RSet". These commands can be used to "Justify" data in a receiving field (left or right), or they can be used to move data from a variable of one type to a variable of another type.
The RSet above right-justifies the numeric string in the receiving fixed-length string.
The LSet above moves the data from the field emp, which is of a Type of myRec, to strOutput, which is of the Type OutRec.

This all probably seems far too complicated, but it is a way to accomplish the fixed-width records you are needing.

Good Luck,
Tranman
 
Tranman:
interstring way of of creating a fixed lenght record but would return the same result without the rset and if you have to pad the amount with zero it should be Format(127.58, "0000000.00")


 
pwise,
No, not the same result without the rset--

With, field = _000000127.58
Without, field = 000000127.58_

(The underscore represents a blank.)

I was trying to illustrate the use of the command.

I purposely did not pad the whole field with 0's so that the result of the RSet could be seen.

Should probably have said RSet emp.strAmount = Format(127.58, "0.00"), so you could *REALLY* see the difference.

Tranman
 
Tranman:

you are right but what i ment is that since you whould have to pad it with zeros might as well do Format(127.58, "0000000.00")
 
Tranman:

anyway a star for teaching me a new method
 
yor example is good for reading the fields not joining the fields
Oh, really? You might have to pad the numeric fields, but it'll work just fine.

I like Tranman's answer, too. [smile]

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top