ACC: How to Create a Multiuser Custom Counter (1.x/2.0)ID: Q88159
|
Moderate: Requires basic macro, coding, and interoperability skills.
You can define a field as a counter in a Microsoft Access table. When you
define a field as a counter, Microsoft Access manages the numbering in the
field. When you add a record, Microsoft Access assigns the next available
value to the field. You have no control over this value.
You may want to use a unique counter that is not immediately sequential in
nature. For example, you may want to use a counter that decrements, or a
counter that steps according to some value.
This article demonstrates a sample user-defined Access Basic function that
you can use to create a custom counter field.
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.
The following steps outline in general how to create a custom counter.
Later in the article, you will find a more detailed description of how
to create a custom counter.
Function Next_Custom_Counter ()
On Error GoTo Next_Custom_Counter_Err
Dim MyDB As Database
Dim MyTable As Table
Dim NextCounter As Integer
' If the control is large, you may need to make this a Double.
'==============================================================
' Open table and get the next available number, increment value
' by 10 and save the number back into the table.
'==============================================================
Set MyDB = CurrentDB()
Set MyTable = MyDB.OpenTable("Counter Table")
MyTable.Edit
NextCounter = MyTable("Next Available Counter")
'===============================================================
' The next line can be changed to conform to your custom counter
' preferences. This example only increments the value by +10
' each time.
'===============================================================
MyTable("Next Available Counter") = NextCounter + 10
MyTable.Update
MsgBox "Next available counter value is " & Str$(NextCounter)
Next_Custom_Counter = NextCounter
Exit Function
'================================================================
' The following error routine should be replaced with a custom
' error routine. This example only resumes execution if an error
' occurs. If a record locking error occurs, this is fine. But any
' non-record locking error will result in an infinite loop.
'================================================================
Next_Custom_Counter_Err:
Msgbox "Error " & err & ": " & error$
If ERR <> 0 then Resume
End
End Function
Additional query words: network duplicate
Keywords : kbusage TblDatyp
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 23, 1999