Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15

    Alphabet autoincrement


    I am trying to assign a unique two letter code to a set of record. From AA..AB..BA....all the way to ...ZZ, how do i go about doing this ??? IN VBA ?? Thank you for the help in advance!

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Upper case Alphabets have ASCII codes from 65 to 90 [in order from A - Z].
    You could use those numbers to write VBA Code to increment the ASCII code.
    For example:
    Code:
    Dim intFirst_Letter as Integer
    Dim strCode As String, strFirst_Letter As String, strSecond_Letter As String
    
    'Get the value of the Code into strCode here . . . I tested it with this:
    strCode = Me.Text_Box_On_Form
    
    'This gives you the first letter of the code - and converts it to an ascii number.
    strFirst_Letter = Left(strCode, 1)
    intFirst_Letter = Asc(strFirst_Letter)
    
    'This will increment the ascii number and then convert it back to the 'incremented' letter.
    intFirst_Letter = intFirst_Letter + 1
    strFirst_Letter = Chr(intFirst_Letter)
    MsgBox "New First Letter = " & strFirst_Letter & "."
    You can use that as a starting point.

    Hope this helps!

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Why? Is this just an exercise or is there some purpose?
    You might want to Google "Cartesian Product" to assist with all 2 letter combinations.

  4. #4
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    Stupid question first, why!

    Reason being this is quite hard from where my head is sitting, its doable, but likely to be long an complex, here's why!

    Working off a similar basis to hexadecimal, you could work A=0 to Z=25.

    you would then need to count the number of existing records, and add one to get the new record number.

    Then you need to calculate the number to your first letter, this is done by fix([new_record_number]/25)

    then calculate the number for your second letter, this is done by mod([new_record_number]/25)

    your should then write a function (probably including a long select case) to translate the number to a letter.

    My head isn't quite with it enough yet to write the full vb required, but the concept (i think) is there, assuming i understood your correctly.

  5. #5
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    Robeen, you know what, I never even thought of touching ASCII, I havent looked at it since I was using a BBC model B!
    Last edited by R_Badger; 05-23-2012 at 07:35 AM. Reason: Using ASCII might be a bit easier than what I was suggesting about a long select case function!

  6. #6
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    Im doing this for a project and it’s a requirement, and im sorry you find this a stupid question, I understand the work is lengthy but if it wasn’t doable I don’t think I would have been asked to create it, just needed some guidance thank you for the help.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Funny how we see this differently.
    I'd write a small sub to populate a table, with A thru Z 26 records
    I would make a cartesian product using 2 copies of the table - to get all 2 char unique combinations.
    Stuff these in a table with an id

    Assign values to the new records based on the Id

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    KWHAT,

    it’s a requirement
    What exactly will this 2alpha char unique field be used for? Why couldn't it be a unique number? I don't think anyone said it was a stupid question , but it is the kind of question that requires a little explanation. In my view it is a HOW you might do something, but I'd be asking WHAT is it you're trying to accomplish --there may be options.

  9. #9
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    Well there are limitations, I need to set the first two characters as a state and the last two need to be characters as well, so there a multiple groups that need to be assigned a unique ID within the same state therefore I need the last two letters to change incrementally, there can’t be any numerical values in the string .

  10. #10
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    @Orange thats a great idea with the two tables. Thanks!

  11. #11
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    But how do i go about after creating the two tables to get a cartesian product to of all 2 char unique combinations.

  12. #12
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    KWHAT I wasn't saying your question was stupid, its mine. Why doesnt help what, but as orange said understanding of concept sometimes reveals other options

  13. #13
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    Quote Originally Posted by R_Badger View Post
    KWHAT I wasn't saying your question was stupid, its mine. Why doesnt help what, but as orange said understanding of concept sometimes reveals other options
    I totally understand i should have added more detail and explaination to what i was asking.

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I still don't accept that this is really a requirement that wouldn't/couldn't/shouldn't be solved in a different way, BUT

    Here are a few steps for the purpose of the exercise.

    Create a table, in my case TblAlphaChar, with 1 field SingleAlph text 1

    Populate the table with a small procedure
    Code:
    Sub PopulateTbl()
    Dim x As Integer
    Dim s1 As Integer
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("tblAlphaChar")
    s1 = 65 'capital A
    For x = 0 To 25
    rs.AddNew
    rs!singlealph = Chr(s1 + x)
    rs.Update
    Next x
     rs.Close
    End Sub

    Run the following query (using 2 copies of tblAlphaChar a and b) to make a new table TblAlphaPairs with the unique combos if characters

    SELECT A.singlealph & b.singlealph AS MyPair INTO tblAlphaPairs
    FROM TblAlphaChar AS a, TblAlphaChar AS b;

    Open tblAlphaPairs in design mode, add an Id field autonumber
    save the table.
    See then attached jpg for the values in tblAlphaPairs (and other pieces)
    Attached Thumbnails Attached Thumbnails AlphaPair.jpg  

  15. #15
    KWHAT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Dec 2011
    Posts
    15
    Thanks, for the steps they really helped, but for my own curiosity how would you go about creating a unique string with four characters, were the two preceding characters must be the state or province.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-22-2012, 10:46 AM
  2. Replies: 2
    Last Post: 02-25-2012, 10:17 AM
  3. Replies: 11
    Last Post: 02-13-2012, 10:06 AM
  4. Cleaning up the alphabet
    By ducecoop in forum Access
    Replies: 4
    Last Post: 10-28-2010, 08:33 AM
  5. Alphabet break in report
    By amccook in forum Reports
    Replies: 8
    Last Post: 08-27-2010, 03:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums