473,441 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

AutoNumber and Input Mask question

damonreid
114 Expert 100+
Is it possible to make a field that is not a number field into an auto number?
I shall try to explain better.

I have a field with the mask AAA-00-000 where AAA is a category code, the first set of numbers is the year (e.g. 07 for this year) and the final set of numbers is a auto number relating to both the category code and year (you should be able to have AAA-06-001, AAA-07-001 and BAA-07-001) but instead of having people look up the next number themselves it would be to nice to have it auto populate once they pick the category code.

Is this possible?
Jul 9 '07 #1
16 8331
kepston
97 Expert
On your form, in the AfterUpdate event of your category control, set the DefaultValue for your AAA-00-000 field.
You will probably need to use the Dmax() function to get the highest value in use.
Jul 9 '07 #2
damonreid
114 Expert 100+
Thank you very much for this pointer.
I will get around to this early next week and will report back on how it goes.
Jul 10 '07 #3
hyperpau
184 Expert 100+
Thank you very much for this pointer.
I will get around to this early next week and will report back on how it goes.
I have done an access file like this.
I have a form populate an autonumber in this format:

HPES 07070001

where the first four numbers are the year and month respectively
then the last four are autonumbers incrementing by one.

If this is what you want, I can give you pointers.

on your tabel, make sure you have an autonumber field. But you would
not use this field for your customized autonumber. It's just there
to be as an index (primary key) of your records.

in your table, add two additional fields aside from the autonumber field.
1st field should be a number with long integer as the format.
2nd field should be String. This is where we would record the customized
autonumber.

Now, I would assume that your form would be opened from a switchboard
in add mode. (meaning, you cannot go to other records and you you go to a new record as soon as the form loads)

let's say you name the 1st field (the number field) " lngNum " and the second
field (string field) as " strID ".

Add this on your form_load() event.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load( )
  2.  
  3. If Me.NewRecord Then
  4.  
  5.      ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  6.      Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
  7.  
  8.      'Customize the ID with Year, then attach the incremented lngNum
  9.      Me!StrID = "AAA-" & Format(Date, "yy") & Format(Me!lngNum, "0000")
  10.  
  11. Else
  12.  
  13.      Me!StrID = Me!StrID
  14.  
  15. End If
  16.  
  17. End Sub
Now as soon as you open that form in add mode, it automaticaly generates that customized ID you want, and increments it by 1. :)

Hope it works for you.
Jul 10 '07 #4
missinglinq
3,532 Expert 2GB
As stated, you should use Dmax() to generate your own incrementing number. You should really never use an Autonumber datatype for any field that will be manipulated by the user or thru code. Autonumbers are meant to be used for "housekeeping" by Access! They're too many things that can mess up their progression!

Linq
Jul 10 '07 #5
damonreid
114 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load( )
  2.  
  3. If Me.NewRecord Then
  4.  
  5.      ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  6.      Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
  7.  
  8.      'Customize the ID with Year, then attach the incremented lngNum
  9.      Me!StrID = "AAA-" & Format(Date, "yy") & Format(Me!lngNum, "0000")
  10.  
  11. Else
  12.  
  13.      Me!StrID = Me!StrID
  14.  
  15. End If
  16.  
  17. End Sub
This is great, however after the first addition I get an error (I can enter AAA-07-0001 but when I go to add another number, AAA-07-0002 I get the following error:

Run-time error '13':

Type mismatch


Relating to the following code
Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
Jul 11 '07 #6
kepston
97 Expert
This is great, however after the first addition I get an error (I can enter AAA-07-0001 but when I go to add another number, AAA-07-0002 I get the following error:

Run-time error '13':

Type mismatch


Relating to the following code
Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
That's because strID is a string and lngNum is a number.
You will need to strip the number part off strID.
Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(Dmax("Right(strID,4)","NameOfTable"),0) + 1
When you have multiple product codes, you should also include a criteria for the left part of strID, so that you are only looking for the max value for the current product and year i.e. the AAA-00 part
Something like
Expand|Select|Wrap|Line Numbers
  1. strCriteria="Left([strID],6)=[strProdCode] & '-' & Format(Date,'yy')"
where [strProdCode] is the control you enter the AAA part into
Jul 11 '07 #7
damonreid
114 Expert 100+
Ok so I have changed the code so there is a Combo Box called PrgCodeID and when you select an ID it should pick a project number based on that ID.

AAA-07-0001
Where;
AAA is the Project Code ID
07 is the year
001 is the number that increases

Expand|Select|Wrap|Line Numbers
  1. Private Sub PrgCodeId_AfterUpdate()
  2. Dim strProdCode As String
  3.  
  4.     If Me.NewRecord Then
  5.     strProdCode = Me.PrgCodeId.Value
  6.  
  7.          ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  8.          'Me!lngNum = Nz(DMax("[strID]", "Project Details"), 0) + 1
  9.          Me!lngNum = Nz(DMax("Right(strID,4)", "Project Details"), 0) + 1
  10.          'Customize the ID with Year, then attach the incremented lngNum
  11.          strCriteria = "Left([strID],6)=[strProdCode] & '-' & Format(Date,'yy')"
  12.          Me!strID = Me.PrgCodeId.Value & "-" & Format(Date, "yy") & "-" & Format(Me!lngNum, "000")
  13.  
  14.     Else
  15.  
  16.          Me!strID = Me!strID
  17.  
  18.     End If
  19. End Sub
I have tried to change the code to reflect this but what happens now is every code returns XXX-07-000 regardless of how many times I implement it.

I have;
ABC-07-000
ABD-07-000
ABC-07-000
ABC-07-000
ABD-07-000
in the table now.
Jul 11 '07 #8
damonreid
114 Expert 100+
Ok I am an idiot... changed the 4 to a 3.

I am still getting the next number for all codes and not just the particular one now.


ABC-07-002
ABC-07-003
ABB-07-004
ABC-07-005
ABB-07-006
ect...
Jul 11 '07 #9
damonreid
114 Expert 100+
Ok I made some changes and got it working.

First I made a query called ProjectCode;
Expand|Select|Wrap|Line Numbers
  1. SELECT [Project Details].autonumber, [Project Details].lngNum, [Project Details].strID
  2. FROM [Project Details]
  3. WHERE ((([Project Details].strID) Like [Forms]![AddNewProject]![PrgCodeId] & "*"));
Then I changed the code on the form to;
Expand|Select|Wrap|Line Numbers
  1. Private Sub PrgCodeId_AfterUpdate()
  2. Dim strProdCode As String
  3.  
  4.     If Me.NewRecord Then
  5.     strProdCode = Me.PrgCodeId.Value
  6.  
  7.          ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  8.          Me!lngNum = Nz(DMax("Right(strID,3)", "ProjectCode"), 0) + 1
  9.          'Customize the ID with Year, then attach the incremented lngNum
  10.          strCriteria = "Left([strID],3)=[strProdCode] & '-' & Format(Date,'yy')"
  11.          Me!strID = Me.PrgCodeId.Value & "-" & Format(Date, "yy") & "-" & Format(Me!lngNum, "000")
  12.  
  13.     Else
  14.  
  15.          Me!strID = Me!strID
  16.  
  17.     End If
  18. End Sub
This solved the problem for me.
Jul 11 '07 #10
damonreid
114 Expert 100+
One quick question I hope you can help with.


Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(DMax("Right(strID,3)", "ProjectCode"), 0) + 1
If strID had a space in it say str ID how would I put that in this line? I have tried and _ and single and double quotes around it but none of the appear to work.
Jul 11 '07 #11
JKing
1,206 Expert 1GB
If your field name has spaces in it use square brackets. i.e. [Field With Many Spaces] Otherwise the compiler will see a variable for each word in your fieldname.
Jul 11 '07 #12
damonreid
114 Expert 100+
The [] didn't work either... strange.

What I have tried to do is get the string using the strID field and then enter that information into my Project Number field. This is a little cumbersome but appears to work.


Expand|Select|Wrap|Line Numbers
  1. Private Sub PrgCodeId_AfterUpdate()
  2. Dim strProdCode As String
  3.  
  4.     If Me.NewRecord Then
  5.     strProdCode = Me.PrgCodeId.Value
  6.  
  7.         ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  8.         Me!lngNum = Nz(DMax("Right(strID,3)", "ProjectCode"), 0) + 1
  9.         'Customize the ID with Year, then attach the incremented lngNum
  10.         strCriteria = "Left([strID],3)=[strProdCode] & '-' & Format(Date,'yy')"
  11.         Me!strID = Me.PrgCodeId.Value & "-" & Format(Date, "yy") & "-" & Format(Me!lngNum, "000")
  12.  
  13.  
  14.  
  15.     Else
  16.  
  17.         Me!strID = Me!strID
  18.  
  19.     End If
  20.         Me.Project_Number = strID.Value
  21. End Sub
Jul 12 '07 #13
kepston
97 Expert
The [] didn't work either... strange.

What I have tried to do is get the string using the strID field and then enter that information into my Project Number field. This is a little cumbersome but appears to work.


Expand|Select|Wrap|Line Numbers
  1. Private Sub PrgCodeId_AfterUpdate()
  2. Dim strProdCode As String
  3.  
  4.     If Me.NewRecord Then
  5.     strProdCode = Me.PrgCodeId.Value
  6.  
  7.         ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  8.         Me!lngNum = Nz(DMax("Right(strID,3)", "ProjectCode"), 0) + 1
  9.         'Customize the ID with Year, then attach the incremented lngNum
  10.         strCriteria = "Left([strID],3)=[strProdCode] & '-' & Format(Date,'yy')"
  11.         Me!strID = Me.PrgCodeId.Value & "-" & Format(Date, "yy") & "-" & Format(Me!lngNum, "000")
  12.  
  13.  
  14.  
  15.     Else
  16.  
  17.         Me!strID = Me!strID
  18.  
  19.     End If
  20.         Me.Project_Number = strID.Value
  21. End Sub
Let me apologise for the error in post #7, which you corrected by post #9.
(You were getting 4 characters because it was appending the '1', and not increasing by 1)
Anyway, continuing...
The strCriteria isn't currently being used anywhere.
Change it to
Expand|Select|Wrap|Line Numbers
  1. strCriteria = "Left([strID],3)='" & [strProdCode] & "-' & Format(Date,'yy')"
If you move it from line 10 and put it in after line 7.
You can then use it in the DMax() function.
Expand|Select|Wrap|Line Numbers
  1. DMax("Right(strID,3)", "ProjectCode", strCriteria)
I've not tested this, but I will try and keep up with you today
Jul 12 '07 #14
hyperpau
184 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load( )
  2.  
  3. If Me.NewRecord Then
  4.  
  5.      ' Increment the lngNum field by One, if field is Null, set as 0 then add 1
  6.      Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
  7.  
  8.      'Customize the ID with Year, then attach the incremented lngNum
  9.      Me!StrID = "AAA-" & Format(Date, "yy") & Format(Me!lngNum, "0000")
  10.  
  11. Else
  12.  
  13.      Me!StrID = Me!StrID
  14.  
  15. End If
  16.  
  17.  
  18.  
  19.  
  20.  
  21. End Sub
This is great, however after the first addition I get an error (I can enter AAA-07-0001 but when I go to add another number, AAA-07-0002 I get the following error:

Run-time error '13':

Type mismatch


Relating to the following code
Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(Dmax("[strID]", "NameOfTable"), 0) + 1
Sorry, I mistyped the code. instead of strID, you should use lngNum
Expand|Select|Wrap|Line Numbers
  1. Me!lngNum = Nz(Dmax("[lngNum]", "NameOfTable"), 0) + 1
you will just make it so complicated if you use the Left or Right methods.

Again, this code will only work when the form loads in add entry mode. If you would like to add a new entry, you must close the form and reopen it in add entry mode.
If you want to add a new record without closing the form, try changing the Form_load() to Form_current()
Jul 12 '07 #15
kepston
97 Expert
you will just make it so complicated if you use the Left or Right methods.
Quite right! And presumably less efficient.

The criteria is still required though, otherwise you will have the same situation as in post #9 with all project codes running in one sequence rather than each having it's own.

Aside:
Again, this code will only work when the form loads in add entry mode. If you would like to add a new entry, you must close the form and reopen it in add entry mode.
If you want to add a new record without closing the form, try changing the Form_load() to Form_current()
Would OnCurrent increase the number on every visit to the record?
Equally, if the Form was opened to Edit, would the first record be altered?
Jul 12 '07 #16
Lama ani lo yahol liktov beivrit?






כושר
Sep 1 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: dude | last post by:
hello how would i make an input mask that only makes the first letter a capitol one? i've been playing around and testing various masks, also tried the wizard, but i've had no luck. could...
2
by: Ellen Manning | last post by:
I've got an A2K database with a link to a table in another A2K database. This linked table contains SSN formatted with the SSN input mask. I'm trying to use a dlookup using this linked table. ...
9
by: Paul | last post by:
hi, is there an input mask i could use on a report to do the following: (1) if i enter "THISISATEST" on my form, i want the text box on my report to display: "T H I S I S A T E S T". (2) if...
2
by: johnp | last post by:
Hi, Our Tech department updated users to Office 2003 this week. Now the input mask in one of the applications is showing up as: (###) ###-### The input mask wizard works correctly when I...
7
by: F. Michael Miller | last post by:
I have a db with Access front end, sql back, linked tables. I need to be able to change input masks at the table level in code. Any ideas? Thanks!
3
by: AA Arens | last post by:
When I want the first character of a field to be Uppercased, I need to make an input mask, like >L< followed by ??????? for example. But this mask creates ____ in an unfilled field, which I don't...
9
by: msnews.microsoft.com | last post by:
Hello. How can I set input mask for TextBox? Or can I use for mask input some other control?
11
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
7
desklamp
by: desklamp | last post by:
I'm a total Access newbie, please bear with me! Using Win2K/Access 2003. I'm trying to create a table in which I can store IP addresses and other information. According to Microsoft, there is no...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.