Results 1 to 4 of 4
  1. #1
    accessnihon is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    10

    Looping through records in a continous form

    Good morning :-)

    I am currently struggling with an operation that might be pretty simple to solve. I am at the moment thinking about the creating the following solution:

    A user enters a value x between 0 and 1 into a field and clicks a button.
    The vba code should then loop through all FILTERED items of the current recordset and sum up their population, until the value the user has entered is reached.

    Once this point has reached, all records that have not been summed up yet, should be filtered out.

    I don't know if my wording is understandable, but would really appreciate any help for generating the necessary code/solution.



    best regards

    Code:
    Private Sub sumup_Click()
    
    Dim popArray() As Integer
    Dim RecordCount As Integer
    
    RecordCount = DCount("[population]", "[main]")
    ReDim popArray(RecordCount)
    
    popCounter = 0
        While popCounter < 0.02
        popCounter = popCounter + ([population] / DSum("[population]", "[main]"))
    Wend
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    If by current recordset you mean an open form, think I would use the form RecordsetClone method. Something like:
    (The input of between 0 and 1 throws me off)

    Dim x As Double, pop as double
    x = 0.01
    With Me.RecordsetClone
    While Not .EOF And x < Me.textboxname
    pop = pop + .[population]
    x = x + 0.01
    If Not .EOF Then .MoveNext
    Wend
    End With
    MsgBox "Population = " & pop
    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
    accessnihon is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    10
    Sorry for my late reply.
    My internet access was somewhat restricted due to the holidays, but I can promise better reaction times now.

    Quote Originally Posted by June7 View Post
    If by current recordset you mean an open form,
    That is indeed what I mean. However, it is important that all restrictions applying to the open form (i.e. active filters) are also active for the loop functions.
    Values that don't match the filter, should not be part of the mentioned "RecordsetClone".

    I also think that my explanation was kind of spoiled, in the initial post.
    The value X is a percentage, therefore it should be between 0 and 1. When summing up the population values, it would be interesting for me to only add the relative percentage.

    I tried to implement this via the dsum command, but am not sure if that will actually work.

    Code:
    popCounter = popCounter + ([population] / DSum("[population]", "[main]"))
    Any suggestions/help is very welcome.
    thanks for the help so far.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Making me think. I use RecordsetClone a lot. If the form is filtered, it is usually filtered by the WHERE argument of DoCmd.OpenForm. In one case is a subform and the filter is done by the Master/Child links. The clone is always a duplicate of the form's recordset.

    As for the DSum, it must reference table, not form. Without filter criteria it will sum every record in table.
    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.

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

Similar Threads

  1. Looping through Records in SQL
    By make me rain in forum Queries
    Replies: 13
    Last Post: 07-17-2011, 08:58 AM
  2. Replies: 1
    Last Post: 05-23-2011, 07:11 AM
  3. Replies: 0
    Last Post: 02-13-2011, 02:55 PM
  4. Checkbox in continous form
    By senthilrg in forum Access
    Replies: 11
    Last Post: 12-05-2009, 08:49 AM
  5. Horitontal Continous Form?
    By Lawrence in forum Forms
    Replies: 1
    Last Post: 07-21-2009, 03:06 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