the dlookup is useful. But the docmd.openquery doesn't function properly
when you're using an if/then statement. I'll state the error if you wish.
as for your date diff, that won't work for workdays. i already have a
function for htat. it looks like this
Function smsQtyOfWorkingDays(pvarStartDate As Variant, pvarEndDate As
Variant) As Integer
End Function
On Error GoTo smsQtyOfWorkingDays_Err
Dim lngStartDate As Long, lngEndDate As Long
lngStartDate = CLng(CVDate(pvarStartDate))
lngEndDate = CLng(CVDate(pvarEndDate))
If lngStartDate <= lngEndDate Then
smsQtyOfWorkingDays = DateDiff("w", lngStartDate, lngEndDate) * 5 +
smsQtyOfWorkingDaysBetween2WeekDays(Weekday(lngStartDate),
Weekday(lngEndDate))
Else
lngStartDate = CLng(CVDate(pvarEndDate))
lngEndDate = CLng(CVDate(pvarStartDate))
smsQtyOfWorkingDays = -(DateDiff("w", lngStartDate, lngEndDate) * 5 +
smsQtyOfWorkingDaysBetween2WeekDays(Weekday(lngStartDate),
Weekday(lngEndDate)))
End If
smsQtyOfWorkingDays_Done:
Exit Function
smsQtyOfWorkingDays_Err:
Resume smsQtyOfWorkingDays_Done
End Function
Function smsQtyOfWorkingDaysBetween2WeekDays(intFirstWeekDay As Integer,
intSecondWeekDay As Integer)
On Error GoTo smsQtyOfWorkingDaysBetween2WeekDays_Err
smsQtyOfWorkingDaysBetween2WeekDays = 0
Dim intForIdx As Integer, intCycle2 As Integer, intCnt As Integer
intCnt = 0
If intFirstWeekDay <> intSecondWeekDay Then
If intFirstWeekDay < intSecondWeekDay Then
intCycle2 = intSecondWeekDay
Else
intCycle2 = intSecondWeekDay + 7
End If
For intForIdx = intFirstWeekDay To intCycle2 - 1
Select Case intForIdx Mod 7
Case 1, 7:
Case 2, 3, 4, 5, 6: intCnt = intCnt + 1
Case Else
End Select
Next intForIdx
End If
smsQtyOfWorkingDaysBetween2WeekDays = intCnt
smsQtyOfWorkingDaysBetween2WeekDays_Done:
Exit Function
smsQtyOfWorkingDaysBetween2WeekDays_Err:
Resume smsQtyOfWorkingDaysBetween2WeekDays_Done
End Function
Function WorkDayDiff( _
DateFrom As Date, _
DateTo As Date _
) As Long
WorkDayDiff = DateDiff("d", DateFrom, DateTo) - _
DateDiff("ww", DateFrom, DateTo, 1) * 2 - _
IIf(Weekday(DateTo, 1) = 7, _
IIf(Weekday(DateFrom, 1) = 7, 0, 1), _
IIf(Weekday(DateFrom, 1) = 7, -1, 0))
End Function
End Function
and works just dandy.
However. I need to run a query on a button click that extracts data from
two text boxes and then takes the queries numerical result and divides it by
the number of working days, to get an average over time.
So the button click essentially does this, right now:
clears the listboxes, runs a query through a macro, puts the valu einto a
listbox, takes listbox.listindex(1) and divides that by the result of another
query, in a different listbox. As i said. sloppy. I want to do all of this
programmatically and can't get it working. Here's what it looks like:
Private Sub Command49_Click()
Me.RunMacro (QtyWorkingdaysMacro)
Me.Requery
If Check51.Value = 0 Then Text55 = List69 / List63 Else Text55 = List71 /
List63
End Sub
Private Sub Command50_Click()
[Forms]![AverageForm].Requery
[Forms]![AverageForm].Refresh
If Check51.Value = 0 Then Text55 = List69.ListIndex(1) Else Text55 =
List71.ListIndex(1)
End Sub
here are the queries in question:
SELECT
Sum(MailLog.Itemsfororg100+[Itemsfororg107]+[Itemsfororg109]+[Itemsfororg113]+[Itemsfororg114]+[Itemsfororg119]+[Itemsfororg144]+[Itemsfororg185]+[Itemsfororg195]+[Itemsfororg528]+[CSCExpensereport]+[ATDCheckreq]+[ODCCorrections]+[Diner]+[Investigators]+[Credits]+[Freight]+[Tax]+[Fedex]+[Reversals])
AS [Total Processing Batch]
FROM MailLog
WHERE (((MailLog.ProcessingAssignedto) Like [Forms]![AverageForm]![Combo14])
AND ((MailLog.ProcessingAssignedDate)>=[Forms]![AverageForm]![Text7] And
(MailLog.ProcessingAssignedDate)<=[Forms]![AverageForm]![Text2]) AND
((MailLog.ProcessingCompleteDate)>=[Forms]![AverageForm]![Text7] And
(MailLog.ProcessingCompleteDate)<=[Forms]![AverageForm]![Text2]));
and the second query looks like this:
SELECT MailLog.ProcessingAssignedto,
(Sum(MailLog.Itemsfororg100+[Itemsfororg107]+[Itemsfororg109]+[Itemsfororg113]+[Itemsfororg114]+[Itemsfororg119]+[Itemsfororg144]+[Itemsfororg185]+[Itemsfororg195]+[Itemsfororg528]+[CSCExpensereport]+[ATDCheckreq]+[ODCCorrections]+[Diner]+[Investigators]+[Credits]+[Freight]+[Tax]+[Fedex]+[Reversals]))
AS [Total Processed Batches]
FROM MailLog
WHERE (((MailLog.ProcessingAssignedDate)>=[Forms]![AverageForm]![Text7] And
(MailLog.ProcessingAssignedDate)<=[Forms]![AverageForm]![Text2]) AND
((MailLog.ProcessingCompleteDate)>=[Forms]![AverageForm]![Text7] And
(MailLog.ProcessingCompleteDate)<=[Forms]![AverageForm]![Text2]))
GROUP BY MailLog.ProcessingAssignedto
HAVING (((MailLog.ProcessingAssignedto)=[Forms]![AverageForm]![Combo14]));
roger said:
1st I think you're in over your head.
I have NO idea what you're doing with THREE hidden listboxes.
Look these things up in Help: (or go get a good book)
to calc days:
=DateDiff("d", date1, date2)
although if its time worked should you be counting hours?
To RUN a Query
Docmd.runquery (qryname)
To get one value from a qry (or table) (that has many rows and cols)
=Dlookup(fieldname, datasource, where-condition)
hth
:
I have a form named average form. On current, it puts two short dates into
two textboxes. The form has 3 hidden list boxes. Two have queries attached
to them, and the third calculutes the number of work days between the two
dates in the text boxes previously mentioned. I'd like to do all of this
programatically without using the invisible listboxes, it seems really
sloppy, i just dont have the experience in VB and SQL to do it properly. I
also don't know how to properly extract a single value from a query, or run a
query programatically. When i try do.cmd and run it that way, it won't
allow it. If any other information or a copy of the form or queries in
question is required, please, ask, and i'll be happy to provide. Thank you
in advance.