I have a MS Access Form with various controls organised into tabs.
There are some text fields among them, and I need the text field to update (that is, the vba to update them) when their tab is opened. The text fields aren't bound, the updating is a bit more customised.
I've tried the below code but it returns True for text fields that aren't on the visible tab
Private Sub TabCtl2_Change()
Dim Ctl As Control
For Each Ctl In Me.Form.Controls
If Ctl.ControlType = acTextBox Then
'Want to do stuff here
Debug.Print Ctl.Visible 'This is returning 'True' for all text fields
'not just the ones on the active tab.
Debug.Print Ctl.Name
End If
Next
End Sub
As far as I can see the Tab objects don't have a Controls collection or whatever you call it. Can someone help me how to loop through controls on a tab? I don't want to code it all in manually if I can help it.
The Page object in the Pages collection in the tabcontrol does have a Controls property.
This means, if you want to do something for all controls on the active page, you can use the following:
For Each control In Me.MyTabcontrol.Pages(Me.MyTabcontrol.Value).Controls
'Do Stuff
Next
Related
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.
I am trying to create a form template to use at work that can be customized via check boxes.
So far, I have been thinking about adding check boxes before every paragraph and at the end a button.
When I click the button, my intention is to delete all the paragraphs that don't have the checkbox activated.
The problem is that I don't know which is the most user friendly approach to this.
I am thinking about making bookmarks for each paragraph and use an IF formula for each of the check boxes.
The most user friendly check boxes are the content control ones, but I don't know how to reference them in VBA code.
All I can find is about form field check boxes, but I don't know how to make them clickable.
Before I go studying about each of these two options, I am interested in finding out which of these two alternatives is more appropriate to fulfill my requirements.
Thank you!
I have managed to do what I intended.
First of all, I've put content control checkboxes before every paragraph and I've set a unique tag for each one.
Then, for each checkbox, I've selected the paragraph, including the checkbox, and added a bookmark named exactly like the checkbox. Next, I've selected only the checkbox and added a bookmark name hide_nameofthecheckbox.
I've added an ActiveX button, with the following vba Code on click:
Private Sub btnSubmit_Click()
Dim bookmark As String
Dim ctl As ContentControl
For Each ctl In ActiveDocument.ContentControls
If ctl.Type = wdContentControlCheckBox Then
If ctl.Checked = False Then
bookmark = ctl.Tag
Bookmarks(bookmark).Range.Font.Hidden = True
Else
bookmark = "hide_" & ctl.Tag
Bookmarks(bookmark).Range.Font.Hidden = True
' DO NOTHING
End If
End If
Next
End Sub
Basically, when I click the Submit button, the code verifies each checkbox for value True or False. If it is checked, it hides the checkbox, If it is unchecked, it hides the entire paragraph, including the checkbox.
This way, after I click the submit button, there will be no checkboxes visible, so the document is print-ready.
I have a vb.net form that uses multiple textboxes across several different tabs. Within one of those tabs, I have a sub set of tabs. My save functionality calls stored procs for each tab and cycles through the values on each page to either do an update or a "add new". I noticed that while testing, some of the pages do not save or update any of the values in the textboxes. After a few days of investigating, I realized that if I edit something, then physically click through the other tabs, it all saves/updates properly. If I don't click through them, they don't all save. Is there a reason for this that I am missing? When you enter a search value, I cycle through the pages and populate them all at the same time so I was assuming it wrote those values BEFORE it physically rendered...I guess I am wrong?
From the TabPage documentation Remarks section
Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown.
So the answer to your question is "Yes the tabpage must be shown".
However, the definition of "shown" is subject to interpretation. In reality, all you need to do set the TabPage.Visible property to True and not actually cycle through and display each TabPage.
A recursive scan of the form for TabPage controls will work:
Private Shared Sub TabPagesVisible(parent As Control)
For Each c As Control In parent.Controls
If TypeOf c Is TabPage Then c.Visible = True
TabPagesVisible(c)
Next
End Sub
Example usage:
Sub SaveFormTabData()
TabPagesVisible(Me) ' Me refers to the containing form
' code to save control data
End Sub
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.
I have a main form with a tab control containing multiple subforms. I need to be sure that the data in a subform is saved when the user switches tabs. The problem is that DoCmd.RunCommand acCmdSaveRecord seems only applies to the current form so it doesn't save the data in the subform.
I have tried different events on the subform such as deactivate, OnLostFocus etc but they don't fire until another field somewhere else gets the focus.
The ideal solution would seem to be to put something on the OnChange event of the tab control to be sure that all the data is saved. That is my question, how to do I save the record in a subform?
In Access, the default is to save, so unless you have done something pretty complicated to prevent this, moving the focus from a subform will automatically save the record. You can test this by adding a record, moving from the subform, and then checking the table.
You don't have to do anything at all, as the subform is saved as soon as it loses focus (when the tab control changes).
But as an exercise, I've outlined the code you'd write if you needed to.
You can save any form by setting it's .Dirty property to False. For something like this that's going to run a lot, I think I'd write a sub to walk through the subforms, check if any are dirty, and save the dirty ones. Something like this:
Public Sub SaveSubFormsOnTab()
Dim pge As Control
Dim ctl As Control
For Each pge In Me!ctlTab.Pages
Debug.Print pge.Name
For Each ctl In pge.Controls
If ctl.ControlType = acSubform Then
If ctl.Form.Dirty Then
ctl.Form.Dirty = False
End If
Debug.Print ctl.Name
End If
Next ctl
Next pge
Set ctl = Nothing
Set pge = Nothing
End Sub
Now, that's actually quite inefficient in cases where you have lots of controls on your tab control that aren't subforms. If your tab has nothing but subforms, it will be fairly efficient. In either case, it's much more efficient to use a custom collection populated in the form's OnLoad event, and then you'd walk that collection that includes nothing but your tab control's subforms, and save any that are dirty.
Either of these is preferable to using the OnChange event of the tab, because each time you add a tab page with a subform or change the name of a subform control, you'd have to alter the OnChange event.
I was having a similar issue where I needed various code to run in the subform and values in the main form - that were based on values in the subform - to be recalculated. Here is what finally worked:
This is an example with a main form named 'frmCustomers' containing a subform 'sfmTransaction', which in turn has a subform called 'sfmOrderLine'. The forms have a button 'cmdSave' that is only visible when the user clicks the 'cmdEdit' button (the purpose of which is to lock all the controls until the user clicks the edit button, and then to re-lock them when he clicks save):
On the main form ('frmCustomer') go the subform's exit event, and add 'me.recalc' twice, like this:
Private Sub sfmTransaction_Exit(Cancel As Integer)
If (Not Form_sfmTransaction.NewRecord And Form_sfmTransaction.cmdSave.Visible) Or (Not Form_sfmOrderLine.NewRecord And Form_sfmOrderLine.cmdSave.Visible) Then
'To save subforms
Me.Recalc
Me.Recalc
End If
End Sub
In the subform ('sfmTransaction') go the subform's subform's exit event, and add 'me.recalc' twice, like this:
Private Sub sfmOrderLine_Exit(Cancel As Integer)
If Not Form_sfmOrderLine.NewRecord And Form_sfmOrderLine.cmdSave.Visible Then
'To save subform
Me.Recalc
Me.Recalc
End If
End Sub
Hope this helps.
Setting the dirty property to false may force Access to save the data to the database, but it bypasses the before_update event. This means that if you've used this event for validation or other purposes, you can now have bad data in your database.