Discussion Locked

autogenerating order numbers in access97

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
the admin department want a database of customer enquiries on the network, so that anyone taking an enquiry can add it to the database. so far, so easy. however they want to keep the numbering system they already use (yy/mm/0000). is there any way of automatically generating a number in this format whenever anyone opens the AddNewRecord form? - in words of one syllable please, i do networks, not databases (duh!)

10 replies

by roc180

There are many ways to acomplish this task. The easiest would probably be, to have a hidden field with the controlsource=Table.ID# on your addNewRecord form that whenever a new record is created you a call a function that returns the next number inthe order, then set your hidden field = the new #. If you would like a working example drop me a line. Also make sure to include how your AddNewRecord form works (Datsheet view, tabular fields).

Good Luck

Neal Martinelli

by ishmael

i'm not much cop with access, so i found both these answers a bit hard to follow, but after buying the microsoft press book, i used a query in between the table and the form with a field that calculated the value i required. like so many problems of this nature, its really easy when you know how. on to the next one...
neither of these answers really told me to do that, so i'm going to reject them both, although i'm sure they are both right. unfortunately i just don't know enough to get them to work. sorry, but thanks a lot for your efforts...

by ishmael

okeydokey. the table consists of [id] {autonumber, primary key}, [date] {date, default value date()}, [customer] {text}, [product] {text}, and [enquiry] {memo}. the form contains all the fields except [id]. i need an additional field that contains the month and year information from the [date] field, combined with the value from the [id] field, in the format yy/mm/[id] value. its pretty important that this field is displayed on the form before the operator closes it when adding a new entry. alternatively a way of customising the autonumbering so that it produced the correct format?
i don't know. i'm lost. give me back my cable crimpers...

by donq

This does exactly that for a Building Work Request:

Function AssignWRBID(strTableName As String, strCounterField As String)
On Error GoTo AssignWRBID_Err

Dim dblNewID As Double
Dim strYear As String
Dim strMonth As StringDim strDay As String
Dim strNumber As String
Dim strNextSequenceNumber As String

dblNewID = GetNextCounter(strTableName)

If dblNewID <> -1 Then
strYear = Format$(DatePart("yyyy", Date), "0000")
strMonth = Format$(DatePart("m", Date), "00")
strDay = Format$(DatePart("d", Date), "00")
strNumber = Format$(dblNewID, "0000")
strNextSequenceNumber = "WRB" & strYear & strMonth & strDay & strNumber
Screen.ActiveForm(strCounterField) = strNextSequenceNumber
Else
MsgBox "Can't Add Record; Counter Not Available", MB_OK + MB_CRITICALSTOP + MB_BTN1DEFAULT, "Error Saving Record"
SendKeys "{ESC}{ESC}"
End If

AssignWRBID_Exit:
Exit Function

AssignWRBID_Err:
SendKeys "{ESC}{ESC}"
MsgBox "Error " & Err & ": " & Error$, MB_OK + MB_CRITICALSTOP + MB_BTN1DEFAULT, "AssignID"
Resume AssignWRBID_Exit

End Function

-----------
Function GetNextCounter(strTableName As String) As Double

Dim MyDB As Database
Dim MySet As Recordset
Dim CurrentCounter As Integer
Dim CurrentDate As Double
Dim TodayDate As Double
Dim intLockCount As Integer

On Error GoTo GetNextCounter_Err

intLockCount = 0

'SET THE VARIABLE TodayDate TO TODAY'S DATE.

TodayDate = Date
Debug.Print TodayDate

' OPEN A TABLE ON THE APPROPRIATE TABLE IN THE DATABASE.

Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set MySet = MyDB.OpenRecordset(strTableName & "_ID", DB_OPEN_TABLE, DB_DENYREAD)

' CHECK THAT CURRENT DATE = DATE COUNTER LAST USED.
' IF YES, INCREMENT 1 AND RETURN NEXT COUNTER VA

by donq

Then you will need to add the procedure call to your BeforeInsert event of the form where your users enter their stuff:

Dim varReturn As Variant

varReturn = AssignWRBID("WRB", "wrbPK")

My table name was "WRB" and the field where my return value went was the Primary Key field named "wrbPK". They are the two arguments the procedure looks for.

Also pre-construct your "_ID" table with the fields "dblDate" and "intCounter" in them. You must seed dblDate with a number (suggest35000) and set intCounter default to 0 (zero)

Good Luck - Don Quigg

by ishmael

see my comment on the above answer, and again, thanks a lot, i'm sure it'll make sense when i know a bit more...

by ishmael

aaargh, and i thought i was confused before. itried adding your code to a button on the form, but it just seems to generate a #name? error. i am sure your code is fine, but i think i need a little hand holding. where, exactly, do i stick this code (don't...) and how do i get it to work?
i spose i'd better up the number of points on this one...

This discussion is closed to new replies.