Access VBA - Hide Labels, text box, buttons If = " " - vba

I'm trying to hide make some labels, text boxes and buttons hidden:
If rst![RI] = "" Or IsNull(rst![RI]) Then
I have:
A Label named "Label83"
A Text box named "C1"
A Text box named "Tex4"
A Text box named "Text8"
A button named "Command18"
So whenever I'm on PM200 and If rst![RI] = "" Or IsNull(rst![RI])
Then the listed label, text boxes and button should be hidden.
Thank you in advance.

You can use as a minimum:
Me!Label83.Visible = Len(Nz(rst![RI].Value))
or, to play nice:
Me!Label83.Visible = CBool(Len(Nz(rst![RI].Value)))
For more controls, set a variable:
Dim Visible As Boolean
Visible = CBool(Len(Nz(rst![RI].Value)))
Me!Label83.Visible = Visible
Me!text8.Visible = Visible
' etc.
And do rename your controls to something meaningful, like: lblCtransfer

Related

Dynamically assign text to text box in the loop

I want to assign Text to the textBox in the look, I tried
Dim textBoxHB As TextBox = FindName("txt_HB_" + iRecCnt.ToString())
Me.Controls(String.Format("txt_HB_" + iRecCnt.ToString()).Text = .HouseBill
My Text box name change form txt_HB_1 ,txt_HB_2 and so on, and i want to where iRecCnt has 1,2.. values and Text is coming form .HouseBill
Is there any other way i can try?
Replace the loop with this:
Dim boxes = Me.Controls.OfType(Of TextBox).Where(Function(b) b.Name.StartsWith("txt_HB_"))
For Each box As TextBox in boxes
box.Text = .HouseBill
Next

Excel VBA changing properties of Form Control on Chart Tab

I have added an Label on my Chart tab, but when I try to change it's font all of the fields are greyed out. I have searched enough but didn't find any solution. I am attaching a screenshot of my chart tab.
I want to change the font properties of this Label 10 on the chart tab. Is there a way I can access the properties by VBA.
To change the text I recorded a macro.
ActiveChart.Shapes("Label 10").Select
Selection.Characters.Text = "Mohit"
ActiveChart.ChartArea.Select
I tried the line code:
Selection.TextFrame.Characters.Font.Size = 20
but it doesn't work. is there a way to change the color and font size of this label ?
Alternate that I am using now
Now I am using Rectangle shape instead of Label. What I wanted to acheive was to display the name of the selected button ( PV , Terrorism or SRCC ) as an indication that this button have been selected. So I clicked PV and my series changed and Rectangle displayed the name of selected Peril.
Now if I select SRCC again the same thing happens.
For the code I only have to change the text of the rectangle every-time a button is pressed as I preset the font and size of text once which remains the same. I wasn't able to change the font and size of text with a label.
ActiveChart.Shapes("Label 10").Select
With Selection
.Characters.Text = "Mohit"
.Font.Size = 20
End With
or you can avoid Select/Selection accessing your Chart object by its name:
With Charts("ChartName").Shapes("Label 10").TextFrame2.TextRange
.Text = "Mohit"
.Font.Size = 20
End With

Why does the GroupBox's Text display as expected, but not the Labels' Text?

I have this form that contains a GroupBox, and in this GroupBox are some Labels and a CheckBox. The CheckBox is set to 'Checked' in design-time, which means when it loads that defaults to 'Checked'. Hence when a new instance of this form is created, the CheckChanged event for that check box fires before the Form's Load event. The Text values of the labels depend on whether the check box is checked or not.
This is in the CheckChanged event for the check box, which fires before the Form's Load event:
If chkUseAsFull.Checked Then
fraHalf.Text = "Full Spacing"
lblLitHalfSpacing.Text = "Spacing"
lblLitHalfSpeed.Text = "Speed"
txtHalfSpacing.Text = txtSpacing.Text
txtHalfSpacing.Enabled = False
cmdAdjustHalf.Enabled = False
Else
fraHalf.Text = "Half Spacing"
lblLitHalfSpacing.Text = "1/2 Spacing"
lblLitHalfSpeed.Text = "1/2 Speed"
txtHalfSpacing.Text = Format(spc, "##0.00")
txtHalfSpacing.Enabled = True
cmdAdjustHalf.Enabled = True
End If
fraHalf is the group box, and lblLitHalfSpacing and lblLitHalfSpeed are labels within it.
Note that in design-time, the Text values of the group box and labels are those specified when the check box is NOT checked.
But when I launch this form, the group box's Text is "Full Spacing" but the labels' Text is "1/2 Spacing" and "1/2 Speed".
Why would the text of the group box show according to the check box's checked value correctly, but not the text of those labels?
Don't rely on the designer to run code for you. Chances are the labels are changing, but then the designer is probably changing them back to their original properties later on in the code. Which means that if you know the default is checked, then the labels should be created in that default mode. Or in the load event, you can just call it yourself
fraHalf_CheckChanged(fraHalf, EventArgs.Empty)

Adapt label width to text

I basically programmatically generate many labels with a text read from a text file so I would like to know how I can programmatically adapt the width of a Label to its text.
Controls come in two flavors. The ActiveX version of a label has an AutoSize property. For example, with a ActiveX label control named Label1 in Sheet1
Private Sub test()
Sheet1.Label1.WordWrap = False
Sheet1.Label1.AutoSize = True
Sheet1.Label1.Caption = "This is a lot of text to put in a label"
End Sub
will automatically adjust the width to fit.

Selecting a random button and modifying its text (VB.NET)

I am making a game of tic tac toe, and I have 9 buttons lined up in a grid pattern. I want to select a random button for the computer to start the game from.
I have an array set up with all the names of my buttons, and I was thinking of picking a random entry from that array to start working from. This I have done fine, but I cannot change the text of a button. My code:
''# Define the array
random(0) = "tl"
random(1) = "tc"
random(2) = "tr"
random(3) = "cl"
random(4) = "cc"
random(5) = "cr"
random(6) = "bl"
random(7) = "bc"
random(8) = "br"
''# Grab a random array entry
StartPoint = random(RandomClass.Next(0, 8))
as you can see, I can't simply do StartPoint.Text = "O", even thought StartPoint holds the name for the button.
Any help on changing the buttons text from the name in StartPoint would be helpful, thanks.
You should create an array of actual buttons (not their names). Then when you grab a random button into a button object, it'll actually be a button so you can change it's text property.
Since you're just passing around references to the actual buttons, this should work pretty well.
Dim buttons(8) As Button
buttons(0) = tl
buttons(1) = tc
''# ...
Why don't you create an array of Button objects?
That way, all you have to do is cast access them and set the Text property.
Button startButton = random(RandomClass.Next(0,8))
startButton.Text = "o"