Create a counter

I have a form (single form) where I would like to keep track of how many records were input at any one time.  Thus when the user clicks on the new record button on the navigation bar the counter [ExpenseCount] would increase by one.  When the counter reaches the value in another box [TotalCount] the counter [ExpenseCount] resets.

Further, I would like this to only occur if a third box [SplitExpense] = true

That last part is just a parent if statment, but thought I would throw it in.

Tips
dewster1971Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marc333Commented:
Tips,
Let me make sure I've got this correct:

Let's say TotalCount is 10.

You're looking for ExpenseCount to increase by one every single time a user input a new record until it reaches 10 (totalCount's value).  Of course, this only happens if SplitExpense = True.  Is this correct?

Marc
0
dewster1971Author Commented:
Yep you got it
0
Marc333Commented:
Ok, here's one way:

Create a table with only one record.  The fields would be CounterID, ExpenseCount and TotalCount.  Go into the table and set TotalCount to the number you want.  You can do it with code that's like what I've got below.  

Next you need to decide which event you want to trigger the code.  If its just about new records, you might look at the either the Before or After Insert event.  The Insert event is what is trigger when someone types for the first time on a new record.  

So, for this example, Countertbl is the name for our counter table.  I would use the following in that event:

Private Sub Form_AfterInsert()

Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim subd As String, vlu As Integer, filterstr As String, tst As String
Set cnn = CurrentProject.Connection
Set Arst = New ADODB.Recordset
rst.Open "Countertbl", cnn, adOpenKeyset, adLockOptimistic, adCmdTable
rst.movefirst

If me.splitexpense = True then
    If rst.fields("TotalCount") < 10 then
        rst.fields("Expensecount") = rst.fields("expensecount") + 1
    Else
        rst.fields("Expensecount") = 0
   end if
end if
End Sub



0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Marc333Commented:
Sorry, spotted a mistake:


Private Sub Form_AfterInsert()

Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim subd As String, vlu As Integer, filterstr As String, tst As String
Set cnn = CurrentProject.Connection
Set Arst = New ADODB.Recordset
rst.Open "Countertbl", cnn, adOpenKeyset, adLockOptimistic, adCmdTable
rst.movefirst

If me.splitexpense = True then
    If rst.fields("ExpenseCount") < rst.fields("TotalCount") then
        rst.fields("Expensecount") = rst.fields("expensecount") + 1
    Else
        rst.fields("Expensecount") = 0
   end if
end if
End Sub
0
Marc333Commented:
Oh, and you won't need the line:

Dim subd As String, vlu As Integer, filterstr As String, tst As String

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nico5038Commented:
Just puzzled where the TotalCount is coming from.
Personally I would chose to insert all rows on forehand and set the form´s "AllowAdditions" to "No" to prevent the entry of more rows.
When there may be less rows than the max, you can still use in the BeforeInsert event a check or the max is reached and set  the form´s "AllowAdditions" to "No" by code to prevent more rows being entered.

Nic;o)
0
hnasrCommented:
Try this in Form_Current  event:

Private Sub Form_Current()
[ExpenseCount] = Nz([ExpenseCount], 0) + 0
[totalCount] = Nz([totalCount], 0) + 0
If Me.NewRecord Then
    If [SplitExpense] Then
        [ExpenseCount] = [ExpenseCount] + 1
        If [ExpenseCount] > [totalCount] Then
            [ExpenseCount] = 1
        End If
    End If
End If
End Sub
0
hnasrCommented:
Modified comment: Try it in Form_Current  event,

Private Sub Form_Current()
[ExpenseCount] = Nz([ExpenseCount], 0) + 0
[totalCount] = Nz([totalCount], 0) + 0
If Me.NewRecord Then
    If [SplitExpense] Then
        If [ExpenseCount] = [totalCount] Then
            [ExpenseCount] = 0
        End If
        If [totalCount] > 0 Then
            [ExpenseCount] = [ExpenseCount] + 1
        End If
    End If
End If
End Sub
0
Pigster14IT ConsultantCommented:
Okay, here is a thought I had.

On your table that you are adding records to, have an auto number field.

On Form Open, collect the max autonumber field and store in a global variable.

On Form Current, I am assuming that every time you add a new record it will re-process on the Form_Current code. Each time you add a record, you want to compare the new autonumber just added to the one in global. If the SplitExpense is true and the difference between the current autonumber and the global variable are what you wish, then you can reset your global variable to the most current autonumber and begin counting all over again.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.