I have had a problem when trying to get my label (but same logic would work for a text box) to autosize dependant on what text is in there.
I have found a simpe(ish) solution
There are two text boxes - TextBoxA & TextBoxB
then you type into TextBoxA and press a button it autosizes TextBoxB
the logic is this:
When you press the button it creates a new form and puts a label(or text box) on that form, it then transferes the text over, works out the width then brings that width back and works out its relation to what the curren width is
The Code is:
Note the above will only work if you have a form with two text box 'TextBoxA' and 'TextBoxB' and a button 'TransferButton' and the form will have to be too big - to account for the tex box biend its biggest, it cant go off the form, this can be easaly be resized at the end by using the command:
Me.Section(0).Height = 'The Required height in Twips
ANY QUESTIONS??
I have found a simpe(ish) solution
There are two text boxes - TextBoxA & TextBoxB
then you type into TextBoxA and press a button it autosizes TextBoxB
the logic is this:
When you press the button it creates a new form and puts a label(or text box) on that form, it then transferes the text over, works out the width then brings that width back and works out its relation to what the curren width is
The Code is:
Code:
Private Sub TransferButton_Click()
'Set the TextBox Height, Width and Top
Dim TextBoxHeight As Long
Dim TextBoxWidth As Long
Dim TextBoxTop As Long
TextBoxHeight = 223
TextBoxWidth = 2438
TextBoxTop = 2324
Me.TextBoxB.Height = TextBoxHeight
Me.TextBoxB.Width = TextBoxWidth
Me.TextBoxB.Top = TextBoxTop
'Sort Out Text Box A (Ensure that it does not contain Null infomration)
If IsNull(Me.TextBoxA.Value) Or Me.TextBoxA.Value = "" Then
Me.TextBoxA.Value = "THIS IS A LONG STRING PLEASE TAKE NOTE OF THAT AND WATCH THE MAGIC OF AUTOSIZING!"
MsgBox "You left TextBoxA blank Default Text Entered"
End If
'The Text to be put inot the caption
Dim strText As String
strText = Me.TextBoxA.Value
'Get the required ammount
Dim strRequired As String
strRequired = SizeNewControl(strText)
'Calculate the height
If strRequired <= TextBoxWidth Then
'Set the line height to one
Me.TextBoxB.Height = TextBoxHeight
Else
'It Is bigger than one line!
Dim LineX As Integer
Dim GotTheRequirements As Boolean
LineX = 2
GotTheRequirements = False
While GotTheRequirements = False
If strRequired <= (TextBoxWidth * LineX) Then
Me.TextBoxB.Height = TextBoxHeight * LineX
GotTheRequirements = True
Else
LineX = LineX + 1
End If
Wend
End If
Me.TextBoxB.Caption = strText
End Sub
'------------------------------------------------------
Public Function SizeNewControl(strText As String) As String
Dim frm As Form, ctl As Control
' Create new form.
Set frm = CreateForm
'Hide the form
frm.Visible = False
' Create new command button.
Set ctl = CreateControl(frm.Name, acLabel, , , , 0, 0)
' Set control's Caption property.
ctl.Caption = strText
' Size control to fit caption.
ctl.SizeToFit
'Send the new width back
SizeNewControl = ctl.Width
'Close the form
DoCmd.Close acForm, frm.Name, acSaveNo
End Function
Me.Section(0).Height = 'The Required height in Twips
ANY QUESTIONS??