Results 1 to 4 of 4
  1. #1
    The Professor is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Location
    Cambridgeshire, UK
    Posts
    67

    DMax Question

    Hi,



    I'm trying to use DMax to auto increment a field (CustID, a longInt) and enter the value automatically when a new record is created in Customers Table (can't use AutoNumber in this particular situation) so the code I've written is this:

    Me.CustID = DMax("CustID", "tblCustomers") + 1

    I've stumbled across two problems. One, the code doesn't seem to be doing what I hoped it would and two, I'm unsure where to insert this code.
    Should it go into the controls events, or the forms events, and which event should I use? Apologies if I seem dim, but I appreciate some help.

    Thanks.

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    If this is being used in a Multi-User environment, it needs to be the last code in the Form_BeforeUpdate event, so that it is assigned in the last nanosecond before the Record is actually saved. Placed elsewhere, in a M/U environment, and you run the chance that two users will have the same number assigned to the Record they're entering. In over a decade of doing this kind of thing, in the Form_BeforeUpdate event, I've never had a 'duplication' problem.

    If this is a Single-User environment , it could be done in the Form_Current event.

    Done in the Form_Current event, it will appear on the Record immediately.

    Done in the Form_BeforeUpdate event, it will not be assigned and therefore will not be displayed until the Record is saved, i.e after you leave the Record and then return to it, in one way or the other, or explicitly save the Record.

    How, exactly, are you 'expecting' this to work?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    The Professor is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2012
    Location
    Cambridgeshire, UK
    Posts
    67
    Hi Linq,

    Many thanks for your helpful reply - much apprecaited.

    What I meant when I said the code wasn't doing what I expected (or hoped), is that it was altering the all CustIDs in whatever record was being shown. At first I thought this was because I'd placed the code in the wrong event, but your reply clarified things for me. I got round the problem by enclosing the code in a If Me.NewRecord Then... End If.

    Many thanks again.

    Prof.

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. DMax() with date
    By rkalapura in forum Forms
    Replies: 1
    Last Post: 11-17-2012, 11:17 PM
  2. DMAX in VBA
    By Meanfish in forum Access
    Replies: 1
    Last Post: 11-03-2012, 11:06 AM
  3. Replies: 6
    Last Post: 07-24-2012, 03:02 PM
  4. How to use DMAX
    By tomneedshelp in forum Access
    Replies: 2
    Last Post: 03-01-2012, 07:22 AM
  5. Using Nz and Dmax
    By timmy in forum Programming
    Replies: 5
    Last Post: 07-04-2011, 06:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums