Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto Increment number

Posted on 2014-04-06
9
Medium Priority
?
1,000 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
ID: 39981327
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 49

Expert Comment

by:Dale Fye
ID: 39981353
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 93

Expert Comment

by:Patrick Matthews
ID: 39981707
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:anthonytr
ID: 39981797
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
 

Author Comment

by:anthonytr
ID: 39981808
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
ID: 39981813
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 49

Expert Comment

by:Dale Fye
ID: 39981982
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 46

Expert Comment

by:aikimark
ID: 39982149
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 52

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 39982274
> 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

885 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