Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I create a text box on a form that grows wider with more content? 1

Status
Not open for further replies.

MacroScope

Programmer
Jul 17, 2010
286
US
I'm trying to create a text box on a form in Access 2007 that will grow to accommodate more characters. The CanGrow and CanShrink don't make any difference and I can't seem to find any other appropriate way. Can anyone suggest a method that will allow the box to grow as additional characters are added?
 
The CanGrow property is meaningful only in a Report, not in a Form.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Something like the following might be a good starting point:

Code:
[blue]Option Compare Database
Option Explicit

Private Type Size
        cx As Long
        cy As Long
End Type

Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long


' Assumes text0 parent is an Access Form
Private Sub Text0_Change()
    Dim myFont As IFont
    Dim result As Long
    Dim sz As Size
    Dim hDC As Long
    
    hDC = GetDC(Text0.Parent.hwnd)
    Set myFont = New StdFont
    myFont.Name = Text0.FontName
    myFont.Size = Text0.FontSize
    myFont.Bold = Text0.FontBold
    myFont.Weight = Text0.FontWeight
    result = SelectObject(hDC, myFont.hFont)
    GetTextExtentPoint32 hDC, Text0.Text, Len(Text0.Text), sz
    Text0.Width = sz.cx * 15 + Text0.LeftPadding + Text0.RightPadding  'twips
    If result Then SelectObject hDC, result
End Sub[/blue]
 
How are ya MacroScope . . .

Why not use a decent size memo control with verticle scroll bars? Surely you'll hit a size limit soon enough anyway.

Your Thoughts? . . .


See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks for all the responses.

PHV, I found out that CanGrow isn't functional on forms. I'm just wondering why they have it as an option there at all. Would make more sense not to bother to include it at the form level to me.

strongm, that worked. Very creative coding there. Thank you so much for your help. (I wish they had a high-five emoticon!)

AceMan, I'm doing good. This was an experiment to see if it will be practical for the application I have in mind. I'm not even 100% sure I'm going to use it yet, but at least now I can look at it and decide whether this is practical.

All the assistance is greatly appreciated.
 
MacroScope . . .

I don't think this will work well with a bound textbox on a continuous form (expect the textbox of each record to expand as well). Note that on a report CanGrow/CanShrink are record independent!

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Duane is correct, of course; CanGrow/CanShrink is listed on Forms because they work with Forms if the Forms are being printed, but most of us discourage this practice!

The Missinglinq

Richmond, Virginia

The Devil's in the Details!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top