Results 1 to 4 of 4
  1. #1
    carmenv323 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Location
    Massachusetts
    Posts
    78

    Sequence records with "a", "b", "c" etc

    Hello,


    I have the following code which searches for the last sequence number in the table in order to create folders. If there is more than one project number I would like for those records to output with the sequence number and an "a" or "b" etc. The following works ok but starts the first one with a "b" rather than "a". What is the best way to alter this to do that? Thank you in advance.


    Code:
     If IsNull(frm.SequenceNum) Or (frm.SequenceNum = 0) Then
            Dim intLastSeqNum As Integer
                intLastSeqNum = Nz(DMax("SequenceNum", "tbl_EstimateLog", "intYear = " & intMyYear), 0)  'searching for last seq number for current year
            
            Dim tReq As String
            Dim db As DAO.Database
            Dim Records As DAO.Recordset
            
            Dim strSQL As String
                Set db = CurrentDb
            tReq = "select tbl_Requests.txtprojectnumber from tbl_Requests " & _
                                            " where tbl_Requests.[txtProjectNumber] = '" & frm.txtProjectNumber.Value & "'"
            
                Set Records = db.OpenRecordset(tReq)
                    If Records.RecordCount > 0 Then
                        Dim strLetter As String
                        Dim i As Integer
                            strLetter = "a"
                            For i = 1 To 26       'I PREVIOUSLY HAD 0 To 26 but returned the same result of "b"
                               strLetter = Chr(Asc(strLetter) + 1)
                        Next i
                        frm.SequenceNum.Value = intLastSeqNum & strLetter
                Else
                       frm.SequenceNum.Value = intLastSeqNum + 1
                End If
            End If

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    That is because you add 1 to a starting value of asc of a?
    So start on whatever character is before a in the ascii table

    Not sure how you are going to progress to b ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,421
    Key point is red line.

    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Sub adv()
        Dim strLetter As String
        Dim i As Integer
            strLetter = Chr(96)
            For i = 1 To 26      'I PREVIOUSLY HAD 0 To 26 but returned the same result of "b"
               strLetter = Chr(Asc(strLetter) + 1)
               Debug.Print strLetter;
        Next i
    End Sub
    Click image for larger version. 

Name:	immed.png 
Views:	17 
Size:	4.0 KB 
ID:	48595

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Could you ever do this with the folder structure ever being partially completed? If your code failed part way through, perhaps that would happen and next time you try to run it I would expect errors at least and maybe folder/files getting messed up at worst. I think I would loop from the Asc value +1 of the last value returned by DMax up to 122 if I was OK with being limited to 26 possibilities.

    EDIT - I think I've misunderstood - the sequence number has nothing to do with the appended letter.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 28
    Last Post: 03-23-2021, 11:02 AM
  2. Replies: 3
    Last Post: 04-06-2020, 12:15 PM
  3. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  4. Replies: 2
    Last Post: 01-23-2017, 07:06 PM
  5. Replies: 1
    Last Post: 09-07-2015, 08:00 AM

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