sum() function is not returning value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gillian3114
    New Member
    • Mar 2007
    • 16

    sum() function is not returning value

    Code:
    why this sum function won't work ? 
    eg Sum(personal)
    lblPersonal.Caption it wont retrieve the sum value of the personal field
    
    Dim cn6 As New ADODB.Connection
    Dim strCNString4 As String
    Dim rs6 As New ADODB.Recordset
    Dim intFound6 As Double
    
    strCNString6 = "Data Source=" & App.Path & "\PayrollBakeryDB.mdb"
    cn6.Provider = "Microsoft Jet 4.0 OLE DB Provider"
    cn6.ConnectionString = strCNString6
    
    cn6.Open
    
        With rs6
            '.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
    
             Do Until .EOF
                
                 If !emp_id = txtEmp_ID.Text Then
                       lblPersonal.Caption = rs6.Fields(0) 'here
                       lblSick.Caption = rs6.Fields(1) 'here
                       Vacation.Caption = rs6.Fields(2) 'here
                       lblMaternity.Caption = rs6.Fields(3) 'here
                        
                        intFound6 = 1
                 End If
                
                .MoveNext
              Loop
              
              'If intFound6 = 0 Then
                       'MsgBox "Employee ID not found !", vbExclamation, "Error"
              'End If
              
         End With
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by gillian3114
    Code:
    why this sum function won't work ? 
    eg Sum(personal)
    lblPersonal.Caption it wont retrieve the sum value of the personal field
    
    Dim cn6 As New ADODB.Connection
    Dim strCNString4 As String
    Dim rs6 As New ADODB.Recordset
    Dim intFound6 As Double
    
    strCNString6 = "Data Source=" & App.Path & "\PayrollBakeryDB.mdb"
    cn6.Provider = "Microsoft Jet 4.0 OLE DB Provider"
    cn6.ConnectionString = strCNString6
    
    cn6.Open
    
        With rs6
            '.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
    
             Do Until .EOF
                
                 If !emp_id = txtEmp_ID.Text Then
                       lblPersonal.Caption = rs6.Fields(0) 'here
                       lblSick.Caption = rs6.Fields(1) 'here
                       Vacation.Caption = rs6.Fields(2) 'here
                       lblMaternity.Caption = rs6.Fields(3) 'here
                        
                        intFound6 = 1
                 End If
                
                .MoveNext
              Loop
              
              'If intFound6 = 0 Then
                       'MsgBox "Employee ID not found !", vbExclamation, "Error"
              'End If
              
         End With
    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:
      [CODE=vb]!emp_id = txtEmp_ID.Text ==> !emp_id = Me![txtEmp_ID]
      NOTE: The Text property is only valid when the Field has the focus.[/CODE]

    Comment

    • nico5038
      Recognized Expert Specialist
      • Nov 2006
      • 3080

      #3
      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)

      Comment

      Working...