Results 1 to 10 of 10

Thread: Format Function

  1. #1

    Thread Starter
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76

    Format Function

    I want to use the Format function to do the following:

    VB Code:
    1. sString = "Fred"
    2.  
    3. msgbox format(sString, "??????")

    ...in order that the output is a 6 character string with leading blanks. i.e. " Fred"

    and:

    VB Code:
    1. sString = "Fred Bloggs"
    2.  
    3. 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!

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    First one I can't do with the replace function, but this one works just as well:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim sstring As String
    3.     sstring = "Fred"
    4.  
    5.     Do Until Len(sstring) > 5
    6.         sstring = "?" & sstring
    7.     Loop
    8.    
    9.     MsgBox sstring
    10. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Second one:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim sstring As String
    3.     sstring = "Fred Bloggs"
    4.  
    5.     sstring = CStr(Left(sstring, InStr(sstring, " "))) & _
    6.     CStr(Mid(sstring, InStr(sstring, " ") + 1, 1))
    7.    
    8.     MsgBox sstring
    9. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76
    Thankyou Jimjam!

    ..and thankyou Alex
    My Spidey senses are tingling!

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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:
    1. Private Sub Form_Load()
    2.     Dim x As String: x = "Fred Bloggs"
    3.     MsgBox makeLength(x, 6)
    4. End Sub
    5.  
    6.  
    7. Private Function makeLength(ByVal x As String, ByVal n As Long) As String
    8.     If Len(x) > n Then
    9.         makeLength = Left(x, n)
    10.     Else
    11.         makeLength = Space(n - Len(x)) & x
    12.     End If
    13. 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]

  7. #7

    Thread Starter
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76
    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!

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Shouldn't be too hard!
    I can give you a hand if you want
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9

    Thread Starter
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76
    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!

  10. #10
    Hyperactive Member FATBOYPEE's Avatar
    Join Date
    May 2001
    Location
    Charleville (Ireland) Still. ANYONE GOT A JOB I CAN HAVE ? GIZA JOB. I CAN DO THAT....
    Posts
    463
    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
  •  



Click Here to Expand Forum to Full Width