SubForm won't Requery after MainForm Changes are made - sql

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.

Related

Pass values ​from a subform to a textbox in another access form

I would like to create an invoice in a form, pressing a button from another form with the data. Pressing the button would pass the customer data, dates etc. into different textboxes. But the detail of the purchase, the units, the number of invoices, etc. are in a subform. There is no problem to pass the first item to a textbox in the invoice form. But the issue is that there are several items.
This is my code in the button
Private Sub Boton_Click()
Dim strFrmName As String
strFrmName = "FACTURAR"
DoCmd.OpenForm strFrmName
With Forms(strFrmName)
.RUT_2 = Me.RUT
.REGIS_RECP_2 = Me.registroRecepcion
.NAME_2 = primerNombre & " " & primerApellido & " " & segundoApellido
.BOLETA_2 = Me.BOLETA_INV
.CANT_2 = Me.CANT_INV
'The problems in the subform and thier data...
End With
End Sub
I have tried to pass subform1 to subform2 but nothing works, I have created a vlookup to search on the subform's source table using outside sharing references (placed in the mainform) but it didn't work. Any idea? It's possible?

How do I hide/unhide a subform based on the input from two comboboxes?

I have a mainform, called TrainingsSU
In it I am calling subform qry_TrainingSU (built from the query of the same name)
I have it set up so that the records auto populate the subform based on the two comboboxes in the main form.
But what I need to do is hide the subform, and have it display only when the two comboboxes are populated and records are loaded.
Here's the current VBA (of which I am not really even a novice at)
Private Sub cbo_EmployeeLookup_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_EmployeeLookup.Value = "" Then
Forms!qry_TrainingsSU.Visible = False
Else
Forms!qry_TrainingsSU = True
End If
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_TrainingName.Value = "" Then
Forms!qry_TrainingsSU = False
Else
Forms!qry_TrainingsSU.Visible = True
End If
End Sub
I found the general form of this code in another answer, here: MS Access: Hide and Unhide subform based on drop down in Main Form
However the code doesn't seem to be working for me.
Currently the subform is set to Visible:No
So nothing shows up at all.
If I change that, it doesn't disappear and the empty subform is still visible.
This wouldn't be a problem, except I need to use this form for another query and want to layer them over eachother when the second subform is ready to be used.
Later this form will be used to push an UPDATE SET to a table based on the different subforms.
Is there something that is obviously wrong with the code, or did I miss a setting somewhere?
You can try this:
Private Sub updateStates()
Me!qry_TrainingsSU.Form.Requery
If (Me.cbo_EmployeeLookup.Value <> "" AND Me.cbo_TrainingName.Value <> "") Then
Me!qry_TrainingsSU.Visible = True
Else
Me!qry_TrainingsSU.Visible = False
End If
End Sub
Private Sub cbo_EmployeeLookup_AfterUpdate()
updateStates
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
updateStates
End Sub
Forms!qry_TrainingsSU searches form opened as main form, not a subform.

Disable a subform while editing data in another subform

I have two subforms, the first is a list and the second is a detail record of the record selected in the first subform.
When I select the edit button in the detail subform I want to lock out the list form.
I tried: forms!master!subformList.enabled=false.
Note:
_D is the detailed subform.
_L is the list subform.
cmb_Edit is the edit/save button located on detail subform.
Private Sub EnableFutureForm(EF)
Select Case EF
Case 1 'SubForm Detail is Locked Down
FuturePaint (False)
Forms!FrmFutureWork!FrmFutureWork_D!cmb_Edit.Caption = "Edit"
Forms!FrmFutureWork!FrmFutureWork_L.Form.Enabled = True
Case 2 'SubForm Detail is Opened up
FuturePaint (True)
Forms!FrmFutureWork!FrmFutureWork_D!cmb_Edit.Caption = "Save"
Forms!FrmFutureWork!FrmFutureWork_L.Form.Enabled = False
Forms!FrmFutureWork!FrmFutureWork_D!cmb_TRMM.SetFocus
End Select
End Sub
I want the list subform to not respond to any click, while the Form_Click continues to work.
You can disable events by setting the forms property.
ForForm_Clickit is Form.OnClick property.
'disable event
Forms!FrmFutureWork!FrmFutureWork_L.Form.OnClick = vbNullString
'enable event
Forms!FrmFutureWork!FrmFutureWork_L.Form.OnClick = "[Event Procedure]"
Or you can add a check to the events-code:
Private Sub Form_Click()
If Forms!FrmFutureWork!FrmFutureWork_L.Enabled then
' Actions on subform-control (not the form!) enabled
Else
' Action on subform-control disabled
End IF
End Sub

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

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

Copy record from previous field if specific combobox value

I have an access form that needs filling in daily by various people.
It's to document changes to a website and I currently have a combobox box set up for the various sections to state whether they are AMENDS, REVERTS or NO CHANGE.
I have set conditional formatting to then highlight these sections but am also trying to get it to work so that if the user chooses "NO CHANGE" then the data for that field copies over from the previous record.
I have set this up in the AfterUpdate code for the combobox, but nothing is happening, not even an error... can anyone help?
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=Forms![form_FORM]![ID]-1")
End If
End Sub
(Where 3 is the value of NO CHANGE in the combobox, and FIELD_TO_CHANGE, tb_TABLE and form_FORM being the names of the various elements)
Thanks!
First you should define your control COMBOBOX1, enter in Properties Window, define
COMBOBOX1.AfterUpdate = "[Event Procedure]"
then your Private Sub COMBOBOX1_AfterUpdate() will be taken into account. Error may occur and popup to you.
Then change the event handler like this to start:
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=" & (Me.[ID] - 1))
End If
End Sub
There will be many errors to correct before your form works...