Results 1 to 10 of 10
  1. #1
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Form Equivalent of TextWidth (97 SR1)

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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Form Equivalent of TextWidth (97 SR1)

    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.
    Attached Files Attached Files

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Form Equivalent of TextWidth (97 SR1)

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

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Form Equivalent of TextWidth (97 SR1)

    Charlotte,

    The MSKB code needs the control to have focus at two places:
    1. <LI>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.
      <LI>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

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Form Equivalent of TextWidth (97 SR1)

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

  6. #6
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Form Equivalent of TextWidth (97 SR1)

    Naughty naughty Charlotte, you are breaking one of your own rules. Would you like to inform us of your method please. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    Pat <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  7. #7
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Form Equivalent of TextWidth (97 SR1)

    Oof! I don't envy the problem, but I'm pretty curious to see how you handled it.
    <font face="Comic Sans MS"><font color=blue>~Shane</font color=blue></font face=comic>

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Form Equivalent of TextWidth (97 SR1)

    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. <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23> Besides, you can always use Hans' code. It works too. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Charlotte

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Form Equivalent of TextWidth (97 SR1)

    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.
    Attached Files Attached Files
    Charlotte

  10. #10
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Form Equivalent of TextWidth (97 SR1)

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

    Merry Xmas Charlotte, thanks for your help in 2002.

    Pat <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

Posting Permissions

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