Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Increment number

Posted on 2014-04-06
9
Medium Priority
?
990 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 48

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 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.

 

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 48

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 51

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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 …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

715 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