function to pad text with 0's

  • Thread starter Thread starter Tom Hanley
  • Start date Start date
T

Tom Hanley

I am not finding a command equivalent to the dbase "pad" function for
justifying a text string to a fixed length and adding 0's or another
character to fill in the empty space. I know I am going to feel stupid when
I'm told but hey...it's Monday morning!
 
Tom,

The following code is so old and clunky that I am a little embarrassed to
post it; however, I have been using it for years to make fixed length
strings for export to fixed-width text files.

If you want to test it out, copy the function below and put it into a public
module.

Public Function PadStr(strField As String, strChar As String, intLenStr As
Integer, intpadTo As Integer, strWhere As String) As String

' strField is the string which is to be padded - may be a field name or an
expression
' strChar is the padding string; i.e., " ", "0", etc.
' intLenStr is the actual trimmed length of the string or expression to be
padded
' intPadTo is the desired string length
' strWhere is "L" for pad on the left, "R" for pad on the right

Dim intPadDiff As Integer ' the difference between string length and
desired length

intPadDiff = intpadTo - intLenStr

' Determine how to pad based on the length of the string passed
If intPadDiff < 0 Then ' String length is greater than desired length
PadStr = Left$(strField, intpadTo)
Else
If strWhere = "L" Then
PadStr = String$(intPadDiff, strChar) & strField
Else
PadStr = strField & String$(intPadDiff, strChar)
End If
End If

End Function

----------------------------------------------------------------------------
---------------
You can call the function by putting the following behind a command button,
as follows:

Dim strPadded As String

strPadded = PadStr(Me!Email, "@", Len(Me!Email), 50, "R")
MsgBox strPadded
 
Back
Top