Access - counting scanned records (1 Viewer)

Mark H.

New member
Local time
Today, 09:17
Joined
May 20, 2022
Messages
7
I'm trying to create a counter for each time a Tracking Number is scanned into a form. Although the display box and buttons from the code below work, I can't figure out how to progressively display "y" either on an unbound box or by clicking the count button.


Private Sub Tracking_Number_AfterUpdate()

Dim x As Integer
Dim y As Integer
x = 20

Do Until y >= x
y = y + 1
Next

TotalCount = y
End Sub

Private Sub CountButton_Click()
MsgBox y
End Sub

Private Sub ResetCount_Click()
Call form_load
Call Form_Current
End Sub
 

cheekybuddha

AWF VIP
Local time
Today, 13:17
Joined
Jul 21, 2014
Messages
2,237
Why are you looping at all?

First, you need a module level variable to store the count.

Then, your AfterUpdate event increments it

Your reset button click event will set it to 0 again

Code:
Option Explicit
Option Compare Database

Private m_iCount As Integer ' Defaults to 0

Private Sub Private Sub Tracking_Number_AfterUpdate()

  m_iCount = m_iCount + 1

End Sub

Private Sub CountButton_Click()

  MsgBox m_iCount

End Sub

Private Sub ResetCount_Click()

  m_iCount = 0

End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:17
Joined
Feb 19, 2002
Messages
42,970
If all you are doing is capturing the barcode, your form should be designed to advance to a new record after each scan.

Count the records by using a query or dCount() after they are added to the table.

If this is a batch process, generate the BatchID and add that as a default to the BatchID field on the form so it is added with each record. That makes the count easy. If you want to see it increment, add the dCount() to the form's Current event so it can be shown on the form.
 

Mark H.

New member
Local time
Today, 09:17
Joined
May 20, 2022
Messages
7
Why are you looping at all?

First, you need a module level variable to store the count.

Then, your AfterUpdate event increments it

Your reset button click event will set it to 0 again

Code:
Option Explicit
Option Compare Database

Private m_iCount As Integer ' Defaults to 0

Private Sub Private Sub Tracking_Number_AfterUpdate()

  m_iCount = m_iCount + 1

End Sub

Private Sub CountButton_Click()

  MsgBox m_iCount

End Sub

Private Sub ResetCount_Click()

  m_iCount = 0

End Sub
Thank you so much, this is exactly what I needed. I'm fairly new to VBA coding, I was trying whatever I could find in the forums first.
 

Users who are viewing this thread

Top Bottom