Dear Community
Needing some assistance. Before I start, I wouldf say that I am self taught on the basics of VBA - primarily in excel. But I am creating a database with forms in Access and there is one thing I cannot get to work. So I am posting here in the hope that
someone can help me. But please bear in mind I have had no formal vba training, so if I get lost with some termonology, please help me along.
So the issue.
I have a database table (Main_Table) that is being updated via a split form. all is well with the simple data entry. But, there are some fields that can be calculated from entries in other fields on the system. The calculated field option is not warranted
as the build expression module only has some calculations. This needs something more complicated.
Before I set out the code I have, I will say that I have read about data normalisation - and some may say that I should just store this data in a query and / or combine the grade fields into one field. However, that will not quite do it. That is because
while the database will be used for data entry, other team members will also have access to the tables to create ad-hoc queries from. They are needing fairly straight forward tables and links between tables - so I'd rahter not have to ask them to then link
those in with queries. So I do need the main table records to update.
What I have done so far
So - in the main table, as well as a unique ID (for use a "Curve") there are tons of other fields. But in this instance, the 3 fields that are of interest are "Quantitative Grade", "Qualtitative Grade", and "Overall Grade". The three grade fields are "number
fields".
- Within each record the "Overall Grade" is calculated as a max of the entries in "Quantitative Grade" and "Qualtitative Grade". So if "Quantitative Grade" = 2 and "Qualtitative Grade" = 3, "Overall Grade" = 3.
- But if instead "Quantitative Grade" = 4 and "Qualtitative Grade" = 1, then "Overall Grade" = 4.
Basically - after each record is updated, I need overall grade to recalculate.
First things is first - I have found code on the internet to create a Function that will get the max value from 2 fields:
Option Compare Database
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
End If
End If
Next
MaxOfList = varMax
End Function
I have set that up - and called it in an update query:
UPDATE Main_Table SET Main_Table.Overall_Grade = MaxOfList([Quantitative Grade],[Qualitative Grade]);
That update query works independently - and is my backstop. I may just run the query regularly from a button. But I'd prefer it to update each record as and when a record is filled in (probably after you leave the record, as I understand in-record updates
will cause issues for new records that have null values.)
The way to do this seems to be by adding an "After update" event to the control on the form - for both the "Quantitative Grade" and "Qualtitative Grade" fields - to run the function above to update the "Overall_Grade" field.
I tried two alternatives - see below
Public Sub Qualitative_Grade_AfterUpdate()
UPDATE Main_Table SET Main_Table.Overall_Grade = MaxOfList([Quantitative Grade],[Qualitative Grade]);
End Sub
Public Sub Quantitative_Grade_AfterUpdate()
Main_Table.Overall_Grade = MaxOfList([Quantitative Grade], [Qualitative Grade])
End Sub
Neither works.
Can anyone see what I am missing. Or send me to agroup that may know what to do to resolve?
Thanks in advance.
Jonathan