• Form Equivalent of TextWidth (97 SR1)

    Author
    Topic
    #380836

    I need a function similar to the TextWidth method of report objects but which will work on a form. I need to break text up into lines for a form display similar to what we already do on a report. Unfortunately, forms don’t have a TextWidth method, at least no in A97.

    Viewing 1 reply thread
    Author
    Replies
    • #639457

      Charlotte, the attached module contains a function GetTextWidth. This function returns the text width (in twips) of the text in the active control. You can use it (for instance) in the OnChange event of a text box.

      The code is derived from the code provided in MSKB article ACC: How to Limit Number of Characters to Size of Text Box.

      • #639505

        Thanks, Hans. I had already looked at that code in the MSKB before I posted. The problem with it is that it assumes the control will have the focus and gets the hWnd for that control. What I’m doing is filling in a *series* of unbound form controls with text that may be chunks of a string too big to fit into a single control from a single record or may be a series of strings from sequential records. For sufficient reasons the controls are not necessarily the same width. I adapted a method from the Access Developers Handbook for A97 used for sizing controls to fit their contents. I just used the relevant portion to determine the length of the text in twips based on the font size and compared it to the width of the control in twips. And of course it has to be parsed by word so that no word gets broken at the end of the available space.

        • #639510

          Charlotte,

          The MSKB code needs the control to have focus at two places:

          1. To get the current text in the control. Since you are breaking up an existent string, you don’t need that; you can pass strings as a parameter instead.
          2. To get the window handle of the control. To get around this, you could fill an array with the hWnd’s of the controls in OnLoad of the form, and pass the appropriate hWnd as a parameter.

          The function could look like this:

          Public Function GetTextWidth(strVal As String, hWnd As Long)
          ‘ Get text width of string in control in twips
          Dim hDC As Long
          Dim lpSize As Size

          ‘ Get device context of control
          hDC = GetDC(hWnd)

          ‘ Compute the width of the text
          GetTextExtentPoint hDC, strVal, Len(strVal), lpSize
          ReleaseDC hWnd, hDC
          ConvertPixelsToTwips lpSize.cx, lpSize.cy
          GetTextWidth = lpSize.cx
          End Function

          • #639725

            True, and thanks for the ideas, but I already solved the problem with another method.

            • #639727

              Naughty naughty Charlotte, you are breaking one of your own rules. Would you like to inform us of your method please. evilgrin
              Pat smile

            • #640000

              The code’s at work and I’m at home when I lounge around here. I’ll have to send it to myself from work before I can strip it down and post it. hairout Besides, you can always use Hans’ code. It works too. grin

            • #640646

              Here’s the code I used. It definitely works with Access 97 but I would be cautious about recommending it for later versions due to the highly explicit declarations from the Msaccess executable itself. The advantage to it is that the controls don’t ever need to get the focus in order to determine the width.

            • #640678

              Thanks, I will probably only use it for info only.

              Merry Xmas Charlotte, thanks for your help in 2002.

              Pat cheers

    • #639833

      Oof! I don’t envy the problem, but I’m pretty curious to see how you handled it.

    Viewing 1 reply thread
    Reply To: Form Equivalent of TextWidth (97 SR1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: