Results 1 to 11 of 11
  1. #1
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26

    Calculating information based on filtering a split datasheet

    Question 1: I'm trying to find the value of a specific cell in a split datasheet form, I have the column I need and the value of another item in the row, how would I go about this? (Basically, I have a column that lists the number of requests, and a number that puts a value to those requests. I want to figure out the highest number of the requests, and then figure out the value of that request. The highest request isn't always the latest one, so I can't use max.)

    Question 2: I'm trying to add up some numbers based on values in a column. This is a kind of pseudo-code I came up with.

    'Dim x As Integer
    'Dim money as Integer
    'money = 0
    'x = Rows.Count


    'For loop_count = 1 to x
    'If Me.Change_Request.x > 0 Then
    'money = money + Me.Change_Amount.Value
    'Else


    'money = money + Me.Total_Amount.Value
    'Next loop_count


    'Me.Filtered_Amount = money

    Can anyone help me with the actual VBA? xD

    Thanks guys!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Question 1: You want to return the value of a field for the record that has the maximum number of requests? This can be tricky.

    DLookup("ValueField","tablename","NumberRequestsFi eld=" & DMax("NumberRequestsField","tablename","filter clause if needed"))

    Otherwise, maybe use RecordsetClone. Something like:

    Dim x As Double, y As Double
    With Me.RecordsetClone
    While Not .EOF
    If !NumberRequestsField > x Then y = !ValueField
    x = !NumberRequestsField
    .MoveNext
    Wend
    End With

    Question 2: Don't understand. Why don't you do a GROUP BY query or a report with Grouping & Sorting and aggregate calcs?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26
    I attempted to use the recordsetclone, but could not fully figure it out. What do the ! and . mean? Should they represent the table? If so, does the default name of the split datasheet go to Sheet1, or the name of the form itself?

    Dim x As Double, y As Double


    With Me.RecordsetClone
    While Not .EOF
    If Me.Change_Request__ > x Then y = Me.Total_Amount
    x = Me.Change_Request__
    Me.Next
    Wend
    End With


    Also, would it be possible for me to use DSum for my second question?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    In the example, I use the bang (!) for referring to fields of recordset. I use the dot (.) for referring to controls, methods, properties. The dot will provoke intellisense popup tips. Your code syntax does not follow my example.

    DSum could probably be used.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26
    I decided on using a really roundabout way for part 2 using DSum, but can't seem to get it to be the correct amount.

    Dim AmountWR As Integer
    Dim AmountCR1 As Integer
    Dim AmountCR2 As Integer
    Dim AmountCR3 As Integer
    Dim AmountCR4 As Integer
    Dim AmountCR5 As Integer
    Dim AmountCR6 As Integer
    Dim FilteredAmount As Integer


    AmountWR = DSum("Total Work Order Amount", "Sheet1", Me.Change_Request__ = 0)
    AmountCR1 = DSum("Year 1 Incremental Change", "Sheet1", Me.Change_Request__ = 1)
    AmountCR2 = DSum("Year 2 Incremental Change", "Sheet1", Me.Change_Request__ = 2)
    AmountCR3 = DSum("Year 3 Incremental Change", "Sheet1", Me.Change_Request__ = 3)
    AmountCR4 = DSum("Year 4 Incremental Change", "Sheet1", Me.Change_Request__ = 4)
    AmountCR5 = DSum("Year 5 Incremental Change", "Sheet1", Me.Change_Request__ = 5)
    AmountCR6 = DSum("Year 6 Incremental Change", "Sheet1", Me.Change_Request__ = 6)


    FilteredAmount = AmountWR + AmountCR1 + AmountCR2 + AmountCR3 + AmountCR4 + AmountCR5 + AmountCR6


    Me.Filtered.Text = FilteredAmount

    Thanks for all the help btw

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Why are you referencing control on form in the WHERE criteria? This must be a field in the data source. Also, when names have spaces, special characters, punctuation (underscore is exception) must enclose in [].

    AmountWR = DSum("[Total Work Order Amount]", "Sheet1", "fieldname = 0")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26
    Ah, I see what you're saying, does this look better?

    Dim AmountWR As Integer
    Dim AmountCR1 As Integer
    Dim AmountCR2 As Integer
    Dim AmountCR3 As Integer
    Dim AmountCR4 As Integer
    Dim AmountCR5 As Integer
    Dim AmountCR6 As Integer
    Dim FilteredAmount As Integer


    AmountWR = DSum("[Total Work Order Amount]", "Sheet1", "[Change Request #] = 0")
    AmountCR1 = DSum("[Year 1 Incremental Change]", "Sheet1", "[Change Request #] = 1")
    AmountCR2 = DSum("[Year 2 Incremental Change]", "Sheet1", "[Change Request #] = 2")
    AmountCR3 = DSum("[Year 3 Incremental Change]", "Sheet1", "[Change Request #] = 3")
    AmountCR4 = DSum("[Year 4 Incremental Change]", "Sheet1", "[Change Request #] = 4")
    AmountCR5 = DSum("[Year 5 Incremental Change]", "Sheet1", "[Change Request #] = 5")
    AmountCR6 = DSum("[Year 6 Incremental Change]", "Sheet1", "[Change Request #] = 6")


    FilteredAmount = AmountWR + AmountCR1 + AmountCR2 + AmountCR3 + AmountCR4 + AmountCR5 + AmountCR6


    Me.Filtered.Text = FilteredAmount

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Should work.

    Still advise against the spaces and special characters in names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26
    Yeah, I realized that afterwards xD

    I put in the code, got a run-time error 6 Overflow on the AmountWR = DSum("[Total Work Order Amount]", "Sheet1", "[Change Request #] = 0") line. When I comment out the line, I get a runtime error 94 Invalid use of Null, would a debug help me figure out what's going on?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    You explicitely declare the variables as Integer and the sum returned must be too large for that type. Use Double type.

    The variables can't hold a Null (only Variant type can hold Null). If no records meet the criteria of the DSum then Null is returned.

    Also, if there is possibility [Change Request #] could be Null in the data source, the expression will fail because can't test for Null = something.

    Need to handle possible Null. Review http://allenbrowne.com/casu-12.html

    Text is the wrong property for a textbox control. The default property of data controls in Access/VBA is Value so don't even state the property:

    Me.Filtered = AmountWR + AmountCR1 + AmountCR2 + AmountCR3 + AmountCR4 + AmountCR5 + AmountCR6
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    JustLearning is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2012
    Posts
    26
    Thank you so much for all your help! That worked perfectly!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filtering records on a split form
    By Accessbeginner23 in forum Programming
    Replies: 1
    Last Post: 07-02-2012, 03:23 PM
  2. Replies: 2
    Last Post: 02-27-2012, 03:02 PM
  3. Datasheet view of Split Form in a Tab??
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 12-19-2011, 04:37 PM
  4. Formatting split form datasheet portion
    By SpaceEd in forum Forms
    Replies: 5
    Last Post: 10-19-2011, 11:40 AM
  5. Code to call Split Form View Datasheet
    By ahightower in forum Programming
    Replies: 1
    Last Post: 07-28-2011, 04:57 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
  •  
Other Forums: Microsoft Office Forums