Problem outputting to text box using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billelev
    New Member
    • Nov 2006
    • 119

    #1

    Problem outputting to text box using VBA

    This is probably a very easy question to answer:

    I have been outputting some text to a message box, similar to the following:

    Code:
    strOutput = "---" & Chr(10) & Chr(10)
    strOutput = strOutput & "VAR:" & AddTabs(1) & Format(rsVar![VAR], "currency")
    strOutput = strOutput & Chr(10) & Chr(10)
    strOutput = strOutput & "Position Value:" & AddTabs(1) & Format(rsVar![ValuePosition], "currency") & Chr(10)
    strOutput = strOutput & "Position Risk:" & AddTabs(1) & Format(rsVar![valueRisk], "currency") & Chr(10)
    msgbox(strOutput)
    Which works fine. I tried outputting the same string (strOutput) in a text box on a form, i.e.:
    Code:
    txtBox.caption  = strOutput
    The problem is that rather than creating new lines using chr(10), small boxes appear.

    Does anyone know an equivalent to chr(10) that will work when sending a string to a text box?
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. You are not actually outputting the string to the textbox, but to its associated label (by use of the caption property). As far as I know labels are not intended to display multi-line strings, hence your problem with the line feed.

    You should find that the actual text box displays multi-line strings correctly. To set the textbox itself to the string, simply use

    txtBox = strOutput

    You will need to resize the textbox vertically to see the second and subsequent lines correctly.

    -Stewart

    ps there is a vb constant vbCrLf which you can use in place of your Chr(10) to insert a complete carriage-return and line-feed combination into the string. You may find that this works reliably if the Chr(10) does not.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      ** NEWS **

      Stewart leaves me nothing to say - AGAIN!

      /** NEWS **

      I would only stress further that using vbCRLF (and/or associated vbCR and vbLF constants) is a recommended way to refer to these characters. It makes the code more easily readable if nothing else.

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        Actually Captions on labels CAN be multi lined.

        As the previous posts say use the vbCrLF instead of chr(10) and then you MUST set the height of your label to accomodate all the lines. If your height is not set to accomodate all the lines it will just show 1 line or as many as it can show in the label.

        I create multi line labels all the time. I also ran your code and substituted vbCRLF and it works for me.

        cheers,

        Originally posted by billelev
        This is probably a very easy question to answer:

        I have been outputting some text to a message box, similar to the following:

        Code:
        strOutput = "---" & Chr(10) & Chr(10)
        strOutput = strOutput & "VAR:" & AddTabs(1) & Format(rsVar![VAR], "currency")
        strOutput = strOutput & Chr(10) & Chr(10)
        strOutput = strOutput & "Position Value:" & AddTabs(1) & Format(rsVar![ValuePosition], "currency") & Chr(10)
        strOutput = strOutput & "Position Risk:" & AddTabs(1) & Format(rsVar![valueRisk], "currency") & Chr(10)
        msgbox(strOutput)
        Which works fine. I tried outputting the same string (strOutput) in a text box on a form, i.e.:
        Code:
        txtBox.caption  = strOutput
        The problem is that rather than creating new lines using chr(10), small boxes appear.

        Does anyone know an equivalent to chr(10) that will work when sending a string to a text box?

        Comment

        • billelev
          New Member
          • Nov 2006
          • 119

          #5
          Thanks for all your comments.

          I have replaced Chr(10) with vbCrLf with the desired results.

          However...vbTab is not producing tabs, but those small boxes again...?

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            I don't think tabbing is supported in a label caption. You would just need to add spaces.

            cheers,

            Originally posted by billelev
            Thanks for all your comments.

            I have replaced Chr(10) with vbCrLf with the desired results.

            However...vbTab is not producing tabs, but those small boxes again...?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32669

              #7
              Tabs in general (and therefore vbTab in this situation) only has meaning within a defined context. What would you be expecting of a tab within a label?

              The difference between Chr(10) and vbCRLF is not simply that one is a constant. Chr(10) is actually equivalent to vbLF rather than vbCRLF (It is a Line-Feed rather than a Character Return / Line Feed combination).

              I suspect that whichever control you're using this in (TextBox or Label) has no context defined for Tab characters therefore it won't match what you expect.

              Comment

              • billelev
                New Member
                • Nov 2006
                • 119

                #8
                Originally posted by NeoPa
                Tabs in general (and therefore vbTab in this situation) only has meaning within a defined context. What would you be expecting of a tab within a label?

                The difference between Chr(10) and vbCRLF is not simply that one is a constant. Chr(10) is actually equivalent to vbLF rather than vbCRLF (It is a Line-Feed rather than a Character Return / Line Feed combination).

                I suspect that whichever control you're using this in (TextBox or Label) has no context defined for Tab characters therefore it won't match what you expect.
                I'm trying to display a table of data, aligning the columns using tabs. I'm also outputting the same text to an email, which accepts the tab character. The table is roughly 6 columns by 20 rows.

                Comment

                • Stewart Ross
                  Recognized Expert Moderator Specialist
                  • Feb 2008
                  • 2545

                  #9
                  As NeoPa says, a tab has no meaning without a context. Thirty years ago typewriter teachers used to teach typing students about the use of tabs, and how important they were in lining up numbers within the manually-spaced lines of (monospaced) Courier or similar text.

                  Tabs are more or less obsolete in powerful word processors such as Word, where the line-oriented left, right, centre and decimal tabs have generally been replaced by the use of tables, where the columns themselves can align text left, centre and right. Even so, Word still has default tabs set every 1/2 in as standard. Whay am I mentioning this? Because if you set up a document with 1/2" tabs, use these to space the text, then someone changes it to 1", say, the document suddenly becomes unreadable.

                  It is because a tab is effectively an instruction to move the current text location to a defined point that it has no meaning unless the context is defined.

                  ...And all of this is a long-winded way of asking you, as NeoPa has also done - in what context is it meaningful to include tabs in a text control which has no default means of handling the spacing instruction you have included?

                  -Stewart (the verbose...)

                  Comment

                  • LBryant
                    New Member
                    • Mar 2008
                    • 18

                    #10
                    You can use a List Box control for displaying information in columns, yes?

                    Comment

                    • Stewart Ross
                      Recognized Expert Moderator Specialist
                      • Feb 2008
                      • 2545

                      #11
                      (Sigh) It takes longer to write a reply than it does for the thread context to change, hence I sent then deleted a message which was out of context of the changed thread.

                      The last poster makes the point that a listbox control can be used to output columns. It can, but with limitations - there is not much in the way of alignment that can be done, for instance. And it does not answer what you need for output via e-mail. I would suggest using a text export which is tab-delimited or using CSV format to output your columns, rather than trying to do this in the message itself. This would be an attachment task, but at least has the advantage that tabs delineate columns, whereas otherwise the interpretation of tabs depends entirely on the application.

                      In my experience e-mail applications do not do a good job of tabbing, as they impose defaults in the same way as word processors do - arbitrarily. Some columns line up, some don't as they miss the tab point concerned. Some spill into the next column, and so on.

                      -Stewart

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32669

                        #12
                        You could try using spaces to format your output. Many text processors use this method even now, especially when no default context is defined. Alternatively, using the same approach, you could use a character other than the space for tabulation formatting (characters frequently used for this include . - _ = * + etc).

                        PS. Thanks Stewart for that clarification. You translated my message perfectly :)

                        Comment

                        • billelev
                          New Member
                          • Nov 2006
                          • 119

                          #13
                          Thanks for the clarification.

                          The original context was within an email, wherein a tab character moved in multiples of six character spaces, to the nearest multiple. This made the formatting of the table using tabs a reasonably trivial task. I'll probably try using spaces to format the text within the text box.

                          Comment

                          Working...