CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: padding

  1. #1
    Join Date
    Oct 2011
    Posts
    34

    padding

    hi

    need to pad a few fields been pulled from a database

    [code language="VB.NET"]Private Function ReadQueryResults() As Boolean
    On Error GoTo ReadQueryResults_Error

    Dim sOut As String
    Dim j As Integer

    Close #17
    Open OPXCOM$ + "\SNEX.TXT" For Input As #17
    Do While Not (EOF(17))
    For i% = 1 To UBound(QU_ARRAY$)
    Input #17, QU_ARRAY$(i%)
    Next i%

    SNEX_ARRAY$(EXE_DESCRIPTION) = "DESCRIPTION"
    SNEX_ARRAY$(EXE_AMOUNT) = QU_ARRAY$(QU_AMCLSGPRME)


    sOut = ""
    For j = 1 To UBound(SNEX_ARRAY$)
    sOut = sOut & SNEX_ARRAY$(j)
    Next j




    Loop
    Close #17



    ReadQueryResults_Exit:
    Exit Function
    ReadQueryResults_Error:
    ErrorRoutine$ = "ReadQueryResults"
    Gerr Err
    End Function[/code]



    never done this before has anyone any ideas how to do this

    thanks

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: padding

    Well first it looks like you are pulling data from a file rather than a database. Second it also says you are using VB.Net rather than VB6 but the code looks like VB6. Third you did not say if you want to right pad or left pad the data nor what you want to pad with.

    One way to padd a string in VB is to use the String() and Right() or Left() functions.
    For example you can right pad using code something like this.
    Code:
    NewString=Left$(OldString & String$(MaxLen,PadChar),MaxLen)

    btw I would advise using a variable and the FreeFile function rather than a hard coded file number.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Oct 2011
    Posts
    34

    Re: padding

    this works to right pad it
    SNEX_ARRAY$(EXE_DESCRIPTION) = QU_ARRAY$(QU_GLNO)
    SNEX_ARRAY$(EXE_DESCRIPTION) = Space(24 - PadRight(SNEX_ARRAY$(EXE_DESCRIPTION))) & SNEX_ARRAY$(EXE_DESCRIPTION)

    how do i get it to left pad

  4. #4
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: padding

    You just reverse it. The string goes first and then the space. I don't know vbnet so I am unfamiliar with PadRight, but it is not needed. I'll use len for the length of the string.

    SNEX_ARRAY$(EXE_DESCRIPTION) = SNEX_ARRAY$(EXE_DESCRIPTION) & Space(24 - len(SNEX_ARRAY$(EXE_DESCRIPTION)))

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: padding

    Code:
    NewString=Right$(String$(MaxLen,PadChar) & OldString ,MaxLen)
    The advantage to this method is that it allows you to pad with any character rather than being limited to spaces.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: padding

    You can also use Rset() and Lset()

    Code:
    Option Explicit
    
    Private Sub Form_Load()
     Dim str(247) As String * 10
     Dim strOut As String
     Dim x As Integer, dt As Date
     dt = #8:00:00 AM#
     For x = 0 To 47
       str(x) = Space(8)
     Next x
    ' Open "c:\temp\schedule.txt" For Output As #1
        For x = 0 To 47
          If x Mod 2 = 0 Then
            RSet str(x) = Format(dt, "HH:MM AMPM")
          Else
            RSet str(x) = Format(dt, ":n") & "   "
          End If
          strOut = str(x) ' & vbCrLf
    '      Print #1, strOut
        Debug.Print strOut
          dt = DateAdd("n", 30, dt)
        Next x
    '  Close #1
      Unload Me
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured