MS ACCESS VBA - best way to change all properties values of a form's textboxes at once - vba

I have a form with 3 textboxs and i want to change their properties values according to some events.
My idea is to set these properties to a global form's variables in order to use them whenever i want with a function or module (this depends on your suggestion).
Assuming that i want to change all textboxs.enabled property by pressing a button
and i want to change another different property if needed in the future (like bordercolor),
my example is this:
in the form VBA,
Option Compare Database
Option Explicit
Private Const AllTxtboxes = *** all txtboxes ***
in the VBA button
Private Sub button1_Click()
AllTxtboxes.enabled = True
End sub
Thanks in advance.

You need to loop trough all controls and identify those you want to change. The problem is that each control type got their own properties, so you need to make sure you are in the right one.
And for that, there is a property common to all controls named Tag
There, you can specify a value and check it. It's really helpful to select only what you want.
I made a simple form with 6 textboxes:
Note that in design view, properties panel at right, tab others, last property is Tag (sometimes it comes as aditional info or something like it).
There, I typed 99 but only on textboxes 2, 4 and 6, because those are the textboxes I want to handle (that would be kind of your array of textboxes)
I added a command button to change forecolor of those textboxes to red when clicked. My code is:
Private Sub CMD_CHANGE_FONT_COLOR_Click()
Me.Painting = False
Dim MyControl As Control
Dim MyTxt As TextBox
For Each MyControl In Me.Controls
If MyControl.Tag = 99 Then
'we set MyTxt to MyControl so we can use Intellisense, not really needed, but it makes coding easier
Set MyTxt = MyControl
MyTxt.ForeColor = vbRed
Set MyTxt = Nothing
End If
Next MyControl
Me.Painting = True
End Sub
When the form loads, you will see:
But after click on command button, the forecolor of those 3 textboxes will be red:
There you go. So everytime you want to do something with that array of textboxes, you can use this code to loop and change the properties you want.

Related

How do I properly use if/then for a bolean value in order to make an object visible?

I want to display an endless access form. For each data set there is a yes/no value (PA). If yes, then a hidden object should be displayed. It seems very straight forward, but it doesn't work.
I have tried by changing the value of PA to 1, 0, -1. Either nothing happens, or the object will be displayed for all data sets.
The object is defined as hidden in the form.
Private sub form_current()
If PA.value = true Then
me.object.visible = True
End if
End Sub
I would be very happy for some advice. /LP
Handle the control's Change event, and then you can assign to its value:
Private Sub PA_Change()
Me.object.Visible = PA.Value ' TODO: give 'object' an actual name
End Sub
Find PA in the top-left codepane dropdown, then select the Change event in the top-right code pane dropdown if it's not automatically selected - the VBE will generate the event handler procedure for you.
As the object is unbound, you can't do this. When unbound, it will either be visible or not - for all records.
One workaround is to move the control to a tiny subform having a master/child relation to the main form.

Cannot grab list box using Visual Basic in a Word document

My goal for what I am trying to do overall is have one box on a document that holds a database type and when someone select that database type the document will hide/show certain sections.
Well my issue right now is that I cannot seem to grab the the list box ContentControl I created.
I have a listbox ContentControl with a tag boxDB.
Currently this is all that I can get
Sub ListBox_AfterUpdate()
Dim box
Set box = ActiveDocument.SelectContentControlsByTag("boxDB")
End Sub
I have tried several things with box to try and get it to have data. My first assumption was that since it is a collection I would just find the first item by using the first index of the items..but it always says that there is no item at that index.
Is there another method I should be using to grab the value that was set?
In order to access the control and it's features by tag, read the reference documentation for SelectContentControlsByTag. This shows that the returned object is a collection of controls that match the tag, so clearly you might have several controls with the same tag string.
You will help yourself quite a lot by always using Option Explicit in all your code and by specifically declaring each variable with a type. That way you can use the Intellisense feature of the VBA Editor to reveal what methods and properties are available as you write your code.
Assuming that your ContentControl is already established with the tag (and that you only have one control with that tag
Option Explicit
Sub SetupListbox()
Dim theControls As ContentControls
Dim box As ContentControl
Set theControls = ThisDocument.SelectContentControlsByTag("boxDB")
Set box = theControls.Item(1)
box.DropdownListEntries.Add "Red"
box.DropdownListEntries.Add "Green"
box.DropdownListEntries.Add "Blue"
box.DropdownListEntries.Add "Yellow"
box.DropdownListEntries.Add "Orange"
End Sub
Sub GetSelectedItem()
Dim theControls As ContentControls
Dim box As ContentControl
Set theControls = ThisDocument.SelectContentControlsByTag("boxDB")
Set box = theControls.Item(1)
Debug.Print box.Range.Text
End Sub

How to update a label's caption on another user form?

I am using VBA to Show a userform.
On this form, I have buttons to open another userform (AddAmountForm).
When I click on the button to open AddAmountForm I also want to update the caption for a label that is on AddAmountForm.
Here is what I've tried this on the first userform:
Private Sub AssocDuesAddbtn_Click()
AddAmountForm.Show
With AddAmountForm
.AddName.Caption = "Assoc. Dues"
.Repaint
End With
End Sub
However, this doesn't update the caption, it remains the same.
There will be several buttons that will open this second userform and each one will need to update the label to something different based on what each button is supposed to be updating.
What am I doing wrong?
AddAmountForm.Show
This displays the default instance of the AddAmountForm form, modally. This means the next instruction will only execute after the form is closed.
You probably mean to change its label before you show it:
With AddAmountForm
.AddName.Caption = "Assoc. Dues"
.Show
End With
That said, using a form's default instance is going to bite you in the rear end, one day or another. Forms are objects, treat them as such:
With New AddAmountForm
.AddName.Caption = "Assoc. Dues"
.Show
End With
If the caption is a compile-time constant (a string literal is a compile-time constant expression), there's no reason to assign it at runtime - just set the label's caption in the form designer's Properties toolwindow (F4).

Microsoft Word VBA tab key to make textbox visible

Longtime viewer, first time question asker.
I'm currently working with UserForms within MS Word and have a particular form that can have up to 20 different labels and accompanying textboxes with varying texts. I have all but the first hidden while not in use, however I would like the next label and text box to become visible following input in the previous textbox. So if you enter data (anything) in the first textbox, the next label and text box will become visible. Does this make sense? I've seen other responses here suggest using AfterUpdate() rather than Change() or Click() but can't figure out how to use any of them. I would share my code but at this point I don't have any code to share, other than my labels and textboxes are lblField1 txtField1, lblField2 txtField2...
Any suggestions?
I would suggest using Change event, when using AfterUpdate you need to leave you TextBox for a while to fire the event. If you have only one TextBox visible there is nothing to move to. If you have more TextBoxes you would need to move back to fire AfterEvent and I don't think this is what you expect.
So, double click wherever on your userform and add the following code in code area:
Private Sub txtField1_Change()
txtField2.Visible = True
lblField2.Visible = True
End Sub
Next, add next portion for next textbox:
Private Sub txtField2_Change()
txtField3.Visible = True
lblField3.Visible = True
End Sub
And so on, if only you have an order in controls name you just need to change numbers in the end of control names.

Access Subform Source object

What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value