Access - Show values from subform in texboxes - ms-access

I have a form "frm_Results" with subform "subfrm_shv_Results" and three textboxes ("txt_Number", "txt_Name", 'txt_Surname").
I can show values of selected record in Debug.Print or MsgBox:
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Number.Value
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Name.Value
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Surname.Value
But it is possible to show values of "Number", "Name" and "Surname" in textboxes after click on record in subform "subfrm_shv_Results"?

Yes, you can do this, with a query as the contained object, you need to refer to the controls property, for example, you can set the control source of a textbox to :
= [Forms]![frm_Results]![subfrm_shv_Results].form.controls(0)
Similarly, with a continuous form or such like :
= [Forms]![frm_Results]![subfrm_shv_Results].Form!Surname
The result is not editable, but you can get around this by setting the value in VBA.
Make sure you use the name of the subform control, not the the name of the form contained.

Related

Hide/unhide combobox based on a text box

I have a form in access that works in this way:
User select an ID of a product
VBA code searches in the database and fill some textboxes in the form.
For example:
User select ID 1
Textbox 1 is filled with: fruit
My problem is, i have a combobox with options to select like "banana" or "apple" or "grape". This combobox is visible at the form, but i want it to be invisible and only appears if the textbox 1 is filled with "fruit".
In the design view, i set the combobox's visibility to "no", so it does not appear anymore in the form. Then, i wrote this code:
Private Sub textbox1_AfterUpdate()
If Me.textbox1 = "fruit" Then
Me.LabelOfTheCombobox.Visible = True
Me.Combobox.Visible = True
Else
Me.LabelOfTheCombobox.Visible = False
Me.Combobox.Visible = False
End if
End Sub
But it doesnt work, when the textbox is filled with "fruit", the combobox remains invisible.
Can you help me fix this code?
Everything looks OK. From what you have said, that putting "fruit" in the textbox doesn't cause the combobox to display, then my guess would be that the If-test is failing. Perhaps you could try trimming both sides of the textbox before the comparison. You should also pay attention to the case. Try forcing the case of the textbox to lower case to match the string constant.

How to pass value from subform to mainform ms access

I have a form with a subform (Datasheet view form). When i click a row in subform, i want pass that row value to text box in main form . In subform even wrote this
Private Sub Form_Click()
MsgBox (Noloan)
End Sub
But i don't know how to pass that subform form_click no_loan to mainform.textbox. Thanks for your help.
Additional data:In my db , i have a table which consist field No,NoLoan,Name. NoLoan Field value as i wanna clicked .From that table i create a subform/datasheet view ,subform name is T_Loan1. Then I create Main Form, name is FindLoan2. In that main/parent form i create text box, called Text7, and put T_Loan1 in subform object at footer.
I put this
=[Forms]![FindLoan2]![subformTLOAN].[Form]![Noloan]
in my Text7 Control and It's work.
Thank's for all attention.
Goto to the subform and click on the field which has the row value
Select Field property - click on events tab --> On Click --> Code Builder
Put below code in the Click event where Form_frmMain is your form name and Me.ID is the field.
Private Sub ID_Click()
Forms!FindLoan2.Text7 = Me.ID
End Sub
The best way is to write code that ignores the forms!names. If you copy the form, or change the sub form, or even drop the sub form into a different form, then all your code breaks.
To reference the textbox in the parent form from code, use:
me.Parent!Text7 = me!id
I am not sure which version of MS Access you are using, but it is also possible to use Tempvars to store values for use elsewhere if you have a complicated application which makes referencing another form/field/control difficult.
To create:
Tempvars.Add "TempVarName", TempvarValue
To use:
Tempvars!TempVarName
To remove:
Tempvars.Remove "TempVarName
I try to limit my use of them, as it becomes hard to manage. I am always careful about when I set the values and I ensure that they are removed after I am finished with them to avoid confusion or issues with values persisting when they shouldn't.

Display Issue for filtering a combobox in a continuous form

I have a continuous form containing many records. Each record contains a combo box that has vba code filtering the viewable selections when it gains focus (it does this by changing the RowSource.
My problem is that if i click the combo box in record A and then click the combo box is record B, the display-filter for record B is applied to record A. I do not have this problem if I click another control in between clicking the combo boxes.
Why is the filter applied to A's combo box even though focus has (supposedly) been lost? And how do I prevent this display error from happening? The only solution I have now is to make an arbitrary control gain focus when focus for the combobox is lost, but obviously that can be annoying for the user...
Possibly relevant code (SuperintendentID is the name of my combobox):
Private Sub SuperintendentID_GotFocus()
'Filters SuperintendentID based on the task's division
Me!SuperintendentID.RowSource = "SELECT tblJoinDivisionSuperintendent.SuperintendentID, [FirstName] & "" "" & [LastName] AS Name, tblJoinDivisionSuperintendent.DivisionID FROM tblSuperintendent RIGHT JOIN (tblDivision RIGHT JOIN tblJoinDivisionSuperintendent ON tblDivision.ID = tblJoinDivisionSuperintendent.DivisionID) ON tblSuperintendent.ID = tblJoinDivisionSuperintendent.SuperintendentID WHERE tblJoinDivisionSuperintendent.DivisionID = " & Me!DivisionID & ";"
Me!SuperintendentID = Me!SuperintendentID.ItemData(0)
Me!SuperintendentID = Me!SuperintendentID.OldValue 'Prevents a new value from being assigned when the control first gains focus
End Sub
You might be firing both the GotFocus events for comboboxA and comboboxB, but the screen is only showing you the last value you changed.
You could add use a Form.Repaint method (where Form=your form object) as part of the GotFocus event to ensure that the screen was refreshed after changing the underlying datasource.
You could test this to make sure both events are firing by putting a debug.print statement in the event as well, such as debug.print "GotFocus ComboboxA"

Control a subform from main form

I have a main form Form_frmSaleand it contain a sub-form frmSale_subform.How can I "access" the fields in subform
For e.g with no subform i would do Sale_Date.Enabled = True
What code should i put if i want to control subform fields from main form(Suppose Sale_Dateis in a subform
I tried the following but its not working
Me.frmSale_subform.Sale_Date.Enabled = True
frmSale_subform.Sale_Date.Enabled = True
One way you should be able to do that is using:
Me.frmSale_subform.Controls("Sale_Date").Enabled = True
If that doesn't suite your fancy you can also reference http://access.mvps.org/access/forms/frm0031.htm for other ways to do it.

combobox cascade effect not working for different rows

I have two comboboxes on a subform.The first combobox is used to populate the second combobox. These are placed in the detail section of the form. I want them to work this way:when I select any value from the first combobox, I want the second combobox of the same row to get populated by relevant value.
As of now, I have tried to implement this and as I select any value from the first combobox of row 1 I see the second combobox of the same row gets populated but as I go on selecting values from the first set of comboboxes I see that the values in the second set of the comboboxes above changing or becoming null.
Here's the code:
The 1st combobox is cboRCMTask:
Private Sub cboRCMTask_AfterUpdate()
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
Me.cboRCMTaskOptions.Requery
End Sub
cboRCMTaskOptions is the second combobox.
The form_current event:
Private Sub Form_Current()
Me.cboRCMTask.RowSource = "SELECT ID, RCMTask FROM tblRCMTask;"
If IsNull(txtRCM_ID) Then
Me.cboRCMTask = Me.cboRCMTask.ItemData(0)
End If
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
If IsNull(txtRCMOption_ID) Then
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
End If
End Sub
From your description, you are using a continuous form. While it looks like a continuous form has many rows, from the point of view of coding you can consider it to have just one row, the current row. I suspect that the control source for combo 2 is a hidden, numeric column in the combo, when you change the row source for the combo, the visible row can no longer be found, so it cannot be displayed. You will either have to provide a pop-up form for editing, or a textbox to store the value for the form and a slightly dodgy combo to edit that value. You can do a little to improve the appearance with conditional formatting.
in your first piece of code, shift the code to the on_click event. I am not sure what you are trying to achieve with the reference to ItemData but I think it is unnecessary, comment out that line.
Similarly the third to last line in the Form_current event, replace with a requery.