Link to home
Start Free TrialLog in
Avatar of billcute
billcute

asked on

Re-ordering Autonumber Question

The Sub below in Code Snippet is expected to re-order the two groups of serial numbers in tblMain using the "AfterUpdate Event" of some frmMain's control.

I am quite sure that the Sub needs a minor correction in order to make it work as expected.

tblMain without the FDID modifications look like the one below:
tblMain
SID      TestType            FDID                                     BNo     LNo    CTypeID    STypeID
1          GN              XW-GN-TA-6612-23-CM-01       6612      23           TA             CM
2          GN              XW-GN-TA-6612-23-CM-02       6612      23           TA             CM
3          GN              XW-GN-TA-6612-23-CM-03       6612      23           TA             CM
4          PK               XW-PK-TA-1129-51-DM-01       1129       51           TA             DM
5          PK               XW-PK-TA-1129-51-DM-02       1129       51           TA             DM

User later modify a record in tblMain using frmMain form controls (AfterUpdate Event) and the result below is expected to be obtained.

For example, SID 2 before changes were made, field FDID was:

tblMain
SID      TestType          FDID                                        BNo      LNo    CTypeID     STypeID
2            GN             XW-GN-TA-6612-23-CM-02        6612     23         TA            CM

but after changes were made to SID 2, then field FDID now changed to:

tblMain
SID        TestType              FDID                                   BNo       LNo     CTypeID      STypeID
2               GN           XW-PK-TA-1129-51-DM-03        6612       23            TA             DM

Now after re-order, both FDID groups ............
XW-GN-TA-6612-23-CM- ..and.. XW-PK-TA-1129-51-DM-

......were both expected to re-order to:
tblMain
SID   TestType    FDID                                           BNo     LNo    CTypeID    STypeID
1         GN           XW-GN-TA-6612-23-CM-01      6612     23         TA           CM
2         PK            XW-PK-TA-1129-51-DM-01      1129      51        TA            DM '<== After reorder
3         GN           XW-GN-TA-6612-23-CM-02      6612     23        TA            CM '<== After reorder
4         PK            XW-PK-TA-1129-51-DM-02      1129      51        TA            DM '<== After reorder
5         PK            XW-PK-TA-1129-51-DM-03      1129      51        TA            DM '<== After reorder

Problem:
When the sub is called from the "AfterUpdate event" code of (cboTest, cboCType, cboBNo, txtLNo and cboSType) .......as shown below
        'Call renumbering function
        RenumberFDID sOldPrefix, sNewPrefix

...then group "XW-GN-TA-6612-23-CM-" was re-ordered but....
group "XW-PK-TA-1129-51-DM-" failed to re-order such that tbMain looked like this....
tblMain
SID   TestType    FDID                                           BNo     LNo    CTypeID    STypeID
1         GN           XW-GN-TA-6612-23-CM-01      6612     23         TA           CM
2         PK            XW-PK-TA-1129-51-DM-01      1129     51        TA            DM '<== This is Ok
3         GN           XW-GN-TA-6612-23-CM-03      6612     23        TA            CM '<== This is not Ok
4         PK            XW-PK-TA-1129-51-DM-02      1129      51        TA            DM '<== This is Ok
5         PK            XW-PK-TA-1129-51-DM-03      1129      51        TA            DM '<== This is Ok

Note that SID 3 (FDID) did not re-order as expected.....it remained as:
3         GN           XW-GN-TA-6612-23-CM-03      6612     23         TA           CM
.....instead of..

3         GN          XW-GN-TA-6612-23-CM-02        6612     23         TA           CM  '<== This is expected

I have attached a sample db for testing purposes at this link:
http://www.geocities.com/bombastikbill/Renumbering_db.zip

To Test:
 When frmMain opens ..
      navigate to SID 2 and change the following data in frmMain field controls as follows:
     Current value:                                            Changed Value
    FrmMain.cboTest         = "GN"                           "PK"
    frmMain.cboCType      = "TA"                             "TA"            '<=== Value here did not change
    frmMain.cboBNo          = ''6612"                         "1129"
    frmMain.cboLNo          =  "23"                             "51"
    frmMain.cboSType      = "CM"                             "DM"

The Sub with the problem is placed in Code Snippet.
Public Sub RenumberFDID(ByVal sOldPrefix As String, ByVal sNewPrefix As String)
 
    Dim rs As DAO.Recordset
    Dim iOldPrefix As Integer       'Count for renumbering original prefix
    Dim iNewPrefix As Integer     'Count for renumbering new prefix
    Dim sSql As String
    Dim sFormat As String
       
    DoCmd.Hourglass True
    
    'Initialise
    Set rs = Me.RecordsetClone
    iNewPrefix = 0
    iOldPrefix = 0
    sFormat = "00"
 
'CHANGE HERE - ADD sNewPrefix TO FILTER ONLY IF SPECIFIED 
    'Filter on records that begin with the old
    sSql = "SELECT * FROM tblMain WHERE FDID LIKE '" & sOldPrefix & "*'"
    
    'If new prefix specified (specified when changing record) then include that as well
    If sNewPrefix <> "" Then sSql = sSql & " OR FDID LIKE '" & sNewPrefix & "*'"
    
    Debug.Print "Initiating Search", sSql
    
    rs.Filter = sSql
    Set rs = CurrentDb.OpenRecordset(sSql)
    
    'Now loop through updating existing records
    Do While rs.EOF = False
 
        Debug.Print "Found ID=" & rs!SID, "FDID=" & rs!FDID
        
        rs.Edit
        
        If Left$(rs!FDID, Len(sOldPrefix)) = sOldPrefix Then
            iOldPrefix = iOldPrefix + 1
            Debug.Print "Updating Record to " & sOldPrefix & Format(iOldPrefix, sFormat)
            rs!FDID = sOldPrefix & Format(iOldPrefix, sFormat)
 
'CHANGE HERE - CHECK sNewPrefix HAS A VALUE 
        ElseIf sNewPrefix <> "" Then
            iNewPrefix = iNewPrefix + 1
            Debug.Print "Updating Record to " & sNewPrefix & Format(iNewPrefix, sFormat)
            rs!FDID = sNewPrefix & Format(iNewPrefix, sFormat)
        End If
 
        rs.Update
        
        rs.MoveNext
    Loop
 
    'Closedown
    rs.Close
    Set rs = Nothing
    
    Me.Refresh
 
    DoCmd.Hourglass False
End Sub

Open in new window

Avatar of Hamed Nasr
Hamed Nasr
Flag of Oman image

I suggest you set a beak point at the start of your code.
Step through code checking the process.
Avatar of billcute
billcute

ASKER

hnasr:
Sorry for being so late...I was out of town...thanks for assisting..

I did step through as suggested. There were no errors....and couldnt figure out how to amend the code.

The only problem I am having with the code is that only one group are being updated the other did not.

For example if you download and open my sample db.
If user navigates to the SID 2 using frmMain and make changes to the SID 2 record as shown below:

     Current value:                                            Changed Value
    FrmMain.cboTest         = "GN"                           "PK"
    frmMain.cboCType      = "TA"                             "TA"            '<=== Value here did not change
    frmMain.cboBNo          = ''6612"                         "1129"
    frmMain.cboLNo          =  "23"                             "51"
    frmMain.cboSType      = "CM"                             "DM"

...then.....note the changes before the changes and after the changes were made.
tblMain
SID      FDID                                        
2       XW-GN-TA-6612-23-CM-02      '<==== Before changes were made to SID 2

but after changes were made to SID 2 (see below), then field FDID now changed to:

tblMain
SID      FDID                                    
2       XW-PK-TA-1129-51-DM-01   '<==After changes were made and re-alligns to the new group

...such that the new group in tblMain looks like the one below:
2         PK            XW-PK-TA-1129-51-DM-01      '<== Newly re-alligned to the "PK" group.
4         PK            XW-PK-TA-1129-51-DM-02      '<== After re-order SID 4 was also re-ordered
5         PK            XW-PK-TA-1129-51-DM-03     '<== After re-order SID 5was also re-ordered

However.....the other group "GN" group did not not re-order as expected (see tblMain below) since the SID 2 that belongs to that group has been changed to the PK group.

SID    FDID                                        
1    XW-GN-TA-6612-23-CM-01      
2                                                      <==== SID 2 was renumbered and moved to the PK group
3    XW-GN-TA-6612-23-CM-03    '<== The last 2 digits should be re-ordered such that it looks like:
                                                                XW-GN-TA-6612-23-CM-02

and tblMain will now look like this:

SID    FDID                                        
1    XW-GN-TA-6612-23-CM-01      
2                                                      <==== SID 2 was renumbered and moved to the PK group
3    XW-GN-TA-6612-23-CM-02    '<=== Note the last 2 digit changes after renumbering.
ASKER CERTIFIED SOLUTION
Avatar of Si Ball
Si Ball
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry billcute!
Couldn't follow the logic.
I wonder why you need to change values in a table. Can't you achieve the effect through a query?

hnasr:
You said:
I wonder why you need to change values in a table

I am only using what was availaible to me at the time the initial question was raised.

Next Question:
Can't you achieve the effect through a query?

If you think we can achieve the effect through a query, I am willing to explore it as well. Just let me know and I'll list series of questions on the topic for Adding, editing and Deleting serial numbers in a form based controls using queries.

Regards
Bill
Sudonim:
Thanks for your suggested amendment. It works great.

Regards
Bill
i think you should have asked an adminer to cancel one of the two questions.  
billcute:
Glad you found a solution to current problem.
"...using queries"
Tables are mainly for insert, update, delete. Rearranging records and fields is the job for queries and reports.
If you plan to experiment with the queries, then create a small database with simple tables and few simple fields.
hnasr:
I will prepare a small database with simple tables and a few form fields as requested. I will post the question on Monday or Tuesday next week and I will place a link here to the new question.

Regards
Bill
Sudonim:
Not to worry.

Regards
Bill
hnasr / Sudonim
I have created a new question to explore the query based suggestion at the link below:
https://www.experts-exchange.com/questions/24491073/Updating-records-with-form-based-query.html


...hoping for further assistance on the subject.

Regards
Bill