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

1. ## 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. ## 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
End Function

3. ## 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

End Function

4. ## 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
End If
End Function

In the query your arguments would look like this

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. ## 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>

6. ## 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. ## 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. ## 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.

#### Posting Permissions

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