Is there an easy way to determine what page in a tab is open to read the table in Access? - vba

I have a form with a tab control for payments. There is a separate page for Current, Future and Past payments each tab has a table that has a common field called ID.
I have a button that opens another form with more detailed info on the payment and uses the ID in a query to get the data.
Since each page uses/has the same ID is there an easy way to look up the ID regardless of which page is open?
I tried
ID = Forms!TabBills.Pages(TabBills.Value)![ID].Value

Tab control and its pages are irrelevant when referencing controls that sit on each page. Need to know the subform container name.
Value of tab control is index of page with focus. So use that value with Pages collection to grab Caption of that page. Assuming each tab control page has subform container control with a form as SourceObject and page Caption is same as name of subform container on that page consider:
strSubform = Me.TabBills.Pages(Me.TabBills.Value).Caption
intID = Me(strSubform)!ID
Any variation in naming may need If Then Else or Select Case structure.

Yes there is.
Have a function to look up the ID and call this from your button:
Private Sub YourButton_Click()
MsgBox GetCurrentID()
End Sub
Public Function GetCurrentID() As Long
Dim Control As Control
Dim CurrentID As Long
For Each Control In Me!YourTabControl.Pages(Me!YourTabControl.Value).Controls
If Control.ControlType = acSubform Then
Exit For
End If
Next
If Not Control Is Nothing Then
CurrentID = Nz(Control.Form!ID1.Value)
End If
GetCurrentID = CurrentID
End Function

I would do it with a more dynamic approach.
Helping procedure
This procedure tries to find a sub form control in a page.
If there are more then one sub form controls in a page it returns the first found.
Private Function FindSubformControlInPage(ByVal pageToCheck As Page) As SubForm
Dim item As Control
For Each item In pageToCheck.Controls
If TypeOf item Is SubForm Then
Set FindSubformControlInPage = item
Exit Function
End If
Next
End Function
Usage
Dim currentSubformControl As SubForm
Set currentSubformControl = FindSubformControlInPage(Me.TabBills.Pages(Me.TabBills.Value))
If currentSubformControl Is Nothing Then
MsgBox "No subform control in the current page"
Exit Sub
End If
If currentSubformControl.SourceObject = vbNullString Then
MsgBox "The current subform control doesn't contain a form."
Exit Sub
End If
Dim currentSubform As Form
Set currentSubform = currentSubformControl.Form
MsgBox "Found subform: " & currentSubform.Name
More compact usage
That means you're sure that there always is a sub form control and it contains a form.
Dim currentSubform As Form
Set currentSubform = FindSubformControlInPage(Me.TabBills.Pages(Me.TabBills.Value)).Form
MsgBox "Found subform: " & currentSubform.Name
Get the ID
Finally, having the correct (sub-)form you can access your ID field:
Dim currentID As Long
currentID = currentSubform.Controls("ID").Value

Related

Hide a Form tab depending on the value of a field

pretty simple question here in scope.
Question:
Wondering If I would be able to hide the tabs of a form based off the values of a table's fields.
I have been reading the 2019 Access Bible and so far it is still unclear to me how I would write the VBA module to constantly be running. Im asking the question a little early in my attempts, but hoping I can ask this well enough to get a head start.
I dont quite understand the execution model of VBA for access yet. I have prior expierence coding but this will be my 1st time in access. Just looking for a little help on how to write the function scope. You see below that I think it should be "Main" something, as I want it to run whenever in form view. Like I know how to write Private sub functions to respond to a button click. But not for a function that just runs in the background of the form.
I guess the real question is when to execute? Right? Any suggestions?
I was thinking something along the line of this below.
Main function()
If Me.FieldProcess_Water = "1" Then
Me.TabProcess_Water.Visible = True
Else
Me.TabProcess_Water.Visible = False
End If
End Sub
This requires some setup to reduce redundant code but should do what you want.
First you'll need your checkbox names and page names to be similar. In my example I have the page names as Tab_Name and the checkboxes as just Name. eg. Tab_Process Water and Process Water.
Then Create a sub called Form_Current() in the form's code-behind.
Private Sub Form_Current()
Dim chk As Control
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
If chk = False Then
'Change TabCtl0 to whatever your's is called
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = True
End If
End If
Next
End Sub
This will iterate through the current record's checkboxes and toggle it's respective tab's visibility.
To have the CheckBox update the tabs as they are clicked:
Private Sub Form_Load()
Dim chk As Control
Dim prop As Variant
For Each chk In Me.Controls
If chk.ControlType = acCheckBox Then
chk.OnClick = "=Check_Click()"
End If
Next
End Sub
This will assign a command to each checkbox.
Dim caller As String
caller = Me.ActiveControl.Name
If Me.ActiveControl.Value = False Then
Me.TabCtl0.Pages("Tab_" & caller).Visible = False
Else
Me.TabCtl0.Pages("Tab_" & caller).Visible = True
End If
This will hide the relevant tabs.

How to tell which dynamic control sent to an event?

This is my first attempt at working with dynamically created controls in a user form. The reason is there will always be a different amount of rows returned by some processing.
I have created a class object cControlEvent with the following code. (I cut out the code not pertaining to the checkbox)
Public WithEvents CHK As MSForms.CheckBox
Private Sub CHK_Change()
** tell me which box was changed **
End Sub
in the code module, I have the following code:
Dim CHK_Evts As New Collection
sub Form_Builder()
**non relevant code deleted****
Set Evt = New cControlEvent
If i_Columns = 1 Then
Set Evt.CHK = ctl
CHK_Evts.Add Evt
Else
** more code**
End if
end sub
What do I need to change/add to be able to get the name of the control that is firing off the change event?
EDITED TO ADD:
I have a series of dynamically created checkboxes and textboxes on each line of a user form, with a checkbox before each line, when the checkbox is checked/unchecked, I need to change the backcolor on all the textboxes in that row. Each control is named by it's type, then row then column like this CHX_1_1 would be a checkbox on row 1 column 1, and TXT_1_5 would be row 1 column 5. So, if I know what the name of the checkbox is, I have all I need to change the other controls on that row with a simple for-next loop.
I am not quite sure if I understand your question correctly. But it seems to me that it boils down to "which FormControl (linked to a particular procedure) caused this sub to run". If that's the case then you should be able to make use of the
Application.Caller
Here is a short video to demonstrate it's use in a very simple environment:
Here's hopefully a full solution showing how to get the properties from the check boxes:
Create a blank userform and add a command button to it.
Add this code to the form (note - CommandButton1_Click should be updated to the name of the button you added).
Public CHK_Evts As New Collection
Private Sub CommandButton1_Click()
Dim ChkBox As Variant
For Each ChkBox In CHK_Evts
MsgBox ChkBox.Position & vbCr & _
ChkBox.Status
Next ChkBox
End Sub
Private Sub UserForm_Initialize()
Dim tmpCtrl As Control
Dim cmbEvent As clsControlEvents
Dim X As Long
For X = 1 To 10
Set tmpCtrl = frmNameParser.Controls.Add("Forms.Checkbox.1", "Name" & X)
With tmpCtrl
.Left = 6
.Top = X * 20 + 24
.Height = 18
.Width = 150
End With
Set cmbEvent = New clsControlEvents
Set cmbEvent.CHK = tmpCtrl
CHK_Evts.Add cmbEvent, "Name" & X
Next X
End Sub
Create a class called clsControlEvents and add this code:
Public WithEvents CHK As MSForms.CheckBox
Public Property Get Position() As String
Position = CHK.Top
End Property
Public Property Get Status() As String
Status = CHK.Value
End Property
Private Sub CHK_Click()
MsgBox CHK.Name
End Sub
The two GET procedures pass information back to the CommandButton1_Click procedure so it can list information about all check boxes on the form (held in the CHK_EVTS collection).
The CHK_Click procedure gives immediate information about the check box being clicked.
http://www.cpearson.com/excel/classes.aspx

VB DropDown Read Only

I have a DropDown and a DropDownList on my form. I am aware that a DropDown can hold a placeholder text and a DropDownList cannot, however; I would like some code or a work around to allow either:
DropDown as read-only, therefore not allowing a user to type
But preferably a DropDownList with placeholder text (context menu option, or not)
Is this possible?
Thanks.
But preferably a DropDownList with placeholder text (context menu option, or not)
By definition, the text displayed in this control is always the text of the selected item. You can add a "fake" item to the list if you want (e.g. "Select Property Code"), but you will have to check that this item isn't selected later.
To display one of the items (fake or not), simply set the SelectedIndex to the appropriate value once the list is loaded.
DropDown as read-only, therefore not allowing a user to type
This will actively make sure that the text is either in the list or a default value, effectively making it read only. (Written for a ComboBox, but the behavior should be identical with a DropDown.)
Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
Static recursion As Boolean = False
Dim defaultText As String = "My Default Text"
If recursion Then
recursion = False
Else
If ComboBox1.Items.Count > 0 Then
For i As Integer = 0 To ComboBox1.Items.Count - 1
If ComboBox1.Items(i).ToString = ComboBox1.Text Then
Exit Sub
End If
Next
recursion = True
ComboBox1.Text = defaultText
End If
End Sub
Alternatively, here is a sub I call whenever a "strict" ComboBox looses focus to accomplish the same thing. The difference is that doing it this way allows you to keep the AutoComplete functionality:
Public Sub EnforceList(ByRef box As ComboBox) 'FORCES .TEXT TO BEST (OR FIRST) MATCH IN .ITEMS
'If list contains item whose name begins another item's, the shorter must be listed first, e.g. "sew" must preceed "sewer"
If box.Items.Count = 0 Then Exit Sub 'Can't enforce a list that doesn't exist
Dim txt As String = box.Text
Do
For i As Integer = 0 To box.Items.Count - 1
If box.Items(i).ToString Like txt & "*" Then
box.Text = box.Items(i).ToString
Exit Sub
End If
Next
txt = Left(txt, Len(txt) - 1)
Loop
End Sub

Updating Word Form Field

I have a Word form that I am working on.
It has one field that is supposed to be calculated from other fields.
In the prior iteration, you could click the cell in the table and hit F9 and the field would update.
I have since added some other buttons and VBA and now you can no longer click the cell when "Restrict Editing" is on.
I have tried a button tied to VBA that will update all fields, but when you click that button, you cannot edit any of the fields.
How can I update this field, and still be able to manually update my other fields?
The problem was that Content Controls and ActiveX buttons are not altogether compatible. Also, the programmer I had inherited the form from was using a simple field calculation based on the table in the document instead of VBA. I was able to come up with a better solution. I used the sub:
Private Sub Document_ContentControlOnExit(ByVal thisControl As ContentControl, Cancel As Boolean)
End Sub
to execute code onExit from the controls. This function executes on ALL Content Controls as the user exits the Content Control. Another tool I developed was the following function which will find the index of the control with the given title:
'Function to get control index given the control title
'PARAMETER Control Title as String
'RETURN Control Index as Integer
Public Function GetControlIndex(ccTitle As String) As Integer
'Function Variable Declaration
Dim objCC As ContentControl
'look at each ContentControl
For i = 1 To ActiveDocument.ContentControls.count
Set objCC = ActiveDocument.ContentControls.Item(i)
With objCC
If .Title = ccTitle Then
GetControlIndex = i
End If
End With
Next i
End Function

SubForm won't Requery after MainForm Changes are made

I have an access 2010 database that has a main form 'MainForm' and a subform 'SubForm'. The SubForm is attached to the MainForm as a Subform/Subreport object. The user will select a unique identifier from a dropdown and the subform should use that identifier to pull up employee information on the subform. I have tried any number of ways to avail...
Private Sub Dropdown_Exit(Cancel As Integer)
If IsNull(Me!Dropdown) Or Me!Dropdown= "" Then
' nothing to do due to no one selected
Else
Forms!MainForm!SubForm.Requery
' Forms!SubForm.Requery
' DoCmd.OpenForm "SubForm",,,"[ID]=" & me!SubForm!ID,,acDialog
End If
End Sub
The commented out statements are only some of the things I have tried.
Thanks in advance
You should be able to do this without any code by specifying the LinkMasterField and LinkChildField properties of the subform control on your main form.
It is clear that LinkChildField should be set to ID in the form design mode. It looks like you'll want to set LinkMasterField to Dropdown. You can set the FilterOnEmptyMaster property to Yes to hide all records before the Dropdown is filled, or No to show all records before Dropdown is specified.
EDIT:
If LinkMaster/LinkChild are not appropriate, then code for Dropdown's AfterUpdate event. This fires after a choice is completed via keyboard or mouse. It should look like :
Private Sub Dropdown_AfterUpdate()
If Len(Me!Dropdown & "") = 0 Then
'' handle cleared Dropdown
Else
Subform.Form.Filter = "[ID] = " & Me!Dropdown
Subform.Form.FilterOn = True
End If
End Sub
Changing the filter should update the subform.