Access 2007-2013
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   News   Tips   Glossary   Templates   Forums   Help   Logon   Order   Contact Us  
 
NEW Release: Access Expert 24    dismiss
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   

  Access   Excel   Word   Windows   FrontPage   Hardware   Misc

 

Microsoft Access DMax Counter

An Alternative to AutoNumber Data Types Using the DMax Function

Q: I need a counter on my Customer form, but I don't want to use an Autonumber because sometimes I want to be able to skip a number here or there, or be able to change them on the fly. How can I create my own number that will automatically increment when I add a new record?

A: First, make yourself a Number field in your table. I'll call mine MyCounter.

 

In your Customer form, add the field as a textbox. Now bring up the Form's properties and find the BeforeInsert event. Click on the builder button [...] and launch the Code Builder.

 

In the BeforeInsert event, insert the following VBA code:

MyCounter = Nz(DMax("MyCounter", "CustomerT")) + 1

 

What this will do is use the DMAX function to find the largest value of MyCounter in your Customer table, and add 1 to it. The NZ function will simply return a ZERO instead of a NULL if no value exists.

Now, when the first character is typed into a new record, the BeforeInsert event will go out, calculate the largest counter value, add 1 to it, and put that in the MyCounter field of the new record.

Some people have emailed me asking how to use this in a SUBFORM if you need to keep a unique counter list for each parent item. For example, if your main form is based on OrderT and your subform is based on OrderDetailT then you would simply put the code in the BeforeInsert update for the subform records, and edit it accordingly:

MyCounter = Nz(DMax("MyCounter", "OrderDetailT","OrderID=" & Forms!OrderF!OrderID)) + 1

This just says to find the largest counter from the OrderDetailT table where the OrderID equals the OrderID of the parent form.

For more help with this Form!FormName!FieldName concept, see my other tutorial on Getting a Value from an Open Form.

 

 

By Richard Rost   Richard Rost on Facebook Richard Rost on Twitter Richard Rost on Google Plus Richard Rost on LinkedIn Email Richard Rost
Click here to sign up for more FREE tips

 

You may want to read these articles from the 599CD News:

 

Learn
 
Accessindex
Excelindex
Wordindex
Windowsindex
PowerPointindex
Photoshopindex
Visual Basicindex
ASPindex
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search Our Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About Us
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Gift CDs
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Us
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

Google Plus    LinkedIn

Blog RSS Feed    YouTube Channel
Richard Rost Microsoft MVP