Access Report with dynamically changing text box size - vba

Is it possible to have a report with flexible text width and height? I sometimes have two words in this text and some times hundreds. I want to have small text for the first and big text for the second. How do I do that?

I would advise you to set a your text box size to what you think is optimum and use the CanShrink and CanGrow properties (tap the text box and then open the properties windows and you can find them there).
The CanGrow property indicates whether the size of the text box can increase vertically according to its content. Similarly, CanShrink decreases the height of the text box according to its content. Here is a link for better understanding these two properties.

Use the Detail_Format event.
It fires before each line, and you can change the formatting based on the length of the text.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Len(Field1) < 10 Then
txtField1.FontSize = 18
Else
txtField1.FontSize = 12
End If
End Sub

Related

Fit the text inside a TextBox in VB.net windows forms

In one of my application i require to place the text inside a textbox and textbox width and height must change according to the length of text. So i am using
TextSize = gr.MeasureString(textcontent, TextFont)
where textcontent is content of text and TextFont is type of font.(refer this link)
But if text contains large number of character say it require about 2 lines inside a form(if the text will not fit in single line) then i need to set height also. So I want the text to be fit properly inside the textbox for any given text. Also if there are is case of multiple lines(for large text) then there should be not be extra space at the end of first line and between second line. So how it can be done?
It's going to be harder if you use a regular TextBox control since the properties are limited. So I've taken the liberty to suggest to use the RichTextBox control instead since it's still a TextBox but with more features... So try to put a RichTextBox control onto your form and add this code...
Private Sub RichTextBox1_ContentsResized(sender As Object, e As System.Windows.Forms.ContentsResizedEventArgs) Handles RichTextBox1.ContentsResized
RichTextBox1.Height = e.NewRectangle.Height + 12
End Sub
Do something like this...
Decide maximum width (Mx) of textbox according to size of form or as you wish.
Calculate length L required for a string as you are doing.. TextSize = gr.MeasureString(textcontent, TextFont)
If L is less than or equals to Mx then change the width of textbox to L.
If L is Greater than Mx then Height factor (Hf) = L/Mx.
Set txtBox1.Multiline = true and change txtBox1 height to txtBox1.Height * Hf and set txtBox1 width = Mx

Cut status strip label to width of form

I have a form with a status strip, which contains a progress bar an a label. Both of these are used to show the user the status/progress of several background workers.
My problem is that sometimes the label is longer than the form is wide (it contains Parameter names that vary quite widely in length). The form has a constant width and is not re-sizable by the user. When this issue occurs the label just appears as blank, I would instead like to cut the label to the length of the form and concatenate "..." on the end.
Can anyone give me some advise on where to start with this? i have tried Google and SO searches and have been unable to come up with anything similar. I essentially need to find the length of the string as it will display on the form, but I don't know where to start with that.
First thing to do is to change the StatusStrip.LayoutStyle from Table to Flow. Which will prevent the label from disappearing. Next, you still want the user to have a chance to read the full text of the label even though it is truncated. Set the StatusStrip.ShowItemToolTips property to True and the label's AutoToolTip to True.
Getting the label's text to not overlap the grip is an uglier problem to fix but one you don't have since you made your form un-resizable. Set the form's SizeGripStyle property to Hide.
This will fix your problem, no code required.
You can try something like this:
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
If Label1.Text.Length > iMaxLblLenght Then
Label1.Text = Label1.Text.Substring(0, iMaxLblLenght) & "..."
End If
End Sub

Moving label to fit according to length vb.net

Hi I have a button which will add a string in to a label. (this can be done multiple times.) As the user can add multiple strings with each one been a different length, is there anyway I can get it to find the length of the label and those around it so that it spaces out correctly?
Thanks
Label.Width will return the current width of the label control, but it sounds like you already know this. Since the label can be narrower than the text it is trying to display, you would need to measure the full text using the graphics object. This method will return the width of the text in a label:
Private Function getFullTextWidth(lbl As Label)
Using g As Graphics = Label1.CreateGraphics()
Return g.MeasureText(Label1.Text, Label1.Font).Width
End Using
End Function
Alternatively, you could just set the label's AutoSize property to true, and then just check the label's Width property after you set the Text.

Multiple Text Box and Lookups

I am currently trying to improve on an Access Database VBA that I have inherited from my predecessor at work. I have come unstuck on a particular form.
I have a form that at the moment is just a large form containing 32 individual textbox, with the same code behind each but it is the same code repeated for each textbox with just the references to the text box changing in each.
Private Sub Cand_No2_AfterUpdate()
Cand_Name2 = DLookup("[Name]", "[qryExamAbsences]", "[Cand_No] = Cand_No2")
End Sub
Then once the button is pressed
If Not IsNull([Cand_Name1]) Then
Rope = Rope & " Or Cand_No = " & [Cand_No1]
End If
(The If statement is contained in the button mousedown event.)
Occurs for each text box which then filters a report that is printed for office use. There are many problems with this but the major one I am trying to solve is that there is an upper limit to the number of entries, if I need to filter more than 32 I would need to delete the text and start again as it were.
Is there a way of combining all this into a single section of code which will create text boxes when needed?
EDIT.
I have found a way to give the impression to the user that the text boxes are being created after each entry which has improved the form from a user standpoint (no longer having 32 textboxes or having to scroll down to the Print Button.) however this still hasn't solved the issue of messy code as I have had to repeat the extra code for each box again, it also leaves me with the maximum of 32 entries still.
The new code is as follows:
If Not IsNull(Cand_Name1.value) Then
Cand_No2.Visible = True
Cand_Name2.Visible = True
cmdPrint.Top = 2500
cmdPrint.Left = 2500
DoCmd.MoveSize 1440, 2201, , 4000
Else
Cand_No2.Visible = False
Cand_Name2.Visible = False
cmdPrint.Top = 2000
DoCmd.MoveSize 1440, 2201, , 3500
End If
Essentially makes the next text box visible and moves the print button down to make room for the new text boxes. It also expands the window.
Could you not just have 2 text boxes, one for CAND_NO and another for CAND_NAME and then beside those two boxes place an ADD CAND_NO button.
Create a list box that would list every CAND_NO / CAND_NAME after they press the add button so they can see what they've added so far. Then loop through your list box to build your rope string or have your rope string either a global variable on the form and build it as they add numbers or stored in a hidden text box storing the value as they add numbers if you don't like global.

How to increase the size of a text box base on the text content

I have a report that I need to increase the size of the textbox base on the length of the text. There some formula to calculate the resulting lenght size?
You can either go for a Label with the AutoSize property set to True. Or if you want to stick to TextBox, you can make use of MeasureString method and get the width of the text from there. Use that width to set the width of TextBox.