Automatically generated number (1 Viewer)

Brian Martin

Registered User.
Local time
Today, 22:49
Joined
Jul 24, 2002
Messages
68
Each time a user enters a new record I want to automatically generate the next consecutive number. I want the format to be -
T-0001.......T-9999. What is the best way of doing this?
 

Mile-O

Back once again...
Local time
Today, 22:49
Joined
Dec 10, 2002
Messages
11,316
Brian Martin said:
What is the best way of doing this?

How about just using an autonumber and formatting it to the way you wish?
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Brian,

Here you are

IMO
 

Attachments

  • t-0000.zip
    24.3 KB · Views: 164

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
Customise number

I have utilised the information from this thread to set up something similar in a Database I am creating. The example that was posted was excellent and exactly what I wanted to do.

My problem is that I can't see how or where the tblalphanum and tblcounter are connected. - Yes I am blonde.

Also I want to change the T used in the example to being a C? Where is this set?
 

jfgambit

Kinetic Card Dealer
Local time
Today, 22:49
Joined
Jul 18, 2002
Messages
798
In the design of the table set the field to be an Autonumber, and add the following to the FORMAT field "C-"0000.
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Re: Customise number

jodilees said:


Also I want to change the T used in the example to being a C? Where is this set?


Change this line of code:

strANum = Chr$(intB + 84) & Chr$(intA + 183) & Format$(intNum, "0000")

to this

strANum = Chr$(intB + 67) & Chr$(intA + 183) & Format$(intNum, "0000")

The number 84 is a capital letter T and 67 is a capital C.

The beauty of using this rather than Autonumber is if you have to delete all the records in the table, all that needs to be done to restart the count to 'C-0001' is change the tblCounter back to '1'

IMO
 

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
Ok so I will try that. What about the two tables. How are they linked. I have created a database with similar tables but cant get the tblcounter to work. In other words I don't understand it. Can anyone shed any further light on this.

Regards

Jodi
 

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
Hi Everyone

Can anyone please shed light on how the two tables in the example of this post are linked.

I don't quite understand where they get the links and in trying to create a similar table set up I can't get them to refer to each other.

Regards

Jodi
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Can you post your db?

IMO
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Add this code to the AfterUpdate event of the field where the new data will be entered. Change 'YOUR NAME FIELD' to the name of that field and 'YOUR ID FIELD' to the name of that field that contains the ID.


Private Sub YOUR NAME FIELD_AfterUpdate()
Dim DB As DAO.Database
Dim rst As DAO.Recordset
Dim lngCntr As Long
Dim intRetry As Integer
Dim intNum As Integer, intA As Integer, intB As Integer
Dim strANum As String

On Error GoTo ErrorAlphaNumGenerate
If IsNull(Me!YOUR NAME FIELD) Or Not IsNull(Me!YOUR ID FIELD) Then
Cancel = True
Else
Set DB = CurrentDb()
Set rst = DB.OpenRecordset("tblCounter", DB_OPEN_DYNASET) 'OPEN THE TABLE WITH THE COUNTER
rst.MoveFirst
rst.Edit
rst!Value = rst!Value + 1
rst.Update
lngCntr = CLng(rst!Value) - 1
intNum = lngCntr Mod 1000
intA = (lngCntr \ 1000) Mod 26
intB = (lngCntr \ 1000) \ 26

'GENERATE THE ALPHANUMBER
strANum = Chr$(intB + 67) & Chr$(intA + 183) & Format$(intNum, "0000")
AlphaNumGenerate = strANum
Me!RecID = strANum
ExitAlphaNumGenerate:
Exit Sub
ErrorAlphaNumGenerate:
If Err = 3188 Then
intRetry = intRetry + 1
If intRetry < 100 Then
Resume
Else 'Time out retries
MsgBox Error$, 48, "Another user editing this number"
Resume ExitAlphaNumGenerate
End If
Else 'Handle other errors
MsgBox Str$(Err) & " " & Error$, 48, "Problem Generating Number"
Resume ExitAlphaNumGenerate
End If
End If
End Sub

Hope this helps

IMO
 

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
I have entered the code that was mentioned and still it is not working. I had already done this, perhaps there is an error in it somewhere, I can't find it though.

Thanks so much for your help IMO. I am posting the DB for you to see perhaps you can spot it.

Regards

Jodi
 

Attachments

  • client service and repair.zip
    72.9 KB · Views: 145

Brian Martin

Registered User.
Local time
Today, 22:49
Joined
Jul 24, 2002
Messages
68
Thanks IMO!!!

By the Way! Thankyou for your help IMO. It was the most helpful reply i've ever had on these forums!
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Re: Thanks IMO!!!

Brian Martin said:
By the Way! Thankyou for your help IMO. It was the most helpful reply i've ever had on these forums!

Glad it helped

IMO
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
jodilees said:
I have entered the code that was mentioned and still it is not working. I had already done this, perhaps there is an error in it somewhere, I can't find it though.

Thanks so much for your help IMO. I am posting the DB for you to see perhaps you can spot it.

Regards

Jodi


Here you are. The only mistake you made was not setting the field 'ClientName' AfterUpdate properties to [Event Procedure]. It works fine now.

IMO
 

Attachments

  • client service and repaired.zip
    74.5 KB · Views: 132

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
Thankyou so much IMO.

I thought it would be something simple for someone else, as I said earlier I am blonde and I do have blonde moments from time to time.

Regards

Jodi
 

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
Help Again

I have just added another table to the Database. I want it to do similar to the last one but with different format of Autonumber.

I understand what you are saying about the event procedure but am unsure where to update it. Also I need to know the code to put the letter R at the front of each record.

Is there a site that contains this information?

Regards

Jodi
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Jodi,

Attached is an updated version of your db with some screen shots of where to enter the code.

This line of the code

strANum = Chr$(intB + 65) & Chr$(intA + 183) & Format$(intNum, "0000")

is where you can change the format of the auto number. In this case the number 65 is the letter 'A', 66 would be 'B', 67 'C' and so on. The number 183 is the '-' , and '0000' is how many zero's you want to show.

Hope this helps. Let me know how you get on

IMO
 

Attachments

  • client service and repair.zip
    37.3 KB · Views: 96

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
here's the screen shots

IMO
 

Attachments

  • client service and repair screen.zip
    80.9 KB · Views: 99

jodilees

Registered User.
Local time
Today, 22:49
Joined
Nov 17, 2002
Messages
40
IMO,

Your a legend, I think I have almost worked it out thanks to your expertise.

My next drama is when I go to enter into the form I get a record saying no current record. Then when I click on ok it says that the primary key contains a null value and cannot be saved.

Once again I am sure it is a problem that is relatively easily solved, but my brain is just not working to well this week, or even this year!

Thankyou once again.

Regards

Jodi
 

IMO

Now Known as ___
Local time
Today, 22:49
Joined
Sep 11, 2002
Messages
723
Jodi,

Here's some things to check in the code:

Make sure the code is in the AfterUpdate event of the first field and NOT the ID field.
Private Sub YOURFIRSTFIELDNAME_AfterUpdate()

Check that in the properties for that field it says [Event Procedure] on the AfterUpdate line of the properties box.

Check this line of the code

If IsNull(Me!YOURFIRSTFIELDNAME) Or Not IsNull(Me!YOUR ID FIELD) Then

YOURFIRSTFIELDNAME has to be the name of the text box on the form, ie me!ClientName
YOUR ID has to be the name of the ID text box on the form, ie me!RecID


This line of code

Set rst = DB.OpenRecordset("tblCounter", DB_OPEN_DYNASET)

tblCounter has to be the name of your new counter table

and this line

Me!RecID = strANum

has to be the the name of your new id text box on the form, ie Me!YOURIDFIELD = strANum


That should be it. If you get stuck just post a stripped down version of your DB and I'll look at it for you.

Hope this helps, let me know

IMO
 

Users who are viewing this thread

Top Bottom