• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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
0
dewster1971
Asked:
dewster1971
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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
 
Pigster14Commented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now