I have a query "salary total"(2 tables) , I would like to create a function
to calculate the salary but it does not work, code as follows. Appreciate for
your expert help!
Function dailysalary()
Dim mysalary As Single
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("salary total")
Set rst = qdf.OpenRecordset
Do
If rst![work details]![START TIME] > 8 And rst![work details]![START TIME] <
13 And rst![work details]![END TIME] > 13 And rst![work details]![END TIME] <
19 Then
mysalary = (13 - rst![work details]![START TIME] + rst![work details]![END
TIME] - 14) * rst![work details]![SALARY] / 8
ElseIf rst![work details]![START TIME] > 8 And rst![work details]![END TIME]
< 14 Then
mysalary = (rst![work details]![END TIME] - rst![work details]![START TIME])
* rst![work details]![SALARY] / 8
ElseIf rst![work details]![START TIME] > 13 And rst![work details]![END
TIME] < 19 Then
mysalary = (rst![work details]![END TIME] - rst![work details]![START TIME])
* rst![work details]![SALARY] / 8
ElseIf rst![work details]![START TIME] > 8 And rst![work details]![START
TIME] < 13 And rst![work details]![END TIME] > 13 And rst![work details]![END
TIME] > 19 And rst![work details]![END TIME] <= 24 Then
mysalary = (13 - rst![work details]![START TIME] + 18 - 14) * rst![work
details]![SALARY] / 8 + (rst![work details]![END TIME] - 18) * rst![work
details]![SALARY] / 8 * 1.5
ElseIf rst![work details]![START TIME] > 8 And rst![work details]![START
TIME] < 13 And rst![work details]![END TIME] > 13 And rst![work details]![END
TIME] > 19 And rst![work details]![END TIME] > 24 Then
mysalary = (13 - rst![work details]![START TIME] + 18 - 14) * rst![work
details]![SALARY] / 8 + (24 - 18) * rst![work details]![SALARY] / 8 * 1.5 +
(rst![work details]![END TIME] - 24) * rst![work details]![SALARY] / 8 * 2
ElseIf rst![work details]![START TIME] > 13 And rst![work details]![END
TIME] > 18 And rst![work details]![END TIME] <= 24 Then
mysalary = (18 - rst![work details]![START TIME]) * rst![work
details]![SALARY] / 8 + (rst![work details]![END TIME] - 18) * rst![work
details]![SALARY] / 8 * 1.5
ElseIf rst![work details]![START TIME] > 13 And rst![work details]![END
TIME] > 18 And rst![work details]![END TIME] > 24 Then
mysalary = (18 - rst![work details]![START TIME]) * rst![work
details]![SALARY] / 8 + (24 - 18) * rst![work details]![SALARY] / 8 * 1.5 +
(rst![work details]![END TIME] - 24) * rst![work details]![SALARY] / 8 * 2
Else
mysalary = 0
End If
rst.MoveNext
Loop Until rst.EOF
dailysalary = mysalary
End Function
beyond that, i'm lost on where you're getting rst![work details]![START
TIME]. presumably [START TIME] is a field in query "salary total", which is
the query your recordset is based on. but what is [work details] referring
to?
hth
"Andy" <An...@discussions.microsoft.com> wrote in message
news:F5A0B87A-083B-46EF...@microsoft.com...
Please ingore the "[work details]" (I have removed) and exactly what you
said the value of dailysalary is always going to equal the value of mysalary
*from the last record in the recordset*.
I would like the value of dailysalary is going to equal the value of
mysalary *from EACH record in the recordset* show in the query "salary total"
but I do not know what I can do?? Very appreciate for your expert advise!!!
Thanks again!
*how are you going to use each value* as you retrieve it from each record?
if you're going to do something with it, then you must do it within the
loop, NOT after the loop is completed.
hth
"Andy" <An...@discussions.microsoft.com> wrote in message
news:4D24F624-928F-4EF5...@microsoft.com...
rst.MoveNext
Loop Until rst.EOF
.........
.........
But find the error message :
"error#3027, the query can not be updated"
Have mentioned that the query is based on 2 tables, so I can not update the
table by this query, could you please advise and teach me what I can do? or
suggest any best solution?
Thanks so much!
but, generally speaking, you shouldn't store calculated data in a table at
all. it violates normalization rules, and always creates the potential for
inconsistent - and therefore inaccurate - data. i'd strongly recommend
against it, in most cases.
hth
"Andy" <An...@discussions.microsoft.com> wrote in message
news:D74BE274-F764-4A0A...@microsoft.com...
For example :
[daily total] : dailysalary()
It does not work as per the error message#3027,
I believe that I have something wrong, could you please correct me?
Public Function dailysalary(ByVal sglStart As Single, _
ByVal sglEnd As Single, ByVal sglSalary As Single) As Single
If sglStart > 8 And sglStart < 13 And _
sglEnd > 13 And sglEnd < 19 Then
dailysalary = (13 - sglStart + sglEnd - 14) * sglSalary / 8
ElseIf sglStart > 8 And sglEnd < 14 Then
dailysalary = (sglEnd - sglStart) * sglSalary / 8
ElseIf sglStart > 13 And sglEnd < 19 Then
dailysalary = (sglEnd - sglStart) * sglSalary / 8
ElseIf sglStart > 8 And sglStart < 13 And _
sglEnd > 13 And sglEnd > 19 And sglEnd <= 24 Then
dailysalary = (13 - sglStart + 18 - 14) * sglSalary / 8 +
(sglEnd - 18) * sglSalary / 8 * 1.5
ElseIf sglStart > 8 And sglStart < 13 And _
sglEnd > 13 And sglEnd > 19 And sglEnd > 24 Then
dailysalary = (13 - sglStart + 18 - 14) * sglSalary / 8 + (24 -
18) * sglSalary / 8 * 1.5 + (sglEnd - 24) * sglSalary / 8 * 2
ElseIf sglStart > 13 And sglEnd > 18 And sglEnd <= 24 Then
dailysalary = (18 - sglStart) * sglSalary / 8 + (sglEnd - 18) *
sglSalary / 8 * 1.5
ElseIf sglStart > 13 And sglEnd > 18 And sglEnd > 24 Then
dailysalary = (18 - sglStart) * sglSalary / 8 + (24 - 18) *
sglSalary / 8 * 1.5 +
(sglEnd - 24) * sglSalary / 8 * 2
Else
dailysalary = 0
End If
End Function
make each equation (dailysalary = "a math expression") run on one line,
regardless of linewrap here.
add the calculated field to the query's Design grid, as
[daily total]: dailysalary([START TIME], [END TIME], [SALARY])
hth
"Andy" <An...@discussions.microsoft.com> wrote in message
news:6AEFCAF8-4D49-41FC...@microsoft.com...
"Andy" <An...@discussions.microsoft.com> wrote in message
news:F1C8C7C3-D9C8-44FD...@microsoft.com...