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

How do you obtain the next letters up in the alphabet from existing data?

I have a field in a vehicle table that is a unique code consisting of a 3 digit number and 2 letters. So it looks like this:-
eg.
003AA
003AB
003AC
021JA
021JB
021JC

I have another table that holds new records that I eventually want to add to the first table but the field in the second table only contains the 3 digit number part of this unique code. So the second table looks like this:-

Code, Make, Model
003, AUDI, A4 TDI QUATTRO SPORT
003, AUDI, A4 CABRIOLET
021, FORD, SIERRA LX
021, FORD, SIERRA GLX AUTO

etc...

I want to create a query or something to search the first table for the code on the second table and where I find the 3 digit number (eg. 003), I want to find the next unique code up that I can use, that is not already in use. So I would want it to find new code 003AD for the first vehicle of the example above, because 003AA, 003AB and 003AC are all already in use for other vehicles.

I have no idea how to do this automatically but I am tired of doing this as a manual exercise.

I would really appreciate any help or ideas... Thank you
Aug 10 '10 #1

✓ answered by ADezii

I literally threw this together before going out, but it should at least give you a starting point to reference. Simply pass to the Function a 3-Digit Base Code for which you want generate the next, sequential Code based on those 3-Digits. I listed some Sample Data along with Results, but first a few Assumptions.
  1. Your Table name is tblCodes and consists of a Field named [Code2](TEXT}. Any other Fields in this Table are irrelevant.
  2. The Code always follows the same exact Format (3-Digits, 2 Strings), and is sequential in nature as in: 045AA, 045AB, 045AC, 045AD, 076AA, 076AB, etc.
  3. You will never have a situation where 3 Digits would be followed by a Double 'Z', namely XXXZZ. If you would, the code will have to be modified. For now, it allows for a 'Z' on the back end, and makes the proper adjustments.
  4. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fRetrieveNextAvailCode(strCode As String) As String
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strNewCode As String
    5.  
    6. Set MyDB = CurrentDb
    7.  
    8. 'Retrieve only the Codes that begin with the 3 Characters in strCode,
    9. 'namely 001, 002, 003, 021, 099, 124, etc.
    10. Set rst = MyDB.OpenRecordset("SELECT * FROM tblCodes WHERE Left$([Code2], 3) = '" & _
    11.                               strCode & "'", dbOpenSnapshot)
    12.  
    13. With rst
    14.   If .BOF And .EOF Then     'No Records exist for 3-Digit Code
    15.     MsgBox "No 2-Digit Base Code beginning with [" & strCode & "]", vbExclamation, "No Records"
    16.       .Close: Set rst = Nothing: Exit Function
    17.   End If
    18.  
    19.   .MoveLast     'Retrieve Last Code used (003AD, 098GH, etc.)
    20.  
    21.   If Right$(![Code2], 1) = "Z" Then        'Special case, set Last Character to 'A', then
    22.                                         'Increment 1st Character by 1 Letter
    23.     strNewCode = Left$(![Code2], 3) & Chr$(Asc(Mid$(![Code2], 4, 1)) + 1) & "A"
    24.   Else      'Increment Last Character by 1
    25.     strNewCode = Left$(![Code2], 4) & Chr$(Asc(Right$(![Code2], 1)) + 1)
    26.   End If
    27. End With
    28.  
    29. rst.Close
    30. Set rst = Nothing
    31.  
    32. fRetrieveNextAvailCode = strNewCode
    33. End Function
    34.  
  5. Sample Data (tblCodes):
    Expand|Select|Wrap|Line Numbers
    1. Code2
    2. 003AA
    3. 003AB
    4. 003AC
    5. 003AD
    6. 003AE
    7. 003AF
    8. 004AA
    9. 004AB
    10. 088KZ
  6. Function Calls along with results:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("003")
    2. 003AG
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("004")
    2. 004AC
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("088")
    2. 088LA
P.S. If you really wanted to be trick, you can now easily create a Query which will show the next Code in sequence for the Unique 3-Character Codes in your 2nd Table.

12 4715
dsatino
393 256MB
I feel like there's a way to do this with character codes, but I can't think of how.

A quick work around would be to simply create a reference table where each letter has a numeric value. Capture the last letter of the string, look up it's numeric value,add one, and then lookup the related letter.

Or something of that nature, but you get the general idea
Aug 10 '10 #2
ADezii
8,834 Expert 8TB
I literally threw this together before going out, but it should at least give you a starting point to reference. Simply pass to the Function a 3-Digit Base Code for which you want generate the next, sequential Code based on those 3-Digits. I listed some Sample Data along with Results, but first a few Assumptions.
  1. Your Table name is tblCodes and consists of a Field named [Code2](TEXT}. Any other Fields in this Table are irrelevant.
  2. The Code always follows the same exact Format (3-Digits, 2 Strings), and is sequential in nature as in: 045AA, 045AB, 045AC, 045AD, 076AA, 076AB, etc.
  3. You will never have a situation where 3 Digits would be followed by a Double 'Z', namely XXXZZ. If you would, the code will have to be modified. For now, it allows for a 'Z' on the back end, and makes the proper adjustments.
  4. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fRetrieveNextAvailCode(strCode As String) As String
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strNewCode As String
    5.  
    6. Set MyDB = CurrentDb
    7.  
    8. 'Retrieve only the Codes that begin with the 3 Characters in strCode,
    9. 'namely 001, 002, 003, 021, 099, 124, etc.
    10. Set rst = MyDB.OpenRecordset("SELECT * FROM tblCodes WHERE Left$([Code2], 3) = '" & _
    11.                               strCode & "'", dbOpenSnapshot)
    12.  
    13. With rst
    14.   If .BOF And .EOF Then     'No Records exist for 3-Digit Code
    15.     MsgBox "No 2-Digit Base Code beginning with [" & strCode & "]", vbExclamation, "No Records"
    16.       .Close: Set rst = Nothing: Exit Function
    17.   End If
    18.  
    19.   .MoveLast     'Retrieve Last Code used (003AD, 098GH, etc.)
    20.  
    21.   If Right$(![Code2], 1) = "Z" Then        'Special case, set Last Character to 'A', then
    22.                                         'Increment 1st Character by 1 Letter
    23.     strNewCode = Left$(![Code2], 3) & Chr$(Asc(Mid$(![Code2], 4, 1)) + 1) & "A"
    24.   Else      'Increment Last Character by 1
    25.     strNewCode = Left$(![Code2], 4) & Chr$(Asc(Right$(![Code2], 1)) + 1)
    26.   End If
    27. End With
    28.  
    29. rst.Close
    30. Set rst = Nothing
    31.  
    32. fRetrieveNextAvailCode = strNewCode
    33. End Function
    34.  
  5. Sample Data (tblCodes):
    Expand|Select|Wrap|Line Numbers
    1. Code2
    2. 003AA
    3. 003AB
    4. 003AC
    5. 003AD
    6. 003AE
    7. 003AF
    8. 004AA
    9. 004AB
    10. 088KZ
  6. Function Calls along with results:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("003")
    2. 003AG
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("004")
    2. 004AC
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fRetrieveNextAvailCode("088")
    2. 088LA
P.S. If you really wanted to be trick, you can now easily create a Query which will show the next Code in sequence for the Unique 3-Character Codes in your 2nd Table.
Aug 10 '10 #3
Hi ,

Thank you very much - that looks brilliant.

I think I understand what it is doing.

The only thing is that this code doesn't compile at the beginning when it sets "dim mydb as dao.database" because I don't think I have the DAO option of Database perhaps?

It is saying it does not recognise the user defined type "database".

How do I define the database?

Thank you very much for your help.
Aug 12 '10 #4
NeoPa
32,556 Expert Mod 16PB
It needn't be as complicated as to require a code solution.

Asc() & Chr() are functions that can be used to increment a character from one to the next.

DMax() is a Domain Aggregate function that can find the largest value so far in your table that matches the criteria, which should specify that the code field starts with the numeric string value you're interested in.
Aug 12 '10 #5
ADezii
8,834 Expert 8TB
Wouldn't it be a little sticky if the last Character was a 'Z' which would now require a wrap-around on the last Character to 'A', and an increment of the previous one? If the last 2 Characters were 'ZZ', then I see nothing but a code based solution.
Aug 12 '10 #6
ADezii
8,834 Expert 8TB
Should you decide to use this approach, you may need to Set a Reference to the Microsoft DAO X.X Object Library.
Aug 12 '10 #7
NeoPa
32,556 Expert Mod 16PB
It's a fair point ADezii, but my reading of the question, which could be wrong of course, is that the fourth character is not a progression from A upwards after overrunning the alphabet, but a code assigned to the first three numbers. Looking at the example data posted (and reading between the lines) that data is static and no allowance has been made (nor needs to probably) for any necessity to handle an overflow of the character Z.

Obviously much is conjecture due to the lack of detail in the question, but I see nothing that indicates to me that such handling would be required for this particular question.

Clearly the OP would have the final say on the whole issue of course :)
Aug 12 '10 #8
Hello,

Sorry - I did not put enough detail in the original question.

Yes, if the last character is a Z, then, yes I do require it to start with the next alphabet letter followed by an A - eg. after AZ, I would want it to find BA as the next code, which is what it does in the code you posted.

Also, when it gets to ZZ then it has run out of codes at which point I think I just want it to error to alert me that it has run out of codes.

So I think the code does what I want. I just need to get it to access the DAO library otherwise it won't work.

I'll have another go at doing that...

Is it just a statement that I have to put in?

or could it be possible that the DAO library is not installed in my version of Access? I'm not quite sure how it works....

Thank you for all your help...
Aug 16 '10 #9
NeoPa
32,556 Expert Mod 16PB
If you go into the VBA editor (IDE) then you can set the references required or the database from the Tools menu. You would be looking to select one of the form :
Microsoft DAO x.x Object Library

If you're using ADezii's code, don't forget to set his post as the Best Answer ;)
Aug 16 '10 #10
Thank you - I set the reference in Tools menu and that works.

The only thing is that I now have a further problem.

My table that contains the new records consisting of the 3 digit number code contains many of the same vehicle makes and they all have the same code - eg:-

003, AUDI, A4 TDI QUATTRO SPORT
003, AUDI, A4 CABRIOLET
282, VAUXHALL, AGILA S
282, VAUXHALL, AGILA SE
282, VAUXHALL, INSIGNIA EXCLUSIV 4X4

etc

When I run the code as it is, it assigns the same new code to all the Vauxhalls so they are all assigned
'282BE' for example when I wanted each one to have a different ascending alphabet code.

This is because I am calling it from a 'make table' query and I am not adding each new code each time to the original table that the code is reading so it doesn't know about each new code. Should I call it from a different type of query like an append query so it keeps adding to the same table? Would that work?

What would be the best way of doing it?

Thank you for your help...
Aug 17 '10 #11
NeoPa
32,556 Expert Mod 16PB
I suspect that's down to optimisation. If you sopecify a function call in your SQL the the SQL engine will try to determine if it needs to run the function every time a record is processed, or whether it can get away with calling it just the once up front, and remembering the returned value for use in subsequent calls.

Usually, if the parameter(s) passed to the query are record related then it will call it each time. If the parameter(s) are literal values the SQL engine will assume it is the same value required and simply remember it for other records.

If you'd like to paste in a copy of your MakeTable SQL we can check it for you and probably suggest an alternative way of calling the function so that it executes for each record.
Aug 17 '10 #12
Hello,

I'm pretty sure that it is executing the code for each record because the Make Table query has created a new table with every record assigned a new value consisting of the new code - eg. 282BE. They are just all allocated the same code if they are the same 3 digit number.

I put a debug break in the code and I can see it going in each time and then coming out and then going back in with the next record etc.

It's just that each time a new value is assigned, it assigns the new value and puts it in the new table and then it goes back into the code with the next record and looks at the same original table to obtain the next alphabet code, so it obtains exactly the same code again.

So when it executes the following code :-

Expand|Select|Wrap|Line Numbers
  1. Set rst = MyDB.OpenRecordset("SELECT * FROM tblCodes WHERE Left$([Code2], 3) = '" & _ 
  2.                               strCode & "'", dbOpenSnapshot)
the table "tblCodes" has not been updated with the new codes.

Here is my make table SQL where fRetrieveNextAvailCode is being called on the field CORNHILL CODE:-

Expand|Select|Wrap|Line Numbers
  1. SELECT New_Additions_from_Pricing.[ABI Code]
  2.      , fRetrieveNextAvailCode(New_Additions_from_Pricing![CORNHILL CODE]) AS Code
  3.      , New_Additions_from_Pricing.[BM Ind]
  4.      , New_Additions_from_Pricing.Make
  5.      , New_Additions_from_Pricing.cc
  6.      , New_Additions_from_Pricing.Model
  7.      , New_Additions_from_Pricing.Yrs
  8.      , New_Additions_from_Pricing.Doors
  9.      , New_Additions_from_Pricing.Fuel
  10.      , New_Additions_from_Pricing.Transmission
  11.      , New_Additions_from_Pricing.[Group Value 50]
  12.      , New_Additions_from_Pricing.[Motor Cover Grp]
  13.      , New_Additions_from_Pricing.[MotorCover SW]
  14.      , New_Additions_from_Pricing.[MotorCover SW 50]
  15.      , New_Additions_from_Pricing.[MotorCover Terms]
  16.      , New_Additions_from_Pricing.[MotorCover Excess]
  17.      , New_Additions_from_Pricing.[MotorCover Load]
  18.      , New_Additions_from_Pricing.EffDateFrom
  19.      , New_Additions_from_Pricing.EffDateTo
  20.      , New_Additions_from_Pricing.DisplayInd
  21.      , New_Additions_from_Pricing.CarplanGrp
  22.      , New_Additions_from_Pricing.CarPlanTerms
  23.      , New_Additions_from_Pricing.[CarPlan SW]
  24.      , New_Additions_from_Pricing.[CarPlan SW 50]
  25.      , New_Additions_from_Pricing.HorizonGrp
  26.      , New_Additions_from_Pricing.[Horizon Terms]
  27.      , New_Additions_from_Pricing.[Horizon Excess]
  28.      , New_Additions_from_Pricing.[IT Grp]
  29.      , New_Additions_from_Pricing.[IT Terms]
  30.      , New_Additions_from_Pricing.BD1Grp
  31.      , New_Additions_from_Pricing.[BD1 Trms]
  32.      , New_Additions_from_Pricing.[BD2 Grp]
  33.      , New_Additions_from_Pricing.[BD2 Trms]
  34.      , New_Additions_from_Pricing.[Car Multiplier]
  35.      , New_Additions_from_Pricing.[Clear Peril Group]
  36.      , New_Additions_from_Pricing.[Clear Combined Group]
  37.      , New_Additions_from_Pricing.[Clear COMP XS]
  38.      , New_Additions_from_Pricing.[Clear Terms] INTO New_Cornhill_Codes
  39. FROM New_Additions_from_Pricing;
Thank you for your help...
Aug 17 '10 #13

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

Similar topics

11
by: Hi5 | last post by:
Hi, I am new to access I usedto work in Oracle and Mysql. I am after a way that enables me to populate a database I designed in access with lots of data which can be sorted in excel sheets, ...
2
by: MikeY | last post by:
Hi everyone, Using C#, Windows forms. I am trying to learn how to modify existing data with in MSDE table/fields. If anyone could help me out with my code, I would appreciate it. My code is as...
11
by: Bonjour | last post by:
Hi everybody ! I' d like to check data before inserting. Select @nb = Count (idCustomer) From Customer Where company like '%' + @company+ '%' If "PEUGEOT SA" already exists, @nb > 0 if I...
13
by: nyt | last post by:
I have a problem of number and text field. I got the database file(mdb) that contains many combo boxes used and its list values are created by "value list" For eg field Field name= 'furniture'...
0
by: AboutJAV | last post by:
I created a crystal report with the report.rpt reportdata.xsd I created the a new dataset with the reportdata myreportdata = new reportdata(); That automatically created a new dataset...
20
by: technocraze | last post by:
Hi guys & commnunity experts, Does anyone knw how to go about checking for existing data in an MS Acess table? I have tried out the following code using vb but doesnt seem to work that well? Can...
2
by: Gary42103 | last post by:
Hi I need Perl Script to do Data Parsing using existing data files. I have my existing data files in the following directory: Directory Name: workfs/ams Data File Names: 20070504.dat,...
1
by: Ruby jain | last post by:
pleade help me in coding of next buuton .how the data move when press the next button in c#
2
by: kostasgio | last post by:
Hello, this is my first post here, i hope i'll find this forum usefull. Although i did a search about my question, i didnt find what i need , because the question isnt exactly what it sounds. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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,...

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.