-
Easy question, i`m sure, but its not obvious from the `Format` command help.
I have a simple string, which contains, 10 chars. But i want to write it to a file, padded out (with spaces) to 20 chars.
I`m after something like:
Line = Line & Format(!FieldFromDb, 20)
Any ideas?
Thanks,
Alex.
-
there's a Space$ function in vb that will fill a string with spaces Space$(5) returns a string of 5 spaces
so you could try something like
line=line & Space$(5) & fieldfromDb & space$(5)
-
weeeeellll... the len of the string is variable, so i`d need to subtract the len of my string from the total, and use that as the parameter to space. I was rather hoping there`d be a function that`d do it for me...
perhaps there isnt though!
a.
-
Pallex, there is...
This is a little trick & learned from my Access programming days that works in VB too:
Code:
Line = Line & Left(!FieldFromDB & " ", 20)
Regardless of the length of your field, it will be right-padded so that the total string has a length of 20. Remember, if you use some number other than 20 you have to change the number of spaces to match that number or you'll get weird results. For example:
Code:
Line = Line & Left(!FieldFromDB & " ", 5)
All the best.
-
Hi.
If you want to add spaces after the string but the spaces and the string must be 20, do this:
first declare a variable type string like this;
Dim sString as String *20
This reserve 20 spaces in memory.
Then write it into the file, I think that you're using the Open file function, isn't? Ok, then open a file to write with this:
Open (path-file) for Output as #(filenumber)
Print #(filenumber), sString
Close #(filenumber)
Where (path-file) is the path and name of your file, (filenumber) is the number type integer that you can get it with Freefile function or write it.
This write into the file the string and the spaces but always going to be 20, it's useful if you want to do a table in a text file.
I hope that this can help you.
Best Regards to everyone and have a nice weekend!!
-
Also you can try:
Line = Line & Space$(20 - Len(Line))
This adds enough spaces to reach 20 characters. Be sure, thou, that there are no more than 20 characters in your original Line!