combobox cascade effect not working for different rows - ms-access

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.

Related

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"

Refresh calculation after combobox selection?

I have a drop down combo box, and I also have a text box that has a Macro that does a DLOOKUP then throws the result into the blank text box. This part is working decently enough.
What's frustrating is that it doesn't update the result text box. I need to go back and forth on a record to see the change.
Of course, what I want is for the macro to run as soon as the Combobox is selected then update the result text box with the new number. I'm sure this is easy to do but I just can't figure it out.
Thanks!
Edit:
I've put the following code in my After Update event...
Sub updateDM()
KeyID = Me!DMID.Value 'vDMID.Value
If IsNull(KeyID) = False Then
AssignedPlan = DLookup("AssignedPlan", "DMPlanSum", "DMID = " & KeyID)
vAssignedPlan.Value = AssignedPlan
Me.Refresh
End If
End Sub
Private Sub vStartMonth_AfterUpdate()
updateDM
End Sub
Weird thing about it is that it kindof works, but only if I do the drop down TWICE. For example, let's say I'm on plan A = result 100 (starting selection). I choose Plan B = 200, but result box still has 100. I select Plan B again, result box now changes to 200.
On the other hand, if I change back to Plan A after choosing Plan B, it still changes to 200!
It seems like it's one step behind??
remove that code from form_update and stick it to the combobox_before/after_update event.
just assign the value directly to the textbox.
Private Sub ComboBox_AfterUpdate()
me.txt_resultBox.value = nz(DLookup("AssignedPlan", "DMPlanSum", "DMID = " & ComboBox.value),"")
End Sub

autopopulating text boxes from combo boxes

I have created a form to input new work orders in access. I want the text boxes in my form to populate from the selection in the combo box.
The combo box lists serial number for equipment, and once a serial number is selected, I want CompanyName, PurchDate, LastMaintenanceDate, & ModelNo to populate.
I saw an example of dlookup and tried this expression on the Company Name textbox:
=DLookUp("[CompanyName]","SerialNoInfo","[SerialNoInfo].SerialNo= " & Str([Combo341].[Text]))
I keep getting an error #Type or #Error in the box depending on what I do. When I try to build a code for the event on update, nothing happens at all. I have no idea what I am doing wrong.
You can alternatively use :
Private Sub COMBOBOX_Change()
me.TEXTBOXNAME.Text = me.COMBOBOX.Column(X)
End Sub
Where TEXTBOXNAME is the the textbox you want to fill
Where COMBOBOXis the the combobox from which you want to get data
and X is the number of column with data (starting from 0 )

cascading combo box causing empty fields in next record

I'm having problems with a cascading combo box. Everything works fine with the combo boxes and the values get populated correctly.
Private Sub cmbAdjComp_AfterUpdate()
Me.cboAdjOff.RowSource = "SELECT AdjusterCompanyOffice.ID,
AdjusterCompanyOffice.Address1,
AdjusterCompanyOffice.Address2,
AdjusterCompanyOffice.Address3,
AdjusterCompanyOffice.Address4,
AdjusterCompanyOffice.Address5 FROM" & _
" AdjusterCompanyOffice WHERE
AdjusterCompanyOffice.AdjCompID = " & Me.cmbAdjComp.Column(1) & _
" ORDER BY AdjusterCompanyOffice.Address1"
Me.cboAdjOff = Me.cboAdjOff.ItemData(0)
End Sub
The secondary combo box has a row source query:
SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1,
AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3,
AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM
AdjusterCompanyOffice ORDER BY AdjusterCompanyOffice.Address1;
Both comboboxes have the same controlsource.
Everything works fine and dandy moving between records and the boxes show the correct fields for each record.
When i use the first combo box, and then select the appropriate option in the second combo box, everything works great on the specific record.
However when I move to the next record, the values in the second combo box are all empty. If i close the form and reopen it, and avoid using the cascading combo boxes all the values are all correct when i move between records.
Somehow using the cascading combo boxes creates a conflict with the row source of the secondary combo box.
Hope that is clear! Have been rummaging around for an answer but cant find anything.
any help would be greatly appreciated.
Thanks
Noel
The reason why the combo box is blank when you navigate to the next record is because you have NotInList set to TRUE (which is what you want), but when you arrive on the record, the rowsource has been filtered to not include the value stored in the field the combo box is bound to. Thus, it's blank -- the value is there, but it can't be displayed, since it's not in the list.
To fix this, you need to clear the filter on the second combo box. To do that, in the OnCurrent event of your form, set the rowsource of the filtered combo box to be unfiltered:
Me!cboAdjOff.RowSource = "SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1, AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3, AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM AdjusterCompanyOffice ORDER BY AdjusterCompanyOffice.Address1"
I usually handle this by creating two constants at the top of the form's module, one for the SELECT statement and one for the ORDER BY:
cstrRecordsourceSelect = "SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1, AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3, AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM AdjusterCompanyOffice"
cstrRecordsourceOrderBy = "ORDER BY AdjusterCompanyOffice.Address1"
Then it's much easier to handle. In the OnCurrent it looks like this:
Me!cboAdjOff.RowSource = cstrRecordsourceSelect & " " & cstrRecordsourceSelect
...and in the AfterUpdate of your first combo box:
Me!cboAdjOff.RowSource = cstrRecordsourceSelect & _
"WHERE AdjusterCompanyOffice.AdjCompID = " & Me!cmbAdjComp.Column(1) & _
" " & cstrRecordsourceSelect
This makes the code easier to read, and it also makes it easier to alter the rowsource if you need to, since you have to edit only the constant.