MS Access: ComboBox Requery - ms-access

I have a ComboBox which will display data which dependent on what has been entered in another ComboBox. The second ComboBox is not updating (showing) data in the dropdown after the first has been updated...
Company_Select_Combo is the first ComboBox. After a selection has been made in this combobox, Marketer_Select_Combo becomes visible on the form, and this query should reflect those marketers associated to the company selected in the first combo...
Help
Private Sub Company_Select_Combo_AfterUpdate()
If Company_Select_Combo > 0 Then
Company_Select_Combo.Enabled = False
Marketer_Select_Combo.Visible = True
Me.Marketer_Select_Combo.Requery
Else
Marketer_Select_Combo.Enabled = False
End If
End Sub

Let assume your "Marketer_Select_Combo" query includes:
Marketer_ID, Marketer_Name, and Company_ID
Then simply add the following statement to your SQL:
WHERE Company_ID = [Forms]![yourformname]![Company_Select_Combo]
This way, the elements should be limited to the chosen company. By calling the Requery/Recalc method as you did, it should work as intended.

Related

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

My code is working, but I just want to know if there is a more efficient way to achieve the same effect.
I have this layout in a form:
In my effort to foolproof the record creation process, I would like to have the "Save and Clear fields" button enabled only after all but the 'Comment' textbox/combobox contains some valid data.
The text/combo boxes are called txtBatteryID, cmbModelNumber, cmbChemistryType, txtSpecVoltage, txtSpecCapacity.
My code is as follow
Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
If Me.btnSaveAndCLear.Enabled = False Then
If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
Me.btnSaveAndCLear.Enabled = True
End If
End If
End Sub
As you can see, I did the most straightforward way of using AND to combine all must-have conditions in an IF statement. This sub is called in After_Update() event of each text/combo box. Like this:
Private Sub cmbChemistryType_AfterUpdate()
Call EnableSaveBtnCheck
End Sub
My question, in the end, is: Is there a more efficient way to setup the condition "all text/combo box need to have something valid in them"? And is there a more elaborate way to check if the condition is met (something like a event on the form itself)?
Add the values of those 5 fields. If any of them is Null, the sum will be Null. So you only need call IsNull() once.
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

VBA enabling text and combo box in MS Access

I'd like to make a text box (Text28 hereafter) and a combo box (combo43 hereafter) enabled when another combo box (Combo26) takes the value "Resolved"
So this is the code, written in VBA, that should do that :
Private Sub Combo26_AfterUpdate()
If Combo26.Value = "Resolved" Then
Me.Text28.Enabled = True
Me.Combo43.Enabled = True
Else
Me.Text28.Enabled = False
Me.Combo43.Enabled = False
End If
End Sub
Unfortunatly, nothing happens when I try it. Would you have a idea ?
Thx
If you are pulling two columns as your row source (as indicated in your comment above), it looks like the first one that is displayed is the ID. You should target the second column to do your comparison, e.g.,
If Combo26.Column(1) = "Resolved" Then
...
That works for me!
Combo26.Column(0)
will give you the ID.

Error 424 when trying to get a value form combobox in ON Activate event

I have been trying use the value in the combo box however the combobox will be locked if the revision num > 0
it gives me an error when i put it in the ON ACTIVATE event
i got the error 424 on Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Private Sub Form_Activate()
Me.Requery
' prevents user from changing the client, chemical, active name once the
revision number has become 1 or more
If Me.txtRev.Value > 0 Then
Me.cboClient_Name.Locked = True
Me.cboChemical_Name.Locked = True
Me.cboActive_Name.Locked = True
Me.cboActive_Name.Requery
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2).Value
Me.txtTTC.Value = Me.cboActive_Name.Column(3).Value
End If
End Sub
Is there anyway i can solve this problem?
When you requery the combobox, it looses its value (if it is unbound as it seems), and then neither column(1) has a value.
So after the requery, set the combobox to some value - or rethink your concept.
Edit:
Property Column(n) carries no (sub)properties, thus:
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1)
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2)
Me.txtTTC.Value = Me.cboActive_Name.Column(3)

ms access vba code I can disable a field based on content of another field, but my code to re-enable is not working

I have a simple form where I have a field InstrumentTypes_ID and a field DGLenders_ID.
When InstrumentTypes_ID equals "Mortgage" or "Modification", I want DGLenders_ID to stay enabled, but any other value should make DGLenders_ID become disabled.
I have the following code in an AfterUpdate for the InstrumentTypes_ID textBox, which to me seems would be sufficient to get this effect (However, when testing, I can type in "Warranty Deed" (or anything that's not "Mortgage" or "Modification"), which disables the DGLenders_ID field, but if I go back and update it with "Mortgage" or "Modification", it won't re-enable that field):
Private Sub InstrumentTypes_ID_AfterUpdate()
Me.DGLenders_ID.Enabled = True
If Me.InstrumentTypes_ID = "Mortgage" Then
Me.DGLenders_ID.Enabled = True
ElseIf Me.InstrumentTypes_ID = "Modification" Then
Me.DGLenders_ID.Enabled = True
Else
Me.DGLenders_ID.Enabled = False
End If
End Sub
You should try to put your code in a subroutine and call it on the current event of the form and in the lost focus event of the field.

MS Access runtime error 2115

In Ms Access, I have two unbound combo-boxes: StateBox and DVPCBox. StateBox is simply a list of U.S. states and DVPCBox contains Employee Names from a query based on the value of StateBox.
I'm trying to set the value of DVPCBox equal to the first item in its list. Since the list of Employees is based on the value of StateBox, I need the value of DVPCBox to update every time StateBox changes. I tried the following:
Private Sub StateBox_AfterUpdate()
Me.DVPCBox.Requery
If (Me.DVPCBox.ListCount = 1) Then
Me.DVPCBox.SetFocus
Me.DVPCBox.ListIndex = 0 //<-Error here
End If
End Sub
But I got runtime error 2115 - The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field.
The strangest thing to me is that I'm not even using the BeforeUpdate Event or ValidationRule (as far as I'm aware.)
ItemData(0) is the first combo box value. So set the combo equal to that.
Private Sub StateBox_AfterUpdate()
Me.DVPCBox.Requery
If (Me.DVPCBox.ListCount >= 1) Then
Me.DVPCBox.SetFocus
'Me.DVPCBox.ListIndex = 0 //<-Error here
Me.DVPCBox = Me.DVPCBox.ItemData(0)
End If
End Sub
I also changed ListCount >= 1 because I assumed you wanted to do the same thing when the combo includes 2 or more rows.