473,474 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

automatic update of count

65 New Member
i am using access 2010 and i have a question.

I have a form and all data entered into this form is saved into a table (as a new record).
so each entry has a IDnumber (which is not unique).
this ID has a text field in the form, where the user enters the ID.
What i want is, the next time a user wants to enter some data, the text field for this ID automatically has the last ID plus one.

Is there anyway to do this??

Thanks a lot!! hope I was clear enough...
Feb 17 '16 #1
25 1635
jforbes
1,107 Recognized Expert Top Contributor
That's what the AutoNumber datatype is designed for. Will it not work for you?
Feb 17 '16 #2
saranmc
65 New Member
I do know about the autonumber, but that is not suitable in this situation. this ID has to be entered by the user. it goes like this "D-001, D-002, D-003......"

so if D001 is already filled in the table, i want D002 to show up on the form the next time the user enters new information...
Feb 17 '16 #3
jforbes
1,107 Recognized Expert Top Contributor
This Question is very similar: Autonumber Using alphanumeric (text and number) data type.

If you struggle adapting it, please post back what code you have developed so far, table and column names, and what problems you are experiencing along with anything else you think may be helpful.
Feb 17 '16 #4
NeoPa
32,556 Recognized Expert Moderator MVP
SaranMC:
so each entry has a IDnumber (which is not unique).
Are you sure this is what you meant to say? That would be extremely unusual.

Assuming not, JForbes gave a link that should tell you all you need to know.
Feb 18 '16 #5
saranmc
65 New Member
@NeoPa, it is what i meant to say.

Because, the IDnumber is not exactly an ID number.
I have a list of part numbers and Project numbers.
so many part numbers can belong to a Project.
so it is the Project number that i am talking about.

so the first 10 part numbers can belong to D001, the next 5 belong to D002 and so on.....

so i want the last Project number + 1 to appear on the form.
for the next list of part numbers to be saved.

I hope this makes things a Little more clear..
sorry for confusing you.
thanks.
Feb 18 '16 #6
saranmc
65 New Member
@jforbes i tried out the code.

im getting an error " wrong Syntax "

actually the code is not too clear to me...
where do I put in my table and field Name??

Expand|Select|Wrap|Line Numbers
  1. 1.Public Function getKey(ByRef sTable As String, ByRef sField As String, ByRef iKeyLen As Integer, ByRef sPrefix As String) As String
  2.      Dim sLastKey As String
  3.      sLastKey = DMax(sField, sTable)
  4.      getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
  5.  End Function
  6.  
i used this code as the Default property of the text box in my form where the data code is supposed to appear..

thanks
Feb 18 '16 #7
jforbes
1,107 Recognized Expert Top Contributor
I'm confused myself. I thought you were attempting to populate a Primary Key with an Alphanumeric value, but after reading your last couple of posts, I really don't understand what you are attempting to do.

What is it that you are trying to accomplish?
Feb 18 '16 #8
saranmc
65 New Member
no no... ist not a Primary key... Thats why i mentioned that its not unique.

ok, there are Projects, numbered D001, D002, D003......
then there are part numbers... they are Long numbers like L021457812, L3147925100......

many part numbers can belong to one Project.

the table is supposed to contain part numbers with their corresponding Project number.
the Project number the user writes in the form. if the last Project was D009 this would be D010..

for eg, i say part number L0346512787 belongs to D001. this is saved. I have just named the Project D001.

so the next time I have a new part number and i want to put it into a new Project, i have to Name the Project D002.
But i want this number to appear automatically. the code should check the table for the last Project number, add 1 to it and repopulate the text field where i have to write the Project number.

I hope now its clear..
Feb 18 '16 #9
jforbes
1,107 Recognized Expert Top Contributor
So you don't a have a table for Projects?
Feb 18 '16 #10
saranmc
65 New Member
yes i do... the user enters the Details of the Project and the Project number and it saves it to a table.

aah now i understand your confusion!!! so silly of me... i was looking at the wrong table!

So i could look it up from that Project table. in this table the Project number can be said as a Primary key as it only appears once.
now i tried your code again as the Default property of my text box.. but same error. what am i doing wrong??

thanks jforbes for pointing it out..
Feb 18 '16 #11
saranmc
65 New Member
i dont know where in the code to specify my table and field Name....
Feb 18 '16 #12
jforbes
1,107 Recognized Expert Top Contributor
Well, in that case, I wouldn't use the code I posted, as it probably won't do anything for you, but mess up your data.

So, when you are entering the Project Number and Part Number, has the Project Number you are attempting to use already been populated in the Project Table?
Feb 18 '16 #13
saranmc
65 New Member
no.. in the Project table the Project data is going to be entered new with a new Project number (the next number).

please Forget the part number, that is another table which has both the part number and Project number repeatingly.. my mistake i was wrongly looking at that table.

the Project table has only the Project number, Name, date..... all ONLY ONCE....
Feb 18 '16 #14
jforbes
1,107 Recognized Expert Top Contributor
OK, so we are back to creating a new Primary Key for the Project Number?
Feb 18 '16 #15
saranmc
65 New Member
yes thats right.....
Feb 18 '16 #16
jforbes
1,107 Recognized Expert Top Contributor
In that case, if you have put this code in a code module:
Expand|Select|Wrap|Line Numbers
  1. Public Function getKey(ByRef sTable As String, ByRef sField As String, ByRef iKeyLen As Integer, ByRef sPrefix As String) As String
  2.      Dim sLastKey As String
  3.      sLastKey = DMax(sField, sTable)
  4.      getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
  5.  End Function
You can use some code like this as the DefaultValue Property of your Project Number field on the Form that you want use to add new Projects:
Expand|Select|Wrap|Line Numbers
  1. =getKey("NameOfTheProjectTable", "NameOfTheProjectNumberField", 4, "D")
You'll have to replace NameOfTheProjectTable and NameOfTheProjectNumberField with the actual Table and Column names.
Feb 18 '16 #17
saranmc
65 New Member
thanks for the code jforbes...
just one question.. the second code, to call the getkey..

i put it into the defaultvalue property of the text box right?? into the Expression builder??

Its giving me an error..
Feb 19 '16 #18
saranmc
65 New Member
Ahh... I figured it out now...

now there is no error but the text box just Shows " #Name? "...

is it not able to recognise the value??
I entered D001 into the table and refreshed the form but D002 still does not appear....
Feb 19 '16 #19
saranmc
65 New Member
I used the functions Option from Expression elements in the Expression builder and it gave me a similar code to yours.

Expand|Select|Wrap|Line Numbers
  1. =getKey([DATA_Table];[Datacode];4;[D])
Feb 19 '16 #20
jforbes
1,107 Recognized Expert Top Contributor
That's not going to work. The Function is expecting a string for the Table Name and another one for the Column Name as well as the Prefix to use. So putting a references in there will just confuse Access. When Access sees something like that, it tries to evaluate it and when it sees those Brackets instead of Quotes it attempts to go out and get the Value that is included in the brackets. In the case of DateCode, it will look on the current record of the current form for the value stored in the field Datecode and error out if it's not there. Same goes for Data_Table and D and if any of those three can't be resolved, the whole expression errors out and that #Name error is displayed as the value.

So in your case, you'll probably want to pass string values to the Function instead of values from the current recordset. The following code will more likely work, but I hope you are trying to understand this code as you will have a much better time supporting it if you understand it:
Expand|Select|Wrap|Line Numbers
  1. =getKey("DATA_Table";"Datacode";4;"D")
Lastly, you don't have to use the expression builder to put a value in a property. You can copy and paste the code, or type it in directly.
Feb 19 '16 #21
saranmc
65 New Member
sorry for the late reply jforbes....i tried again by pasting the exact code into the Default value property.....
but again it just says invalid Syntax.....
Feb 23 '16 #22
jforbes
1,107 Recognized Expert Top Contributor
What happens if you type:
Expand|Select|Wrap|Line Numbers
  1. ?getKey("DATA_Table";"Datacode";4;"D")
into the immediate window?
Feb 23 '16 #23
saranmc
65 New Member
i tried again and now i realised something....
the code works.... but it only works starting with D002...
that means it Fails for the first value...when the table is empty...
it should actually Show D001 but it gives an error... "INVALID use of Null"..
your orginal code in #21....
Feb 24 '16 #24
jforbes
1,107 Recognized Expert Top Contributor
Nice job troubleshooting.

The following change should get you up and running. The code expected to find at least one key. The following change should deal with a blank table:
Expand|Select|Wrap|Line Numbers
  1. Public Function getKey(ByRef sTable As String, ByRef sField As String, ByRef iKeyLen As Integer, ByRef sPrefix As String) As String
  2.       Dim sLastKey As String
  3.       sLastKey = Nz(DMax(sField, sTable), sPrefix & "0")
  4.       getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
  5.   End Function
Glad to see you are taking ownership of the code.
Feb 24 '16 #25
saranmc
65 New Member
ahh.. you beat me to the solution... i just figured it out... the usage of " Nz " to tackle this issue... i came back to tell you that i figured it out but i see you have posted your solution....

great.. now it works perfectly!!
i learnt something new today...

thanks a lot jforbes.. Keep up the good work!!
Feb 24 '16 #26

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

Similar topics

1
by: Kelly Olsen | last post by:
I have created an automatic update field in my Vendor_Table that automatically updates the date in that field to respond to any changes made to the record. However, I would like to display this...
0
by: KC | last post by:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnbda/html/updater.asp?_r=1 >-----Original Message----- >I want to add a Windows style automatic update feature to >my C# app....
2
by: anony | last post by:
Maybe this feature is already out there. I guess you could write triggers to do some of this. Often when designing a database I add a start_date and end_date column to the table. The start_date...
0
by: Dean Slindee | last post by:
Any implemented an application that simulates the "automatic update" functionality normally done by Access in VB.NET? Or, know of a posted application example? Thanks Dean S
1
by: =?Utf-8?B?RGF2ZSBDdW5uaW5naGFt?= | last post by:
I'm new at this so I hope I get it right THIS time! Running Windows XP SP2. When I boot up I get the following message: RUNDLL Error loading C:\WINDOWS\SYSTEM32\valcesfk.dll The specified...
4
by: =?Utf-8?B?Zmx5ZXJk?= | last post by:
I have win xp home, automatic updates set for auto at 3:00 am. The download and install happens. (Proved by looking on web update history and in windows directory) however the yellow icon w/ the...
7
by: Otto Carl Marte | last post by:
Hi, When using a CallableStatement with the IBM DB2 Universal JDBC Driver the executeUpdate and getUpdateCount() methods on CallableStatement always return -1. According to the JDBC...
1
by: fxtao | last post by:
my question is how to realize the automatic update of our application programming. Now ,I haven't idea almost.
2
by: =?Utf-8?B?RG91Zw==?= | last post by:
Automatic Update has started installing XP SP3 on my Dell P4-2800 running XP Home. It has been at the same point "Finishing Installation" with details of "Running Processes after install" for over...
1
by: =?Utf-8?B?UHNldQ==?= | last post by:
Hello. System: Win XP Media Centre Edition – Service Pack 3 Somehow, I picked up a variant of VirtuMonde; Symantec Anti-virus, Spybot Search & Destroy etc did not locate it; I discovered it...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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...
1
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: 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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.