Results 1 to 8 of 8
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    How do I pad spaces to the right of a field?
    A2k (9.0.3821) SR-1

    I have 3 text boxes on a form. Text1 & Text2 are unboud Text3 is bound.

    Text1 and Text2 can have up to 10 characters each.

    On after update on text2, I want set Text3 equal to Text1 and Text2, I need X number of spaces between Text 1 and Text2 so Text2 start in position 11 of Text3.

    Text1 Text2
    Abcde 12345

    Text3
    Abcde 12345 (in this case there is 5 space between abcde and 12345)

    I need something like Text3 = Text1 & " " & Text2 with correct syntax to pad X number of spaces

    Any help would be appreciated.

    Thanks, John

  2. #2
    Star Lounger
    Join Date
    Oct 2001
    Location
    Western Massachusetts, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    I'm not entirely sure how you do this with Text3 being a bound field but if you call this Function from a query, it will return the string you need. Then you might be able to use it on your form.

    Paul

    Function padout(text1 As String, text2 As String)
    Dim x As Integer
    Do Until x = 10
    text1 = text1 & Chr(32)
    x = Len(text1)
    Loop
    padout = text1 & text2
    End Function

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    Hi Paul

    The code works fine

    How would I modify it to also pass the pad character?

    Thanks. John


    ' this code take the two text fields and pads them with the number of spaces in count

    Function padout(Text1 As String, Text2 As String, Count As Integer)

    Dim X As Integer
    Do Until X = Count
    Text1 = Text1 & Chr(32)
    X = Len(Text1)
    Loop
    padout = Text1 & Text2

    End Function

  4. #4
    Star Lounger
    Join Date
    Oct 2001
    Location
    Western Massachusetts, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    John, here's some modified code.
    Function padout(Text1 As String, Text2 As String, Count As Integer, myChr As Variant)

    Dim X As Integer
    If Len(Text1) > Count Then
    padout = Text1 & myChr & myChr & Text2
    Else
    Do Until X = Count
    Text1 = Text1 & myChr
    X = Len(Text1)
    Loop
    padout = Text1 & Text2
    End If
    End Function

    In the query your arguments would look like this

    MyFieldadout(Text1,Text2,10, Chr(42))

    Chr(42) would return ********

    I've added the If.....Then statement because the way it was set up, if Len(Text1) was greater than Count, it put you in an overflow error. This way, it just pads with two of myChr.

    Paul

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    I would use:

    <pre> Left(Text1 & Space(10),10) & Text2
    </pre>

    Legare Coleman

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    Hi Legare

    Perfect!

    That is whats interesting about VBA, many ways to do the same thing, being new to VBA I learned alot from Pauls solution.

    Thanks

    John

  7. #7
    Star Lounger
    Join Date
    Oct 2001
    Location
    Western Massachusetts, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    I'll confess. I had no idea a Space() function existed. Very nice.

    Paul

  8. #8
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I pad spaces to the right of a field? (A2k (9.0.3821) SR-1)

    Well " " would have worked just as well (ten spaces enclosed in double quotes. That is also what you have to use if you want some character other than a space.
    Legare Coleman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •