Connecting Tech Pros Worldwide Forums | Help | Site Map

sum() function is not returning value

Newbie
 
Join Date: Mar 2007
Posts: 16
#1: May 26 '07
Expand|Select|Wrap|Line Numbers
  1.  
  2. why this sum function won't work ? 
  3. eg Sum(personal)
  4. lblPersonal.Caption it wont retrieve the sum value of the personal field
  5.  
  6. Dim cn6 As New ADODB.Connection
  7. Dim strCNString4 As String
  8. Dim rs6 As New ADODB.Recordset
  9. Dim intFound6 As Double
  10.  
  11. strCNString6 = "Data Source=" & App.Path & "\PayrollBakeryDB.mdb"
  12. cn6.Provider = "Microsoft Jet 4.0 OLE DB Provider"
  13. cn6.ConnectionString = strCNString6
  14.  
  15. cn6.Open
  16.  
  17.     With rs6
  18.         '.Open "SELECT Sum(personal), Sum(sick), Sum(vacation), Sum(maternity), emp_id, from_date, to_date FROM leavela WHERE Month(from_date)=" & intMonth & " and Year(from_date)=" & intYear & " and Month(to_date)=" & intMonth & " and Year(to_date)=" & intYear & " GROUP BY emp_id, from_date, to_date ", cn6, adOpenDynamic, adLockOptimistic
  19.  
  20.          Do Until .EOF
  21.  
  22.              If !emp_id = txtEmp_ID.Text Then
  23.                    lblPersonal.Caption = rs6.Fields(0) 'here
  24.                    lblSick.Caption = rs6.Fields(1) 'here
  25.                    Vacation.Caption = rs6.Fields(2) 'here
  26.                    lblMaternity.Caption = rs6.Fields(3) 'here
  27.  
  28.                     intFound6 = 1
  29.              End If
  30.  
  31.             .MoveNext
  32.           Loop
  33.  
  34.           'If intFound6 = 0 Then
  35.                    'MsgBox "Employee ID not found !", vbExclamation, "Error"
  36.           'End If
  37.  
  38.      End With
  39.  
  40.  

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#2: May 26 '07

re: sum() function is not returning value


Quote:

Originally Posted by gillian3114

Expand|Select|Wrap|Line Numbers
  1.  
  2. why this sum function won't work ? 
  3. eg Sum(personal)
  4. lblPersonal.Caption it wont retrieve the sum value of the personal field
  5.  
  6. Dim cn6 As New ADODB.Connection
  7. Dim strCNString4 As String
  8. Dim rs6 As New ADODB.Recordset
  9. Dim intFound6 As Double
  10.  
  11. strCNString6 = "Data Source=" & App.Path & "\PayrollBakeryDB.mdb"
  12. cn6.Provider = "Microsoft Jet 4.0 OLE DB Provider"
  13. cn6.ConnectionString = strCNString6
  14.  
  15. cn6.Open
  16.  
  17.     With rs6
  18.         '.Open "SELECT Sum(personal), Sum(sick), Sum(vacation), Sum(maternity), emp_id, from_date, to_date FROM leavela WHERE Month(from_date)=" & intMonth & " and Year(from_date)=" & intYear & " and Month(to_date)=" & intMonth & " and Year(to_date)=" & intYear & " GROUP BY emp_id, from_date, to_date ", cn6, adOpenDynamic, adLockOptimistic
  19.  
  20.          Do Until .EOF
  21.  
  22.              If !emp_id = txtEmp_ID.Text Then
  23.                    lblPersonal.Caption = rs6.Fields(0) 'here
  24.                    lblSick.Caption = rs6.Fields(1) 'here
  25.                    Vacation.Caption = rs6.Fields(2) 'here
  26.                    lblMaternity.Caption = rs6.Fields(3) 'here
  27.  
  28.                     intFound6 = 1
  29.              End If
  30.  
  31.             .MoveNext
  32.           Loop
  33.  
  34.           'If intFound6 = 0 Then
  35.                    'MsgBox "Employee ID not found !", vbExclamation, "Error"
  36.           'End If
  37.  
  38.      End With
  39.  
  40.  

  1. Are the sure the Recordset is returning any Records?
  2. Is the following condition ever being met? (!emp_id = txtEmp_ID.Text )
  3. Change the syntax of the following line of code:
    Expand|Select|Wrap|Line Numbers
    1. !emp_id = txtEmp_ID.Text ==> !emp_id = Me![txtEmp_ID]
    2. NOTE: The Text property is only valid when the Field has the focus.
nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#3: May 26 '07

re: sum() function is not returning value


Best to try the query first in the query editor. (Use a breakpoint and F8 to step through the code and type in the immediate window a "?" followed by the stringing to get the result in textmode for a copy/paste in the query editor.)

Next it's better to use:
Sum(NZ(personal))
as Null values can spoil the result.

Nic;o)
Reply