Automatic resizing of forms and text boxes

Posted on 2006-07-02
Last Modified: 2012-08-14
When data is inputted into a text box, I would like the box to grow vertically so that the entire text can be seen, and for the display to remain expanded.  At the same time, I would like the continuous form itself to grow vertically in response to the expanded text boxes.  This will give the most efficient and compact data display for my database.  Any suggestions on how this could be done, with code examples?  With thanks.
Question by:diddion
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +3
LVL 65

Expert Comment

ID: 17026304
So I take it u dont want a predefined large size? Would that not be easier for u. You can create scrollbars on that textbox.
There is the Can Grow/Shrink properties which resizes when printing, though not 100% sure if it applies to forms


Author Comment

ID: 17026336
Thanks - this is not what we are looking for.  This is a screen-based diary/record system, working as follows:

A task is created that is to be carried out for a client - this is the 'item' below..  Normally, that task is fairly short, but it may on occasions be quite long.  The task form includes a coninuous subform where notes are added - each note will be of different length.  When the task form is opened, what is needed is a view of all text shown in the task form and the subform - for example:

Date               Item                                   Notes

anydate           Check product submission     first note
                                                                subsequent long note with a lot of text
                                                                third brief note
                                                                 final note associated with the product submission

Clearly, one way rouind this would be to have large forms and large text boxes, but as many different tasks as possible need to be displayed on a single screen - so each new record must start off as a single line with the ability to grow vertically.
LVL 54

Expert Comment

ID: 17026407
This won't be possible as the CanGrow property applies onlyto reports and on a continuous form the detail section will match the largest field so when you change it in one row the other rows will increase just as much...

The only workaround I use in cases like this is an additional large textbox on the mainform and there the text of the current row's textbox is made visible in a large size.

Idea ?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 17026419
Thanks.   It doesn't matter if the other fields also grow.  What we're looking for is for the text box fields to grow vertically  according to the data input, with the record growing accordingly......
LVL 65

Expert Comment

ID: 17026471
ok, how does this sound. Its only a start. I havent figured out if one has entered text thats starts to scroll i..e too big for textbox

If height of texbox was 255 for example

And if user enters {ctrl enter} to add to a new line in that textbox, then resize at that point

Private Sub txtNote_KeyPress(KeyAscii As Integer)
    If KeyAscii = 10  Then   'ctrl enter hit
        txtNote.Height = txtNote.Height + 255
        MsgBox "Resized"
    End If
End Sub

but then what u have to do is, if controls exist under this textbox, u have to reposition them (just the TOP property, simply add 255 to all of them)

LVL 54

Expert Comment

ID: 17026505
Then setting the max height shouldn't be a problem too.
When it still doesn't fit for the newest entries, then a vertical scrollbar will be made available.


Author Comment

ID: 17026528
Nic - thanks for this.  It works as you have indicated, but has the disadvantage that the text box is resized for all records - we only want it to be resized (and to stay that way) for the specific record.

Aslo, do you know how the form itself could be resized automaticallyto accommodate the enlarged field - again, specific to the record in question?
LVL 38

Expert Comment

ID: 17026584
See this link:

If this is more along the lines of what you want, maybe this macro would work on your form if you could use a data sheet view.

Author Comment

ID: 17026610
Don't think so in this case - we need to be more dynamic in the form resizing
LVL 38

Expert Comment

ID: 17026716
Another idea.....Look into Smart tags if you have Access 2003+....the excerpt is from Microsoft's writeup:

Access 2003 uses smart tags for the following purposes:
·New! Error checking is made easier with smart tags. Access 2003 flags common errors on forms and reports and offers options for correcting them.
New! Propagating field properties is as simple as modifying a property in one location and then selecting to propagate the change throughout all controls bound to that field.
LVL 65

Expert Comment

ID: 17026732
Because u use a continuous forms, u cant have individual fields based on the same control

You know there is always the zoombox
If u hit SHIFT F2 on a textbox, it brings up the zoombox

Perhaps u can go directly to the zoombox when they enter that field

LVL 54

Accepted Solution

nico5038 earned 250 total points
ID: 17026775
With a continuous form you're just out of luck, you can't change the detail sections individually.
Thus my proposal to use a main/subform combination that will allow you to show the text in a separate large textbox on the main form.
The other option is to have an additional toggle button to allow the user to enlarge/shrink the textbox by code like that provided by rockiroads.


Author Comment

ID: 17026808
Thanks for the help - please continue thinking!  I'm away on holiday and will pick this up on return.
LVL 65

Assisted Solution

rockiroads earned 250 total points
ID: 17026823
If u wanted the zoom, on the GotFocus of a textbox, u can add this code

DoCmd.RunCommand acCmdZoomBox

And it will bring up a zoombox

Enjoy your hols
LVL 65

Expert Comment

ID: 17031012
may not quite be what u want but you never know :)

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 17069823

(Not looking for any points)

Again, might not help much (this is only for automatically sizing Combo Boxes only)
But it might give you some ideas:
(Again, on a contionuous Form all of the comboBoxes will be the salme size)

(I think I got this from:
This tip shows you how to get and set the width of the drop down portion of a combo box. It also includes code to automatically set the drop down width based on the contents of a combo box by measuring the size of the text in each combo box item.

Start a new project and add a module. Then add the following code to the module:
' These functions required to set the drop-down width:
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SETDROPPEDWIDTH = &H160

' These are only required if you want to automatically
' calculate the drop-down width:
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Const DT_CALCRECT = &H400

Public Property Let DropDownWidth(ByRef cboThis As ComboBox, ByVal lWidth As Long)
    SendMessageLong cboThis.hwnd, CB_SETDROPPEDWIDTH, lWidth, 0
End Property
Public Property Get DropDownWidth(ByRef cboThis As ComboBox) As Long
Dim lW As Long
    DropDownWidth = SendMessageLong(cboThis.hwnd, CB_GETDROPPEDWIDTH, 0, 0)
End Property
Public Sub DropDownWidthFromContents(ByRef cboThis As ComboBox, Optional ByVal lMaxWidth = -1)
Dim i As Long
Dim tR As RECT
Dim lW As Long
Dim lWidth As Long
Dim lHDC As Long

    ' Evaluate the width of each item in the
    ' combo box:
    ' First set the combo's parent form font to the
    ' combo font:
    With cboThis.Parent.Font
        .Name = cboThis.Font.Name
        .Size = cboThis.Font.Size
        .Bold = cboThis.Font.Bold
        ' Surely you don't have a combo box with
        ' italic font?
        .Italic = cboThis.Font.Italic
    End With
    ' Cache the HDC of the parent form for speed:
    lHDC = cboThis.Parent.hdc
    ' Loop through each combo box list item & get its
    ' width, storing the largest:
    For i = 0 To cboThis.ListCount - 1
        DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT
        lW = tR.Right - tR.Left + 8
        If (lW > lWidth) Then
            lWidth = lW
        End If
    Next i
    ' Don't allow width to exceed specified max
    ' width, or the width of the screen:
    If lMaxWidth <= 0 Then
        lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16
    End If
    If (lWidth > lMaxWidth) Then
        lWidth = lMaxWidth
    End If
    ' Combo box looks a bit strange when the
    ' drop down portion is smaller than the
    ' combo box itself:
    If (lWidth < cboThis.Width \ Screen.TwipsPerPixelX) Then
        lWidth = cboThis.Width \ Screen.TwipsPerPixelX
    End If
    ' Set the drop down width:
    DropDownWidth(cboThis) = lWidth
End Sub
To try out a the function, add a Combo box, a Label and three Command buttons to your project's form. Set the captions for the command buttons as follows:     Command1     A&dd String
    Command2     &Calc Width
    Command3     &Set Width...
Then add this code to the form:
Private Sub Command1_Click()
Dim sI As String
    sI = InputBox("Enter string", , "New item")
    If (sI <> "") Then
        Combo1.AddItem sI
    End If
End Sub

Private Sub Command2_Click()
    DropDownWidthFromContents Combo1
    Label1.Caption = DropDownWidth(Combo1)
End Sub

Private Sub Command3_Click()
Dim sI As String
    sI = InputBox("Enter width", , DropDownWidth(Combo1))
    If IsNumeric(sI) Then
        DropDownWidth(Combo1) = CLng(sI)
        Label1.Caption = DropDownWidth(Combo1)
    End If
End Sub

Private Sub Form_Load()
    Label1.Caption = DropDownWidth(Combo1)
End Sub
Start the project. The width of the combo box wil lbe added to the label control. You can use the Add String button to add new items to the combo box, Calc Width to automatically set the drop down width to the control's contents and Set Width to set your own width. Note that all widths are specified in pixels.
LVL 27

Expert Comment

ID: 17256277
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Split: nico5038 {http:#17026775} & rockiroads {http:#17026823}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question