How To Have MS Access Increment A Number Without An Autonumber VBA

So I had an interesting question come my way today via the chatbox on this website.

Basically it was to increment a value in a label based on the button that was clicked. Here is what part of the form looked like:

The data was saved and read from this table:

So if the green “+10” button was clicked, 10 got added to the value of the table and was displayed in the form’s label.

The similar functionality happened when you clicked on the orange, blue, and purple buttons.

Here is the code used when you click the corresponding buttons (you can add or subtract from lngCounter to fit the button)

Private Sub btnG_01_Click()
    Dim lngCounter As Long
    Dim strUpdateSQL As String
    'get the counter from the table
    lngCounter = DLookup("[Scores]", "tblLogger", "ID=1")

    lngCounter = lngCounter + 10
    'update the table with the incremented count
    strUpdateSQL = "UPDATE tblLogger SET Scores =" & lngCounter & " WHERE ID=1"
    CurrentDb.Execute strUpdateSQL
    'update the label caption
    Me.lblCounter_LG.Caption = lngCounter
End Sub

What the code does is lookup the value in the table and assigns it to a variable, lngCounter, then the quantity of the button clicked on it totaled with it, and then the new value gets updated in the table.

To make sure only one row gets updated, we use the “WHERE ID=x”.

Then the new value gets reflected in the caption property of the label on the form.

Thanks for a fun question.

Offsite Related Information:

“Go Fund Me” Page

($5 suggested amount)

(…10% of your gift amount will go to charity)

Free! Subscribe To Our YouTube Channel!

Free MS Access VBA Programming Course