Results 1 to 5 of 5
  1. #1
    AMTodd72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    3

    Help with Adding Multiple Counters to a Form

    I am not very good at programming but I am hopeful this is a relatively simple one. I have a form which has a manual 'counter' for 5 different machines to help keep track of routine maintenance. I would like the counter to update a text box each time a button it is clicked, and reset when another button is clicked.

    The code below works exactly as I need it on the form module, but if I try to add another counter for "machine2" I get a compile error when running the script. Hoping someone can help clarify what I am doing incorrectly.

    Thanks


    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub Machine1UP_Click()
    
    
    Dim Machine1UPCOUNT As Integer
    
    
    
    
    Machine1UPCOUNT = Nz(Me.Machine1Counter.Value + 1, 0)
    Me.Machine1Counter.Value = Machine1UPCOUNT
    
    
    
    
    End Sub
    
    
    Private Sub Machine1CLEAR_Click()
    Dim ClearMachine1 As Integer
    
    
    
    
    ClearMAchine1= Me.Machine1Counter.Value = 0
    Me.Machine1Counter.Value = ClearMachine1
    
    
    End Sub


  2. #2
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Hi AMTodd72!

    This is my proposal:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub MachineUpCount(ByVal intMachineID As Integer, Optional ByVal fReset As Boolean)
        'Common procedure for all machines.
    
        'Set the current machine's counter.
        With Me.Controls("Machine" & intMachineID & "Counter")
            If fReset Then
                'Called from "Reset" button.
                .Value = 0
            Else
                'Called from "Up" button.
                .Value = Nz(.Value, 0) + 1
            End If
        End With
    End Sub
    
    Private Sub Machine1CLEAR_Click()
        MachineUpCount 1, True
    End Sub
    
    Private Sub Machine1UP_Click()
        MachineUpCount 1
    End Sub
    
    Private Sub Machine2CLEAR_Click()
        MachineUpCount 2, True
    End Sub
    
    Private Sub Machine2UP_Click()
        MachineUpCount 2
    End Sub
    
    Private Sub Machine3CLEAR_Click()
        MachineUpCount 3, True
    End Sub
    
    Private Sub Machine3UP_Click()
        MachineUpCount 3
    End Sub
    
    Private Sub Machine4CLEAR_Click()
        MachineUpCount 4, True
    End Sub
    
    Private Sub Machine4UP_Click()
        MachineUpCount 4
    End Sub
    
    Private Sub Machine5CLEAR_Click()
        MachineUpCount 5, True
    End Sub
    
    Private Sub Machine5UP_Click()
        MachineUpCount 5
    End Sub
    Cheers,
    John

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Not seeing code for machine2.

    Really don't see that variables serve any purpose. Resets to 0 as soon as procedures ends. Following should produce same result.

    Me.Machine1Counter = Nz(Me.Machine1Counter + 1, 0)

    The second procedure is returning a True or False (-1 or 0). If you just want to set field/control to 0 then just:

    Me.Machine1Counter = 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.

  4. #4
    AMTodd72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    3
    Hi June,

    I did not include code for the other machines as it was a repeat with different variable names. I guess sometimes the answer is in simplicity, as I removed the variables as you suggested and stopped getting the compile error. Thank you!

  5. #5
    AMTodd72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    3
    Hi John,

    My problem seemed to be solved by simplifying the code a little, but I want to say thanks for spending the time to write this.

    Best,
    Aaron

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

Similar Threads

  1. Replies: 6
    Last Post: 07-07-2017, 05:14 PM
  2. Replies: 6
    Last Post: 08-01-2014, 12:13 PM
  3. two counters in form but only if XXX applies
    By dachap1983 in forum Access
    Replies: 2
    Last Post: 02-22-2014, 01:28 PM
  4. Adding multiple records from one form
    By sotssax in forum Forms
    Replies: 8
    Last Post: 07-17-2011, 11:16 AM
  5. Replies: 3
    Last Post: 03-16-2011, 12:44 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