Access 2010
Access 2003
Seminars
Tips & Tricks
Access Forum
Course Index Insider Circle
 
Tutorials   News   Tips   Forums   Help   Logon   Order  
 
     
 
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.

 

 

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

 

 
Learn
 
Microsoft Access
Microsoft Excel
Microsoft Word
Microsoft Windows
Microsoft PowerPoint
Adobe Photoshop
Visual Basic
Active Server Pages
Online Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
WalkThru Tutorials
Free Upgrades
Insider Circle
Info
 
Latest News
New Releases
User Forums
Tips & Tricks
Search Our Site
Waiting List
Production Schedule
Help
 
Live Chat
Customer Support
Troubleshooting
FAQs
TechHelp
Consulting Services
About Us
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Live Chat
General Contact Info
Support Policy
Contact Form
Email Us
Mailing Address
Phone Number
Fax Number
Course Survey
Try Us
 
Free Lessons
Online Theater
Mailing List
Course Indexes:   Microsoft Access   Microsoft Excel   Microsoft Word   Microsoft PowerPoint   Visual Basic (VB)   Photoshop   Active Server Pages (ASP)  
Keyword Search Cloud:   What's This?   courses   microsoft access tutorials   vlookup   access   cartesian   excel   dlookup   vba   attendance   access 2007   sql   windows 7   combo box   pivot table   visual basic   test   iif   word   calendar   query   conditional formatting   pivot tables   photoshop   hlookup   access 101   excel 202   excel 2007   student attendance   update query   my account   word 2007   append query   quickbooks   queries   dsum   reports   powerpoint   if   microsoft access   dmax   mail merge   relationships   ms access   access 2010   vb   dcount   subforms   excel 2003   handbooks   html   599cd   combobox   if function   security   microsoft word   after update  
Copyright 2012 by 599CD.com, All Rights Reserved