Is it possible to run "If Statement" with DSum?

J

judyb

I am currently working with a database that uses the following code:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name")

This code works to give me the sum of all Weeks Service where the Department
Name is equal to the Current Department Name and I do want to keep that code.
In addition to that code though, I need to add, I'm guessing, an "IF
Statement" that will provide me with the sum of ALL Weeks Service of ALL
Department Name IF the Current Department Name is equal to "Reserves".

Example:
Department Name Weeks Service
Millwright 35 weeks
Pipefitter 10 weeks
Millwright (Current Deptment Name) 3 weeks

The existing code adds all the Weeks Service where the Department Name is
equal to the Current Department Name (Millwright) for a total of 38.

In the same example, if the Current Department Name were "Reserves" instead
of Millwright, I need code that will add ALL of the Weeks Service of ALL
Department Name to give me a total of 48.

I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Judy
 
S

Steve

Hello Judy,

Did you try my suggestion day before yesterday.

Steve
(e-mail address removed)
 
J

John W. Vinson

I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week. Here you go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")
 
D

De Jager

judyb said:
I am currently working with a database that uses the following code:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name")

This code works to give me the sum of all Weeks Service where the
Department
Name is equal to the Current Department Name and I do want to keep that
code.
In addition to that code though, I need to add, I'm guessing, an "IF
Statement" that will provide me with the sum of ALL Weeks Service of ALL
Department Name IF the Current Department Name is equal to "Reserves".

Example:
Department Name Weeks Service
Millwright 35 weeks
Pipefitter 10 weeks
Millwright (Current Deptment Name) 3 weeks

The existing code adds all the Weeks Service where the Department Name is
equal to the Current Department Name (Millwright) for a total of 38.

In the same example, if the Current Department Name were "Reserves"
instead
of Millwright, I need code that will add ALL of the Weeks Service of ALL
Department Name to give me a total of 48.

I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Judy
 
S

Steve

I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total WeeksService
for Millwright. The same for Pipefitter. But if the current department name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


John W. Vinson said:
I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week. Here you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")
 
J

judyb

John & Steve,

I apologize for reposting. I was unable to find my first post. Anyway, I
thank you both for responding. I copied and pasted the new code, but it did
not provide the correct answer. In fact, the answer was way too high (in the
thousands) where it should have been below 100. Any other suggestions?
Thanks again!
--
Judy


Steve said:
I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total WeeksService
for Millwright. The same for Pipefitter. But if the current department name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


John W. Vinson said:
I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week. Here you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")


.
 
S

Steve

Judy,

Did you try changing your query to a totals query as I previously suggested?

Steve


judyb said:
John & Steve,

I apologize for reposting. I was unable to find my first post. Anyway, I
thank you both for responding. I copied and pasted the new code, but it
did
not provide the correct answer. In fact, the answer was way too high (in
the
thousands) where it should have been below 100. Any other suggestions?
Thanks again!
--
Judy


Steve said:
I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total
WeeksService
for Millwright. The same for Pipefitter. But if the current department
name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


John W. Vinson said:
On Fri, 12 Mar 2010 10:56:01 -0800, judyb
<[email protected]>
wrote:

I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week. Here
you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")


.
 
J

judyb

Hi Steve,

Yes, I finally located that post and did as you suggested. It is now giving
me a Run-time error "3061": Too few parameters. Expected 2. When I select
the "Debug" button and my code opens, the following text is highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?
--
Judy


Steve said:
Judy,

Did you try changing your query to a totals query as I previously suggested?

Steve


judyb said:
John & Steve,

I apologize for reposting. I was unable to find my first post. Anyway, I
thank you both for responding. I copied and pasted the new code, but it
did
not provide the correct answer. In fact, the answer was way too high (in
the
thousands) where it should have been below 100. Any other suggestions?
Thanks again!
--
Judy


Steve said:
I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total
WeeksService
for Millwright. The same for Pipefitter. But if the current department
name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


On Fri, 12 Mar 2010 10:56:01 -0800, judyb
<[email protected]>
wrote:

I hope this makes sense. In short, I need to keep the existing code,
but need it to sum the Weeks Service slightly different IF the Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week. Here
you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")
--

John W. Vinson [MVP]


.


.
 
J

John W. Vinson

Hi Steve,

Yes, I finally located that post and did as you suggested. It is now giving
me a Run-time error "3061": Too few parameters. Expected 2. When I select
the "Debug" button and my code opens, the following text is highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?

Please post the actual code. It looks like your syntax is wrong! Click the
mouse on the word OpenRecordset and press F1 and study the help message for
the correct form of the OpenRecordset event.
 
J

judyb

The actual code is:

Private Sub Form_Current()

Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub
 
J

judyb

The actual code is:

Private Sub Form_Current()

Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub

This code worked fine until I made the following suggested changes:

Add an unbound textbox to the form where you get [Current_Department_Name}.
Name the textbox DepartmentNameCriteria and set its Visible property to No.
Put the following expression in its Controlsource property:
=IIF([Current_Department_Name] = "Reserves", Null,[Current_Department_Name])

Rather than use the DSum function, change your Service Record Query to a
Totals query. Click on the Sigma button (looks like a capital E) in the menu
at the top of the screen. Change Group By under WeeksService to Sum. Change
Group By under DepartName to Where. Put the following expression in the
criteria under DepartmentName:
Forms!NameOfYourForm!DepartmentNameCriteria Or
(Forms!NameOfYourFormDepartmentNameCriteria Is Null)



--
Judy


J_Goddard via AccessMonster.com said:
Either the syntax is wrong as suggested, or "s" (which must be a string
containing an SQL SELECT... statement) refers to a non-existant field
somewhere. What is "s", and what does it contain?

John G.

Hi Steve,

Yes, I finally located that post and did as you suggested. It is now giving
me a Run-time error "3061": Too few parameters. Expected 2. When I select
the "Debug" button and my code opens, the following text is highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?
[quoted text clipped - 43 lines]

--
John Goddard
Ottawa, ON Canada
jrgoddard at cyberus dot ca



.
 
J

John W. Vinson

The actual code is:

Private Sub Form_Current()

Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub

It sounds like either Service Record Query has two parameters that you're not
resolving, or that it does not include two of the fields you're selecting.
Could you post the SQL of this query?
 
S

Steve

Hi Judy,

First, let's make sure [Service Record Query] is working properly. In the
code below, add the two new lines right after Private Sub .... The new code
will open the query and bypass everything else in the code.

Steve

Private Sub Form_Current()

DoCmd.OpenQuery "Service Record Query"
Exit Sub


Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub





judyb said:
Hi Steve,

Yes, I finally located that post and did as you suggested. It is now
giving
me a Run-time error "3061": Too few parameters. Expected 2. When I
select
the "Debug" button and my code opens, the following text is highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?
--
Judy


Steve said:
Judy,

Did you try changing your query to a totals query as I previously
suggested?

Steve


judyb said:
John & Steve,

I apologize for reposting. I was unable to find my first post.
Anyway, I
thank you both for responding. I copied and pasted the new code, but
it
did
not provide the correct answer. In fact, the answer was way too high
(in
the
thousands) where it should have been below 100. Any other suggestions?
Thanks again!
--
Judy


:

I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total
WeeksService
for Millwright. The same for Pipefitter. But if the current department
name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


On Fri, 12 Mar 2010 10:56:01 -0800, judyb
<[email protected]>
wrote:

I hope this makes sense. In short, I need to keep the existing
code,
but need it to sum the Weeks Service slightly different IF the
Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week.
Here
you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")
--

John W. Vinson [MVP]


.


.
 
J

judyb

Hi Steve,
When you say "add the two new lines of code", are you referring to the
following code?

Forms!NameOfYourForm!DepartmentNameCriteria Or
(Forms!NameOfYourForm!DepartmentNameCriteria Is Null)
--
Judy


Steve said:
Hi Judy,

First, let's make sure [Service Record Query] is working properly. In the
code below, add the two new lines right after Private Sub .... The new code
will open the query and bypass everything else in the code.

Steve

Private Sub Form_Current()

DoCmd.OpenQuery "Service Record Query"
Exit Sub


Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub





judyb said:
Hi Steve,

Yes, I finally located that post and did as you suggested. It is now
giving
me a Run-time error "3061": Too few parameters. Expected 2. When I
select
the "Debug" button and my code opens, the following text is highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?
--
Judy


Steve said:
Judy,

Did you try changing your query to a totals query as I previously
suggested?

Steve


John & Steve,

I apologize for reposting. I was unable to find my first post.
Anyway, I
thank you both for responding. I copied and pasted the new code, but
it
did
not provide the correct answer. In fact, the answer was way too high
(in
the
thousands) where it should have been below 100. Any other suggestions?
Thanks again!
--
Judy


:

I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total
WeeksService
for Millwright. The same for Pipefitter. But if the current department
name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


On Fri, 12 Mar 2010 10:56:01 -0800, judyb
<[email protected]>
wrote:

I hope this makes sense. In short, I need to keep the existing
code,
but need it to sum the Weeks Service slightly different IF the
Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week.
Here
you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")
--

John W. Vinson [MVP]


.



.


.
 
S

Steve

No! Add these two lines of code in Private Sub Form_Current() as shown below
......

DoCmd.OpenQuery "Service Record Query"
Exit Sub

Steve



judyb said:
Hi Steve,
When you say "add the two new lines of code", are you referring to the
following code?

Forms!NameOfYourForm!DepartmentNameCriteria Or
(Forms!NameOfYourForm!DepartmentNameCriteria Is Null)
--
Judy


Steve said:
Hi Judy,

First, let's make sure [Service Record Query] is working properly. In the
code below, add the two new lines right after Private Sub .... The new
code
will open the query and bypass everything else in the code.

Steve

Private Sub Form_Current()

DoCmd.OpenQuery "Service Record Query" <---------------------
Exit Sub
<--------------------


Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub





judyb said:
Hi Steve,

Yes, I finally located that post and did as you suggested. It is now
giving
me a Run-time error "3061": Too few parameters. Expected 2. When I
select
the "Debug" button and my code opens, the following text is
highlighted:
Set rs = CurrentDb. OpenRecordset (s, dbOpenDynaset)

I am lost. This is my first database so I am really green. Got any
suggestions?
--
Judy


:

Judy,

Did you try changing your query to a totals query as I previously
suggested?

Steve


John & Steve,

I apologize for reposting. I was unable to find my first post.
Anyway, I
thank you both for responding. I copied and pasted the new code,
but
it
did
not provide the correct answer. In fact, the answer was way too
high
(in
the
thousands) where it should have been below 100. Any other
suggestions?
Thanks again!
--
Judy


:

I don't think your DSum function gives the OP the answer she wants!

If the current department name is millwright, she wants total
WeeksService
for Millwright. The same for Pipefitter. But if the current
department
name
is Reserves, then she wants total WeeksService for MillWright and
Pipefitter.

Steve


message
On Fri, 12 Mar 2010 10:56:01 -0800, judyb
<[email protected]>
wrote:

I hope this makes sense. In short, I need to keep the existing
code,
but need it to sum the Weeks Service slightly different IF the
Current
Department Name is equal to "Reserves". Thanks in advance!

Perhaps you missed the answers that Steve and I posted last week.
Here
you
go
again:

=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name
OR
[DepartmentName] = 'Reserves'")
--

John W. Vinson [MVP]


.



.


.
 
J

judyb

John,

I used the code below that you posted earlier in the form. It is
caluclating without any error messages, but it is giving me a number that is
far to high. The sum should be 83 and it is showing 30,987. Therefore, I
know that it is pulling Weeks Service from more records than it should.

Earlier Code Posted:
=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")

Now for the SQL from the query (Service Record Query) that you requested in
this post:

SELECT [Service Record].ServiceRecordID, [Service Record].EmployeeID, [Job
Titles].LOP, [Service Record].JobTitleName, [Service Record].DepartmentName,
[Service Record].DateStart, [Service Record].DateEnd,
Sum(CalcWeeks([DateStart],[DateEnd])) AS WeeksService, [Service
Record].Reason, IIf(IsNull([DateEnd]),"Current","Previous") AS Status,
[Service Record].Comments
FROM [Service Record] INNER JOIN [Job Titles] ON [Service
Record].JobTitleName = [Job Titles].JobTitleName
GROUP BY [Service Record].ServiceRecordID, [Service Record].EmployeeID, [Job
Titles].LOP, [Service Record].JobTitleName, [Service Record].DepartmentName,
[Service Record].DateStart, [Service Record].DateEnd, [Service
Record].Reason, IIf(IsNull([DateEnd]),"Current","Previous"), [Service
Record].Comments
ORDER BY [Service Record].EmployeeID;

Thanks so much for your help!


--
Judy


John W. Vinson said:
The actual code is:

Private Sub Form_Current()

Dim s As String
Dim rs As DAO.Recordset

s = "SELECT JobTitleName,DepartmentName,LOP" & _
" FROM [Service Record Query] WHERE EmployeeID = " _
& Nz(Me.EmployeeID, 0) & " AND DateEnd Is Null"

If Not Me.NewRecord Then
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset)
If Not (rs.BOF And rs.EOF) Then
Me.Current_Job_Title_Name = rs!JobTitleName
Me.Current_Department_Name = rs!DepartmentName
Me.Current_LOP = rs!LOP
rs.Close
Set rs = Nothing
Else
Me.Current_Job_Title_Name = Null
Me.Current_Department_Name = Null
End If
End If
End Sub

It sounds like either Service Record Query has two parameters that you're not
resolving, or that it does not include two of the fields you're selecting.
Could you post the SQL of this query?
 
J

John W. Vinson

John,

I used the code below that you posted earlier in the form. It is
caluclating without any error messages, but it is giving me a number that is
far to high. The sum should be 83 and it is showing 30,987. Therefore, I
know that it is pulling Weeks Service from more records than it should.

Earlier Code Posted:
=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And [DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves'")

Aha. You need some parentheses - as is it's finding ALL records ever for
Reserves, not just for this employee:

Earlier Code Posted:
=DSum("[WeeksService]","Service Record Query","[EmployeeID] = " &
[EmployeeID] & " And ([DepartmentName] = Current_Department_Name OR
[DepartmentName] = 'Reserves')")

That's a guess and may still not be correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top