Results 1 to 23 of 23

Thread: [RESOLVED] How add a running total in a database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Resolved [RESOLVED] How add a running total in a database

    I hope the title is not too confusing. I did not know exactly how to word what I need.

    In my program, when a person receives overtime, the number of hours is recorded and saved into the database. This part is fine.

    So, for example, when John Doe works 6 hours of OT, then it is added to the database.

    Now, when the person in charge of OT starts the program again, then John Doe should have 6 hours added to his total hours and it is displayed in the listview.

    My problem is, is that I dont know how to do the second part.

    Here is the code for adding the person info receiving OT hours:

    vb Code:
    1. Private Sub cmdSubmit_Click()
    2.     Dim strSQL As String
    3.     Dim blnTransOpen As Boolean
    4.     Dim ConnectionLiving As Boolean
    5.    
    6.    
    7.             'On Error GoTo ErrHandler
    8.    
    9.             blnTransOpen = False
    10.             ConnectionLiving = False
    11.    
    12.             Call openConn
    13.    
    14.             ConnectionLiving = True
    15.             dbconn.BeginTrans
    16.             blnTransOpen = True
    17.    
    18.             dbconn.Execute "INSERT INTO Event(FDID,Event_Name,Event_Hours,Time_Of_Event,Date_Of_Event,Time_Called, " _
    19.             & "Date_Called,HoursWorked,Left_Message) Values('" & txtFDID.Text & "', " _
    20.             & " '" & txtVenue.Text & "','" & txtHoursNeeded.Text & "','" & txtVenueTime.Text & "','" & txtVenueDate.Text & "', " _
    21.             & " '" & txtTimeCalled.Text & "','" & txtDate.Text & "','" & txtHoursWorked.Text & "','" & txtLeftMessage.Text & "')"
    22.            
    23.  
    24.            
    25.             dbconn.CommitTrans
    26.             blnTransOpen = False
    27.             'DBCon.Close
    28.             ConnectionLiving = False
    29.            
    30.            
    31. '            Call ClearTB
    32. End Sub

    For example: lets say John Doe currently has worked 25 hours of OT and now he has worked 6 more hours.

    So, now his total should be 31 hours. So, how is this entered into the database?

    I hope I am making sense.
    Last edited by cfd33; Apr 11th, 2010 at 10:58 PM. Reason: Title not clear.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How add a running total in a database

    As we are not familiar with your data architecture and we are not mind readers I'm afraid you will have to make your posts more informative.

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How add a running total in a database

    If you want to sum of all the hours worked then it should be something like this.
    Code:
    SELECT SUM(HoursWorked) FROM EVENT
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    As we are not familiar with your data architecture and we are not mind readers I'm afraid you will have to make your posts more informative.
    Sorry about that.

    Ok, I hope I can explain this, I have a form, that the person in charge of overtime uses, to keep track of personnels overtime.

    When a person receives overtime, then the person in charge will enter the amount of overtime received into the form. Once the user clicks the "save" button, then I want the hours to be placed into 2 columns; 1 for the overtime hours the personnel just received and the other column for the running total of overtime hours.

    The running total is needed to push the person with the most hours to the bottom of the overtime list.

    I hope this helps to clear up what I am looking for.

    The code in #1 post works fine and adds 99% of what I need, but I cannot figure out how add to the INSERT SQL above to total the hours and place it in the correct column.

    Would I use something on the lines of : INSERT SUM(HoursWorked) FROM EVENT INTO ? WHERE FDID =
    Last edited by cfd33; Apr 12th, 2010 at 04:30 PM. Reason: adding more explaination

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How add a running total in a database

    Quote Originally Posted by cfd33 View Post
    Would I use something on the lines of : INSERT SUM(HoursWorked) FROM EVENT INTO ? WHERE FDID =
    That was what I was referring to... we are not familiar with architecture (relevant tables and how they are related, primary keys, etc) nor the data involved (what are the values of FDID? you didn't provide sample data).

    Corollary, the issue you cited in post #1 is listview data related but you didn't provide existing query used to fill listview. How are we to improve something we don't understand (again, we don't know the query, tables, etc involved as we can't see what you see on your monitor)?

    dee-u's post is the most informative answer you'll get based on what you provided.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    leinad31 ,

    again, I apologize for the lack of information.

    When I get back tomorrow, then I will upload data.

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How add a running total in a database

    Have you noticed my post?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    dee-u,

    Yes, I have seen your post, but am not sure how to use it.

    Would this be the way to use the code:

    INSERT SUM(HoursWorked) FROM EVENT INTO TotalHoursWorked

    I have now added the project.
    Attached Files Attached Files
    Last edited by cfd33; Apr 14th, 2010 at 08:57 AM. Reason: additional information

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    I am hoping to accomplish the following things:

    1. have a running total of hours column

    2. a msgbox to show when a record was successfully enter

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    Anyone out there?

    I still could use some help, if you have the time.

    Thanks!

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How add a running total in a database

    You shouldn't be storing a running total in the database. That's something that should be calculated on the go, as part of the query.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    techgnome,

    First, thanks for the response.

    The reason I am trying to save the running count is so that when the program is opened, the listview will populate with the running total. So, that way the users can see who is up next for the overtime.

    So, you are saying that I should use a sql query to populate the listview for the total overtime hours?

    I am not sure how to do that.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How add a running total in a database

    Well, I'm assuming you have a query that you are currently using to populate the view, correct?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    Yes, this is what I am using:

    vb Code:
    1. Set rs1 = New ADODB.Recordset
    2.     rs1.CursorLocation = adUseClient
    3.     strSQL = "SELECT * FROM 1Unit ORDER BY Hours ASC"
    4.     rs1.Open strSQL, dbconn, adOpenDynamic, adLockOptimistic, adCmdText

    So, should I use something like:

    strSQL + strSQL = "SELECT SUM (TotalHoursWorked) FROM Event

    and how do I get the results into my last column (Total Hours Worked) ? as the other columns data is being pulled from the database, where the total hours worked, will not?

    Here is the code on load:

    vb Code:
    1. Set rs1 = New ADODB.Recordset
    2.     rs1.CursorLocation = adUseClient
    3.     strSQL = "SELECT * FROM 1Unit ORDER BY Hours ASC"
    4.     rs1.Open strSQL, dbconn, adOpenDynamic, adLockOptimistic, adCmdText
    5.    
    6.    Screen.MousePointer = vbHourglass
    7.    
    8.     rs1.Requery
    9.    
    10.     If rs1.RecordCount = 0 Then
    11.         msfg1U.Clear
    12.         MsgBox "No record found"
    13.         Screen.MousePointer = vbNormal
    14.         Exit Sub
    15.    
    16.     ElseIf rs1.RecordCount >= 1 Then
    17.  
    18.         'populate flexgrid box
    19.         With msfg1U
    20.             .Clear
    21.             .TextMatrix(0, 0) = "FDID"
    22.             .TextMatrix(0, 1) = "LAST NAME"
    23.             .TextMatrix(0, 2) = "FIRST NAME"
    24.             .TextMatrix(0, 3) = "MIDDLE INTIAL"
    25.             .TextMatrix(0, 4) = "ASSIGNMENT"
    26.             .TextMatrix(0, 5) = "KELLY DAY"
    27.             .TextMatrix(0, 6) = "TOTAL HOURS WORKED"
    28.             .Rows = rs1.RecordCount + 1
    29.             .Cols = rs1.Fields.Count - 1
    30.             .Row = 1
    31.             .Col = 0
    32.             .RowSel = .Rows - 1
    33.             .ColSel = .Cols - 1
    34.             .Clip = UCase(rs1.GetString(adClipString, -1, Chr(9), Chr(13), vbNullString))
    35.             .Row = 1
    36.             .ColWidth(0) = 1200
    37.             .ColWidth(1) = 2000
    38.             .ColWidth(2) = 2000
    39.             .ColWidth(3) = 2000
    40.             .ColWidth(4) = 2000
    41.             .ColWidth(5) = 2000
    42.             .ColWidth(6) = 2300
    43.             .ColAlignment(6) = flexAlignCenterCenter
    44.            
    45.          End With
    46.     End If
    47.    
    48.     Screen.MousePointer = vbNormal

  15. #15
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How add a running total in a database

    Your sql should be something like this.
    Code:
    strSQL = "SELECT FDID, LastName, FirstName, MiddleInitial, Assignment, KellyDay, SUM(TotalHoursWorked) FROM table GROUP BY FDID, LastName, FirstName, MiddleInitial, Assignment, KellyDay"
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    Thank you dee-u,

    I will try that when I get back to work tomorrow.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    I have changed my sql statement to the following because I am drawing data from 2 different tables.

    Here is the code:

    vb Code:
    1. strSQL = "SELECT 1Unit.FDID,1Unit.Lname,1Unit.Fname,1Unit.M_initial,1Unit.Assignment,1Unit.Kday,SUM(Event.HoursWorked)," _
    2.     & " AS SUMOFHoursWorked FROM 1Unit LEFT JOIN Event ON 1Unit.FDID = Event.FDID GROUP BY 1Unit.FDID,1Unit.Lname,1Unit.Fname,1Unit.M_initial," _
    3.     & " 1Unit.Assignment,1Unit.Kday ORDER BY Hours ASC"

    I am getting an error of : "Syntax error (missing operator) in query expression"

    I think it has something to do with the part of the sql statement:

    Event.Sum(HoursWorked)

    Any ideas?
    Last edited by cfd33; Apr 20th, 2010 at 11:08 AM. Reason: changed code

  18. #18
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How add a running total in a database

    you have a comma after your SUM, but before you aliased the field....
    rsWorked)," _
    Should be
    rsWorked)" _

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    Man, I should have caught that, I think you told me that one before (yes, I am a slow learner).

    Anyways, now I have a "datatype mismatch error"

    This is my sql statement now:

    vb Code:
    1. strSQL = "SELECT [1Unit].FDID,[1Unit].Lname,[1Unit].Fname,[1Unit].M_initial,[1Unit].Assignment,[1Unit].Kday,Sum(Event.HoursWorked) AS SumOfHoursWorked" _
    2.     & " FROM 1Unit INNER JOIN Event ON [1Unit].FDID = Event.FDID" _
    3.     & " GROUP BY [1Unit].FDID, [1Unit].Lname, [1Unit].Fname, [1Unit].M_initial, [1Unit].Assignment, [1Unit].Kday" _
    4.     & " ORDER BY Sum(Event.HoursWorked)"



    Ok, I found out why I was getting the mismatch error: one of the test data in my database did not have an entry in it.

    But now I am getting an "Invalid Column Value" when the listview is trying to load.

    What causes this?


    This is the code for loading the listview:

    vb Code:
    1. Set rs1 = New ADODB.Recordset
    2.     rs1.CursorLocation = adUseClient
    3.    
    4.     strSQL = "SELECT [1Unit].FDID,[1Unit].Lname,[1Unit].Fname,[1Unit].M_initial,[1Unit].Assignment,[1Unit].Kday,Sum(Event.HoursWorked) AS SumOfHoursWorked" _
    5.     & " FROM 1Unit INNER JOIN Event ON [1Unit].FDID = Event.FDID" _
    6.     & " GROUP BY [1Unit].FDID, [1Unit].Lname, [1Unit].Fname, [1Unit].M_initial, [1Unit].Assignment, [1Unit].Kday" _
    7.     & " ORDER BY Sum(Event.HoursWorked)"
    8.  
    9.    
    10.     Debug.Print strSQL
    11.    
    12.     rs1.Open strSQL, dbconn, adOpenDynamic, adLockOptimistic, adCmdText
    13.    
    14.    Screen.MousePointer = vbHourglass
    15.    
    16.     rs1.Requery
    17.    
    18.     If rs1.RecordCount = 0 Then
    19.         msfg1U.Clear
    20.         MsgBox "No record found"
    21.         Screen.MousePointer = vbNormal
    22.         Exit Sub
    23.    
    24.     ElseIf rs1.RecordCount >= 1 Then
    25.  
    26.         'populate flexgrid box
    27.         With msfg1U
    28.             .Clear
    29.             .TextMatrix(0, 0) = "FDID"
    30.             .TextMatrix(0, 1) = "LAST NAME"
    31.             .TextMatrix(0, 2) = "FIRST NAME"
    32.             .TextMatrix(0, 3) = "MIDDLE INTIAL"
    33.             .TextMatrix(0, 4) = "ASSIGNMENT"
    34.             .TextMatrix(0, 5) = "KELLY DAY"
    35.             .TextMatrix(0, 6) = "TOTAL HOURS WORKED"
    36.             .Rows = rs1.RecordCount + 1
    37.             .Cols = rs1.Fields.Count - 1
    38.             .Row = 1
    39.             .Col = 0
    40.             .RowSel = .Rows - 1
    41.             .ColSel = .Cols - 1
    42.             .Clip = UCase(rs1.GetString(adClipString, -1, Chr(9), Chr(13), vbNullString))
    43.             .Row = 1
    44.             .ColWidth(0) = 1200
    45.             .ColWidth(1) = 2000
    46.             .ColWidth(2) = 2000
    47.             .ColWidth(3) = 2000
    48.             .ColWidth(4) = 2000
    49.             .ColWidth(5) = 2000
    50.             '.ColWidth(6) = 2300
    51.             '.ColAlignment(6) = flexAlignCenterCenter
    52.            
    53.          End With
    54.     End If
    55.    
    56.     Screen.MousePointer = vbNormal

    I get the error at the last column (which I have commented out). This is the column where the data from the Event table should be (the running total of hours).
    Last edited by cfd33; Apr 20th, 2010 at 12:06 PM. Reason: Added more information

  20. #20
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How add a running total in a database

    I wouldn't worry about formatting the data or columns at this point.... just get the data loaded into the view first, make sure that that part is working.
    Also... what IS "msfg1U"? Clearly a grid... but what kind of grid?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    what IS "msfg1U"?
    That is what I use to abbrv. ms flexgrid.

    Is there a different way to load a flexgrid than what I am using?

    With the code above, I get everything from the first table (1Unit) to load into the flexgrid, but not the running total data from the second table (Event).

    I am not sure how to get the running total data from the Events table into the last column of the flexgrid.
    Last edited by cfd33; Apr 20th, 2010 at 01:49 PM.

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How add a running total in a database

    Dont need this:
    rs1.Requery

    After you open the recordset, try looping through the fields collection and print out the names of each field... make sure you are getting the fields you expect.

    also break point at .Cols = rs1.Fields.Count - 1 and double check to make sure that rs1.fields.count does in fact equal 7.

    -=tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: How add a running total in a database

    techgnome,

    I appreciate your help. I am not a programmer, just a borrower of code. I try to find what applies to my little projects and then mod them to my needs.

    I am not sure how to loop through the field collections and print out the names.


    I got it!! I used Si's example and it works.

    Thanks everyone!!!
    Last edited by cfd33; Apr 20th, 2010 at 07:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width