Assigning a tag to multiple controls, of a given type, at once - forms

I have a database in Microsoft Access that I've designed a front-end for. There are approximately 35 forms/subforms so there are hundreds of controls. I have an audit structure for the entire front-end, but it requires adding a tag to each textbox control.
Is there an easy way to do this? Otherwise it'll be extremely manual, tedious and time consuming. I'd much rather write a bit of code that will automate the process of setting the tag for all of the textbox controls.

You can use the following code in a single form:
Public Sub AssignTag()
Dim control As Variant
For Each control In Me.Controls
If control.ControlType = acTextBox Then
control.Tag = "MyTag"
End If
Next
End Sub
Then you have to open the form in layout view, change something, save the form, and possibly change the thing back, to make the change stick (changing controls using VBA doesn't persist unless you make an additional change in layout view).

Loop through AllForms, open each Form in design mode, set tag to all TextBox controls, save and exit.
Public Sub SetControlsTag()
On Error GoTo ErrProc
Dim obj As AccessObject, frm As Form, ctl As Control
For Each obj In CurrentProject.AllForms
'Open in design mode
DoCmd.OpenForm CurrentProject.AllForms(obj.Name).Name, acDesign
Set frm = Forms(obj.Name)
'Set tags
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Then ctl.Tag = "Whatever..."
Next ctl
'Close
DoCmd.Close acForm, frm.Name, acSaveYes
Set frm = Nothing
Next obj
Leave:
Set frm = Nothing
On Error GoTo 0
Exit Sub
ErrProc:
MsgBox Err.Description
Resume Leave
End Sub

Related

Open Form to a value selected on ComboBox - Ms Acces

I want to open a form to a specific record based on the value is selected on a comboBox. I have written a code and its working, but before opening the form it shows a dialogue box with input field asking for the parameter i want for the form which i dont want the VB code to ask.
DoCmd.OpenForm "Final_Exam", acNormal, , "[admclass] = " & Me.Combo4.Value & ""
This is the code i have written and the requirement is that on clicking the button the form opens without any dialogue box asking for parameter. thanksa
What I've found is the best way to open one form from another is to use OpenArgs. When opening Form 2 from Form 1's button, use code like this:
Private Sub cmdOpenOtherForm_Click()
DoCmd.OpenForm FormName:="frmOtherForm", OpenArgs:=Me.Combo4.Value
End Sub
Then, in the Load event of Form 2, use the openargs to set up your filter:
Private Sub Form_Load()
If Not IsNull(Me.OpenArgs) Then
Me.Filter="[admclass]=""" & Me.OpenArgs & """"
Me.FilterOn = True
End If
End Sub
If the field you're filtering on is a text field, make sure to properly escape double quotes (as is done above).

How to navigate from a form to a subform in a different form?

I'm working on a Access-Database that is split into multiple parts. Each has a Main-Form with Subforms in nested Tabs.
Walkthrough
We have a starting-Area that is separated into different Themes. Those theme-forms have buttons that represent Subthemes.
When I hit Button A i want to navigate from Mainform 1 to Mainform 2 Subtheme A (easy as it is the default).
When I hit Button B I want to navigate from Mainform 1 to Mainform 2 Subtheme B. (I can't get this to work)
What i know
I can easily get from Mainform 1 to Mainform 2 landing per default on Theme 1
Private Sub buttonB_Click()
DoCmd.openForm "Mainform2", _
End Sub
I don't know how to navigate to Mainform2->Sub Theme B. I can open the right subform using the OpenArgs but i cant do it with the Tabs. I tried to use the DoCmd.browseTo in onLoad() of mainform2 but that breaks the process.
If i am not mistaken there should be a way to use DoCmd.browseTo in the buttonB_click() but i cant get the path right.
DoCmd.OpenForm Method (Access)
DoCmd.BrowseTo Method (Access)
I tried to describe the problem as general as possible so answers can be helpful for others as well. I hope you can help me!
So I managed to resolve it:
First we navigate to the Main-Form of Theme2 and tell it which topic we want with the help of OpenArgs:
Private Sub buttonThemeB_Click()
DoCmd.openForm "Mainform2", _
OpenArgs:="Theme B"
End Sub
In the FormLoad() of Mainform2 we check the OpenArgs and navigate the Subform-Controle:
Private Sub Form_Load()
Dim strSubFormToken As String
' If OpenArgs property contains a subform name, open corresponding subform
If Me.OpenArgs <> 0 Then strSubFormToken = Me.OpenArgs
Select Case strSubFormToken
Case "Theme A"
strSubForm = "form_themeA"
Case "Theme B"
strSubForm = "form_themeB"
End Select
strSubForm =
If Len(strSubForm) > 0 Then
DoCmd.BrowseTo acBrowseToForm, strSubForm, "frmTheme2.Navigationsunterformular" 'Your Navigationtarget
End If
End Sub
The Mainproblem in all this is that if you use the DoCmd.BrowseTo Path to Subform Control you have to know some rules that aren't documented:
the “>” character seems unusual, that’s because it has been introduced specifically for the Path parameter of BrowseTo. Its meaning is this: The form to the right of the “>” character is loaded into the subform control specified to its left.
The path must specify a series of Form.SubFormControl pairs. Each subform control specified must be a control in the form that precedes it in the path.
The pairs must be separated by the “>” character.
and most important:
The first form in the path must be the form that is currently loaded directly in the Access window (or browser window if the application is running on the web.) This means that the Path parameter only allows you to change the contents of a currently-visible subform control.
So we have to first open the new main-form before navigating the navigation-subcontrol.
Seems to be the best way to handle this.
Thanks for the suggestions!

Open Subform in Access in New Window

I have a form in access ("F_Filter") where I indicate parameters to filter a datable. Form F_Filter also contains a subform which shows the filtered datatable results on the same screen. This subform with the filtered results is named "Child400." Its Source Object is "F_FilterResults".
I would like to be able to 1) open the subform in a new window so all I see are the filtered results and 2) export the results to Excel.
Normally I would be able to open a form in a new window by creating a button and creating an on click event with the following code
DoCmd.OpenForm "NameOfForm", acFormDS
However, this code does not work when I put in "Child400" as the NameOfForm. I think this is because Child400 is a subform and is not recognized by Access.
I also tried DoCmd.OpenForm "[F_Filter]![Child400]", acFormDS to no avail. Note that I have also tried DoCmd.OpenForm "F_FilterResults", acFormDS which works fine but this table only contains the prefiltered results.
DoCmd.OpenForm "F_FilterResults"
is the correct form to open. "Child400" is (I assume) the name of the subform control. That is not a form that you can open.
Then you need to apply the same filter as you did to the subform instance. The same method you use now for the subform can be used for the separate form.
If you have trouble with that, please add the existing filter code to your question.
Edit
There is no magic behind what records a form shows. It is controlled by a couple of properties. The easiest way is probably to simply take them over to the new form.
Something like:
Sub OpenResults()
Dim fSub As Form, fNew As Form
Set fSub = Me!Child400.Form
DoCmd.OpenForm "F_FilterResults", acFormDS
Set fNew = Forms!F_FilterResults
fNew.RecordSource = fSub.RecordSource ' if you change the RecordSource in your code
fNew.Filter = fSub.Filter
fNew.FilterOn = fSub.FilterOn
fNew.OrderBy = fSub.OrderBy
fNew.OrderByOn = fSub.OrderByOn
End Sub

Using RecordSource to change form contents

I am trying to create a method that displays a selected table in a DS view form. The form will be used repeatedly to view different tables, so I figured the easiest way would be to create a form and dynamically change the RecordSource based on the user selection.
I created a form called "frmDisplay" to be used to display the contents of a table to the users. I am trying to use this code to update the form.
Private Sub btnViewEntries_Click()
Dim frmDisplay As Form
Dim selection As String
Me.cmboSelection.SetFocus
selection = "tbl" & Me.cmboSelection.Text
Set frmDisplay.RecordSource = selection
DoCmd.OpenForm "frmDisplay", acFormDS
End Sub
Currently, the code is throwing an "Improper use of property" error when I try to set the RecordSource to the selection made in the combo box.
Set frmDisplay.RecordSource = selection
I have looked around for a while now, but I cannot seem to find any definitive answer that applies to this example. Most posts that I found were missing the Set keyword...
Does anyone recognize the problem? More importantly, is this even the best way to make this
work?
Thanks
_____ EDIT _____
I figured it out, see code below.
Dim selection As String
Me.cmboSelection.SetFocus
selection = "SELECT * from tbl" & Me.cmboSelection.Text
DoCmd.OpenForm "frmDisplay", acDesign
Forms!frmDisplay.Form.RecordSource = selection
DoCmd.Close acForm, "frmDisplay", acSaveYes
DoCmd.OpenForm "frmDisplay", acFormDS
I note you've already provided your answer, but you may find the following helpful.
If you have a main form with the controls cmboSelection and btnViewEntries then add a subform control and call it fsbDisplay. Don't put anything in this subform, leave it unbound.
Now try the following one-liner:
Private Sub btnViewEntries_Click()
fsbDisplay.SourceObject = "Table.tbl" & cmboSelection.Value
End Sub
The advantage of this method is that you can use it to call up any table or query (prefix with Query. instead of Table.) and the fields shown are dynamically created.

VBA Hide User form but retain data entered into it

I'm back again with what I hope is a fairly easy question.
I'm attempting to create a user form in VBA. The user will enter certain bits of information into the form, and then close the form. I'd like the user form to retain the data entered after it is closed by the user. I'm treating it as a class module, since techinically they are, or at least that is how I understand it. Here is the code I'm using:
In the main sub that displays the user form:
Sub NonACATMemo()
Dim UserInput As MemoReasons
Set UserInput = New MemoReasons
UserInput.Show
... And then in the user form itself to close it...
Private Sub UserForm_Terminate()
MemoReasons.Hide
End Sub
I also call this sub from a command button on the form. The issue I'm running into is that when I use this method, I get an error "Run-time error '402': Must close or hide topmost modal form first." If I use unload me, when I try to get data out of the form it is cleared and I get a "server not available" error or something to that effect.
So, any ideas on hiding a user form but retaining the data inside?
Final couple of notes: This is the only user form in the project, and here is an example of how I'm trying to get data out of it using the Public Property Get method:
Debug.Print UserInput.EmailFlag
Debug.Print UserInput.ContraFirm
Debug.Print UserInput.MemoReason
Well, I'm all ears if anyone has any suggestions.
It's an old topic... hopefully someone will need a help on this.
You can do the following :
1-Place a Close/Cancel button (you can set Cancel property to True)
2-Attach following code to Click event
Private Sub btnClose_Click()
'Do some stuff if necessary
Me.Hide
End Sub
3-Attach this code to QueryClose event
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then 'vbFormControlMenu = 0 ([X] button on top right), see MSDN
Cancel = True 'Don't fire Terminate event...
btnClose_Click '...instead, call my close event handler
End If
End Sub
You can check the result by placing a debugger breakpoint in UserForm_Initialize event and it should be fired only for the first time showing the UserForm and thus, granting UserForm state preservation.
I've not seen this approach before. Normally, I would just instantiate the form by:
MemoReasons.Show
Indeed the _Terminate() event is wiping out the data held in the form. So the solution is to not call the _Terminate() event from the button-click. Instead, simply hide the form, e.g.:
Sub ShowMemoReasons()
'In a normal code module, this calls the form
' could be run from the macros menu or attached to
' a shape/button/etc on the worksheet.
MemoReasons.Show
End Sub
Put these in the MemoReasons code module:
Private Sub CommandButton1_Click() '<-- Rename to handle your button's click event
MemoReasons.Hide '## Hides the form but does not release it from memory
End Sub
Private Sub UserForm_Terminate()
'Any events pertaining to the termination of the form object
' otherwise, all form control data will be wiped out when
' this object releases from memory
End Sub
After you do these, if you use the button to HIDE the form, you can call the ShowMemoReasons() and it should re-display the form, while preserving data that was previously entered in the form.
If you use the red "X" button or some other event triggers the Terminate event, you will lose the form data. There are ways to do validation and prevent this with the QueryClose event if necessary.
Update
I don't think you need to Dim an instance of the user form (an exception would be if you will be potentially displaying multiple forms at the same time). Otherwise, declaring UserInput as a public variable is redundant and confusing.
Inicidentally, this is why you're getting the error: Must close or hide topmost modal form first. If you must implement it this way, instead of doing MemoReasons.hide you should use Me.Hide.
As long as you are only displaying one instance of the form, you can always refer to MemoReasons.property because MemoReasons is a public object, just like ThisWorkbook or ActiveWorksheet, etc.
Instead, you should be able to refer to this object (MemoReasons is an object) in any subroutine, for example create another one that is not called from the previous subs. Run the sub to show the form, enter in some data, and then hide the form. With the form hidden, then run this subroutine, and you should see the resulting data from the form.
Sub Test2()
Debug.Print MemoReasons.EmailFlag
End Sub