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.
Related
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.
I have 3 combo boxes that contain numbers:
Me.Authorized
Me.Officer
Me.Enlisted
What I'm trying to do is add the values of Me.Officer and Me.Enlisted and make sure it equals Me.Authorized. I have all the other statements/commands figured out, but I can't wrap my head around this one.
The combo box selected value is a string, even when that string contains only digits. You can use Val() to convert that string to a number.
So your required condition can be expressed as ...
Val(Me.Officer) + Val(Me.Enlisted) = Val(Me.Authorized)
You can enforce that requirement in the form's Before Update event ...
Private Sub Form_BeforeUpdate(Cancel As Integer)
If (Val(Me.Officer) + Val(Me.Enlisted) <> Val(Me.Authorized)) Then
MsgBox "Officer plus Enlisted must be equal to Authorized."
Cancel = True
End If
End Sub
That event procedure will abort a record save when your requirement is not satisfied.
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)
So the first thing is that every table has 4 fields. Created_By, Created_Date, Last_Updated_By and Last_Updated_Date. This will allow me to keep a track of who saved what records and when. I want a public sub (or function?) to update these fields when any record is saved.
Ideally I don't want to have to copy/call my code from every form, but I will if that's what is necessary. I imagine there is a way of doing this globally.
I currently have the following:
Public gLoggedIn As Integer
Public Function get_global(Global_name As String) As Variant
Select Case Global_name
Case "Employee_ID"
get_global = gLoggedIn
End Select
End Function
My next thing is to add vba code to fill the 2 of the 4 fields. The following is something I have in one of my forms, but like I say, I want to able to achieve this globally on every save.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord = True Then
Me.[Created_By] = gLoggedIn
Me.[Created_Date] = Now()
Else
Me.[Last_Updated_By] = gLoggedIn
Me.[Last_Updated_Date] = Now()
End If
End Sub
How can I do this globally?
I'm not sure if 'me' is the correct reference to us if this is global?
Exactly for your demand, you could use a VBA modul with following sub
Public Sub setTimestamp(objForm As Form)
If objForm.NewRecord = True Then
objForm![Created_By] = gLoggedIn
objForm![Created_Date] = DateTime.Now
Else
objForm![Last_Updated_By] = gLoggedIn
objForm![Last_Updated_Date] = DateTime.Now
End If
End sub
You need to call this method in every form when the Timestamp Update should be fired (BTW: I would recommend an event when a save button is clicked or field update, because an edit in the dataset of the form at form update event will probably cause another form update event and could lead to an endless loop).
call setTimestamp(Me)
But you should alternatively consider using a macro like #E Mett said in comment.
Another hint: If you set the variable gLoggedIn to public, you can refer directly without using a function. (You did this even in your example) A function makes sense when you want to hide the variable inside a modul as private. This is called encapsulation, if you want to learn more about it.
EDIT
Changed code by andre451 ' s suggestion.
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.