473,395 Members | 1,462 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,395 software developers and data experts.

Primary Key

77
Dear Friends,

I have a primary Key called the Solar Lantern No in a table called Beneficiary. The number has a format, namely, 4WS0-01; 02; 03 etc. the solar lantern no starts with 4WS0. how do we do this in Access . the Solar Lantern No increases on its own....

Expand|Select|Wrap|Line Numbers
  1. the field in the recordset Beneficiary are:
  2.  
  3. solar lantern no, PK
  4. Name, Text
  5. Motivator, Text
  6. cost, Numeric
looking forward to hear from you.
Thanks
Sajit
Apr 9 '08 #1
6 1236
mshmyob
904 Expert 512MB
Your question was vague but I assume you want to increment the last part of your string by 1 each time based on a trigger.

Here is the code.

Expand|Select|Wrap|Line Numbers
  1. Dim vLng As Long
  2.     Dim vValString As String
  3.     Dim vText As String
  4.     Dim vLen As Long
  5.  
  6.     ' get the length of the string
  7.     vLen = Len(Trim(Me.Text10))
  8.     ' extract the last set of numbers
  9.     vText = Mid(Trim(Me.Text10), 6, vLen - 5)
  10.     'convert the number string to numeric
  11.     vLng = Val(vText)
  12.     ' increment the value by 1
  13.     vLng = vLng + 1
  14.     ' convert the number back to a string
  15.     vValString = CStr(vLng)
  16.     ' conecate the original string prefix and the new numeric string
  17.     vValString = Mid(Trim(Me.Text10), 1, 5) & "0" & vValString
  18.     ' do what you want with the new string
  19.     Me.Text12 = vValString
  20.  
cheers,

Dear Friends,

I have a primary Key called the Solar Lantern No in a table called Beneficiary. The number has a format, namely, 4WS0-01; 02; 03 etc. the solar lantern no starts with 4WS0. how do we do this in Access . the Solar Lantern No increases on its own....

Expand|Select|Wrap|Line Numbers
  1. the field in the recordset Beneficiary are:
  2.  
  3. solar lantern no, PK
  4. Name, Text
  5. Motivator, Text
  6. cost, Numeric
looking forward to hear from you.
Thanks
Sajit
Apr 9 '08 #2
ADezii
8,834 Expert 8TB
Dear Friends,

I have a primary Key called the Solar Lantern No in a table called Beneficiary. The number has a format, namely, 4WS0-01; 02; 03 etc. the solar lantern no starts with 4WS0. how do we do this in Access . the Solar Lantern No increases on its own....

Expand|Select|Wrap|Line Numbers
  1. the field in the recordset Beneficiary are:
  2.  
  3. solar lantern no, PK
  4. Name, Text
  5. Motivator, Text
  6. cost, Numeric
looking forward to hear from you.
Thanks
Sajit
The following code will produce the next [Solar Lantern No] in sequence:
Expand|Select|Wrap|Line Numbers
  1. "4WSO-" & Format$(Val(Right$(DLast("[Solar Lantern No]", "Beneficiary"), 2)) + 1, "00")
Apr 9 '08 #3
ADezii
8,834 Expert 8TB
Dear Friends,

I have a primary Key called the Solar Lantern No in a table called Beneficiary. The number has a format, namely, 4WS0-01; 02; 03 etc. the solar lantern no starts with 4WS0. how do we do this in Access . the Solar Lantern No increases on its own....

Expand|Select|Wrap|Line Numbers
  1. the field in the recordset Beneficiary are:
  2.  
  3. solar lantern no, PK
  4. Name, Text
  5. Motivator, Text
  6. cost, Numeric
looking forward to hear from you.
Thanks
Sajit
The following code will produce the next [Solar Lantern No] in sequence, in the format 4WSO-00:
  1. Retrieve the Last [Solar Lantern No].
  2. Extract the last 2 values.
  3. Increment it by 1.
  4. Reformat the Number "00".
  5. Append it to the Base String.
  6. NOTE: Logic only valid up to 4WSO-99.
Expand|Select|Wrap|Line Numbers
  1. "4WSO-" & Format$(Val(Right$(DLast("[Solar Lantern No]", "Beneficiary"), 2)) + 1, "00")
Apr 9 '08 #4
mshmyob
904 Expert 512MB
Both code will work. The only difference is my has no limit and I kept the leading 0 for you (easy to remove if you want). I broke it out so the logic should be easy to understand. Adezi has spelled out the logic nicely.

cheers,

The following code will produce the next [Solar Lantern No] in sequence, in the format 4WSO-00:
  1. Retrieve the Last [Solar Lantern No].
  2. Extract the last 2 values.
  3. Increment it by 1.
  4. Reformat the Number "00".
  5. Append it to the Base String.
  6. NOTE: Logic only valid up to 4WSO-99.
Expand|Select|Wrap|Line Numbers
  1. "4WSO-" & Format$(Val(Right$(DLast("[Solar Lantern No]", "Beneficiary"), 2)) + 1, "00")
Apr 9 '08 #5
ADezii
8,834 Expert 8TB
Both code will work. The only difference is my has no limit and I kept the leading 0 for you (easy to remove if you want). I broke it out so the logic should be easy to understand. Adezi has spelled out the logic nicely.

cheers,
The only difference is my has no limit and I kept the leading 0 for you (easy to remove if you want).
If I am not mistaking you, you simply cannot remove the leading 0 and expect to generate the next number in sequence. If there is no limit to the numbering system then perhaps it needs to be reformatted to:4WSO-000000 which would allow values from 4WSO-000001 to 4WSO-999999 inclusive.
Apr 9 '08 #6
mshmyob
904 Expert 512MB
I kept it the way he orginally had it so the results would be

4WSO-01
4WSO-02
.
.
4WSO-010
4WSO-011
.
etc.

I don't see a problem except the limitation of the field length he has set.

If he wants to drop the 0 after it reaches 10 then he just need to put an IF statement that looks to see if the new number is greater than 9 then drop the leading 0.

cheers,

If I am not mistaking you, you simply cannot remove the leading 0 and expect to generate the next number in sequence. If there is no limit to the numbering system then perhaps it needs to be reformatted to:4WSO-000000 which would allow values from 4WSO-000001 to 4WSO-999999 inclusive.
Apr 9 '08 #7

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
9
by: 101 | last post by:
Taking a course on SQL. They are saying you can get better performance by having multiple files for a group. They then graphically show an example of "Primary" with multiple data files. I have...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
5
by: shenanwei | last post by:
I have a primary server and backup server located in different physical sites. The primary server is live and ship logs to backup site every 5 minutes. The primary server is being full online...
4
by: misscrf | last post by:
Ok I have 2 issues. 1) I have a main candidate form with 2 subforms on a tab control: http://www.geocities.com/misscrf/images/contactcontinouscheckbox.jpg I have been encouraged to add these...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.