Solved

Auto Increment number

Posted on 2014-04-06
9
949 Views
Last Modified: 2014-04-07
Hi,

I'm in the process of redesigning my database app and can't quite get an aspect to work.  Put simply, when I add a batch reference into my database for a customer the batch number must run concurrently according to the customer.

For example.  I add 5 batches for Customer A (1,2,3,4,5).  I then add 3 new batches for Customer B (1,2,3).  If I use an auto increment field, Customer B would get (6,7,8).  How would I allow each Customer to maintain their own batch sequence?

Can anyone advise on how I would achieve this?

Regards,
Anthony
0
Comment
Question by:anthonytr
9 Comments
 
LVL 12

Expert Comment

by:pdebaets
Comment Utility
One option would be to relax the requirement for batch number for each company to start at 1. Then, an auto-increment number would ensure batch number uniqueness.

Otherwise, an autonumber field is not appropriate here. Declare the field as type Numeric Long Integer and set its default value to be one greater than the maximum value of batch numbers for the current company. ex.:

Form Control property: DefaultValue
=1+DMax("BatchNumber","MyTable","MyCompanyID = " & Forms!MyCurrentForm!MyCompanyIDControlName)
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Another method is to store the next number in a other table.  You may need to do this for a number of tables , so i usually store table ame, uniqueCd, and NextVal.

When i do this, i create a function to call just before i insert records to ensure there is no possibility that someone else can get the sAme value by using the dmax method.  I'm ony phone or i would provide the function, but instead of dmax i open a recordset and get the  nextVal and the reset it in two steps.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Or you could dispense with storing the customer sequence number altogether.  Your AutoNumber already provides a unique identifier in a specific order.  You can always calculate the customer-specific sequence numbers on demand, in your reports.
0
 

Author Comment

by:anthonytr
Comment Utility
I have implemented the code, but keep getting and error when it runs:

Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("tbl_batches", dbOpenTable)
rst.AddNew
rst!customer_id = Me.customer_id
rst!batch_number = 1 + DMax("batch_number", "tbl_batches", "customer_id = " & Forms!frm_batches!customer_id)

rst.Update
rst.Close

Set rst = Nothing
End Sub

Open in new window


It debugs to:

rst!batch_number = 1 + DMax("batch_number", "tbl_batches", "customer_id = " & Forms!frm_batches!customer_id)

with "Runtime Error '2471'
The Expression you entered as a query parameter produced this error: 'TR12043'

customer_id is a text field

Thanks
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:anthonytr
Comment Utility
My bad - I got the syntax wrong.  I now have:

rst!batch_number = 1 + DMax("batch_number", "tbl_batches", "customer_id = '" & Me.customer_id & "'")

all working fine.
0
 

Author Comment

by:anthonytr
Comment Utility
One thing I have realised is this.

If I'm adding a new customer and they currently do not have any Batches recorded in the database the DMax function will error out.  How would I add the first Batch ID to the database for new customers?

Also, how would I return this Batch ID to the original from so that the user can see what batch has been created?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
I use something like the following.  I use a string as the UniqueCD to allow for either string or numeric values, and convert the Code that is passed into the function to a string for that reason.

Public Function fnNextNumber(TableName as string, Code as Variant) as Long

   Dim rst As DAO.Recordset
   Dim strSQL as string

   strSQL = "SELECT * FROM tbl_NextNumber " _
               & "WHERE [TableName] = '" & TableName & "'"
   Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenTable, dbfailonerror)

   rst.FindFirst "[UniqueCD] = '" & cstr(Code) & "'"
   if rst.NoMatch then
        rst.AddNew
        rst!TableName = TableName
        rst!UniqueCD = cstr(Code)
        fnNextNumber = 1
        rst!NextVal = 2
   Else
        rst.Edit
       fnNextNumber = rst!NextVal
       rstNextVal = rstNextVal + 1
   endif
   rst.Update
   rst.close
   set rst = nothing

End Function
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
you need two different fields.  One field for the autoincrement values and another for the batch sequence values.  I implemented a sequencing in one of my applications in two passes.  The first pass added the rows.  I then queried the database to get the lowest autoincrement value for that client.  Using that value in an Update query, I was able to let the database to all of those sequence calculations.

From your description, it isn't clear to me how you want multiple batch rows numbered.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
> How would I add the first Batch ID to the database for new customers?

You can use:

rst!batch_number = 1 + Nz(DMax("batch_number", "tbl_batches", "customer_id = '" & Me.customer_id & "'"), 0)

/gustav
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now