Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1141
  • Last Modified:

Automatic resizing of forms and text boxes

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.
  • 5
  • 5
  • 3
  • +3
2 Solutions
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

diddionAuthor Commented:
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.
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!

diddionAuthor Commented:
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......
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)

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.

diddionAuthor Commented:
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?
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.
diddionAuthor Commented:
Don't think so in this case - we need to be more dynamic in the form resizing
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.
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

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.

diddionAuthor Commented:
Thanks for the help - please continue thinking!  I'm away on holiday and will pick this up on return.
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
may not quite be what u want but you never know :)

Jeffrey CoachmanMIS LiasonCommented:

(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: vbaccelerator.com)
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.
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now