|
-
Nov 5th, 2002, 07:35 AM
#1
Thread Starter
Lively Member
Format Function
I want to use the Format function to do the following:
VB Code:
sString = "Fred"
msgbox format(sString, "??????")
...in order that the output is a 6 character string with leading blanks. i.e. " Fred"
and:
VB Code:
sString = "Fred Bloggs"
msgbox format(sString, "??????")
...in order the the string is truncated. i.e. "Fred B"
is this possible using the Format function (I already know how to do this in other ways)?
Thanks in advance 
Flustor
My Spidey senses are tingling!
-
Nov 5th, 2002, 07:41 AM
#2
Retired VBF Adm1nistrator
Off-hand I think the easiest is to use the normal left$, right$ or mid$ functions.
It would be very easy with those.
I'm trying to figure out Format() for you though
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2002, 08:01 AM
#3
First one I can't do with the replace function, but this one works just as well:
VB Code:
Private Sub Form_Load()
Dim sstring As String
sstring = "Fred"
Do Until Len(sstring) > 5
sstring = "?" & sstring
Loop
MsgBox sstring
End Sub
-
Nov 5th, 2002, 08:05 AM
#4
Second one:
VB Code:
Private Sub Form_Load()
Dim sstring As String
sstring = "Fred Bloggs"
sstring = CStr(Left(sstring, InStr(sstring, " "))) & _
CStr(Mid(sstring, InStr(sstring, " ") + 1, 1))
MsgBox sstring
End Sub
-
Nov 5th, 2002, 08:12 AM
#5
Thread Starter
Lively Member
Thankyou Jimjam! 
..and thankyou Alex
My Spidey senses are tingling!
-
Nov 5th, 2002, 08:15 AM
#6
Retired VBF Adm1nistrator
MSDN mentions that you can use Format() to format your strings with string expressions... but doesn't give any examples 
Best I can come up with so far I'm afraid 
VB Code:
Private Sub Form_Load()
Dim x As String: x = "Fred Bloggs"
MsgBox makeLength(x, 6)
End Sub
Private Function makeLength(ByVal x As String, ByVal n As Long) As String
If Len(x) > n Then
makeLength = Left(x, n)
Else
makeLength = Space(n - Len(x)) & x
End If
End Function
btw lucy, care to donate your cpu ident ?
http://www.vbforums.com/showthread.p...hreadid=211141
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2002, 08:36 AM
#7
Thread Starter
Lively Member
Thanks Jimjam! V kind! 
I was hoping I could use a config file for the formatting of numbers & text, like:
ConfigFile:
Field1=00000
Field2=000
Field3=??????
where Fields1&2 were numeric and field3, ASCII. This way I could read in data using the same command for formatting.
i.e.
sCurrentStringType="Field1"
sCurrentStringData=221
sCurrentStyle=00000
sResult = Format(sCurrentStringData, sCurrentStyle)
Looks like I'll have to use more than the Format command *sigh* nevermind.. it's not too much of a hassle 
Thankyou lovely peeps!!
My Spidey senses are tingling!
-
Nov 5th, 2002, 08:40 AM
#8
Retired VBF Adm1nistrator
Shouldn't be too hard!
I can give you a hand if you want
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Nov 5th, 2002, 08:45 AM
#9
Thread Starter
Lively Member
I don't get offers like that everyday! No, thanks anyhoo 
I'm quite happy writing it, just looking for shorter way of getting the job done
My Spidey senses are tingling!
-
Nov 5th, 2002, 09:11 AM
#10
Hyperactive Member
Maybe out of Order (and not read the Q properly) but hope this helps:
Code:
Public Function Padtrim(sval As String, Optional replacechar As String)
If Len(sval) < 6 Then
sval = Space(6 - Len(sval)) & sval
Else
sval = Left(sval, 6)
End If
If replacechar <> "" Then
sval = Space(Len(sval))
sval = Replace(sval, " ", replacechar, 1, Len(sval))
End If
Padtrim = sval
Peeman
Last edited by FATBOYPEE; Nov 5th, 2002 at 09:57 AM.

Best Bar.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|