473,486 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Updating value of indexed column

122 New Member
I have a table with a composite index on three fields: RDAY, RSM, and RSEQ (each sorted ascending.)

I am trying to renumber the RSEQ field in increments of 5, while preserving the order the records are currently sorted in. Here is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. Public Function Resequence(TableName)
  2.   Dim rs As Recordset
  3.   Set rs = CurrentDb.TableDefs(TableName).OpenRecordset
  4.   rs.Index = "SortOrder"
  5.   rs.MoveFirst
  6.   Do While Not rs.EOF
  7.     If dDay <> rs.Fields("RDAY") Or dSM <> rs.Fields("RSM") Then dSeq = 0
  8.     dSeq = dSeq + 5
  9.     dDay = rs.Fields("RDAY")
  10.     dSM = rs.Fields("RSM")
  11.     rs.Edit
  12.     rs.Fields("RSEQ") = dSeq
  13.     rs.Update
  14.     rs.MoveNext
  15.   Loop
  16. End Function
Unfortunately, this code causes an infinite loop. I think because RSEQ is an indexed field, and we're changing its value, the order of the records changes and we never get to EOF. Currently it only changes values for the first value of RSM.

If I remove RSEQ from the index, the loop completes, but the order is not completely preserved.

How can I move through the records in their original order, and change the RSEQ field without reordering the records?
Aug 13 '10 #1
5 1724
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. Instead of trying to do this in code for such a one-off task I'd generate a temporary table listing the values of all three fields, with a fourth field for the new value of RSEQ which you wish to set, and update the RSEQ field to the new value by joining these tables accordingly.

It should be straightforward to use a query to extract your current composite key values into Excel, say, then create the new key value as a fourth column and paste the four columns back as a new table into Access.

For simplicity I have just given simple table and field names in the example below, with F1 to F3 for your existing fields and F4 for the replacement value of F3:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblTestUpdate INNER JOIN tblUpdateNewKey ON 
  2.  
  3.       (tblTestUpdate.F3 = tblUpdateNewKey.F3) AND 
  4.       (tblTestUpdate.F2 = tblUpdateNewKey.F2) AND 
  5.       (tblTestUpdate.F1 = tblUpdateNewKey.F1) 
  6.  
  7. SET    tblTestUpdate.F3 = [f4];
  8.  
You would have to be careful that in renumbering your existing field you do not create duplicates of some existing values which would violate the integrity of the composite key and therefore not be updateable. For example, you could not renumber a composite value of 1, 1, 1 to 1, 1, 5 if the value 1, 1, 5 already exists in your table.

There are two advantages to this approach. Firstly, in joining to the existing composite key value there are no issues with changing what is already indexed (as long as there are no key violations), and secondly you have a physical mapping table available showing what changes were made which you can use to undo those changes if need be.

-Stewart
Aug 14 '10 #2
ADezii
8,834 Recognized Expert Expert
I started to re-write the code to see if it was a Syntax issue, when I realized that you have no closing End If to the If Statement in Line #7. This could definitely be causing the Infinite Loop problem. In any event, I'll post the alternate code which has the single advantage of always being sure that the Records will be in proper order prior to processing. Don't forget to add the End If (LOL).
Expand|Select|Wrap|Line Numbers
  1. Public Function Resequence(TableName)
  2. Dim MyDB As DAO.Database
  3. Dim rs As Recordset
  4. Dim strSQL As String
  5.  
  6. 'Let's be absolutely sure of the Sort Order
  7. strSQL = "SELECT * FROM " & TableName & " ORDER BY [RDAY], [RSM], [RSEQ];"
  8.  
  9. Set MyDB = CurrentDb
  10. Set rs = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  11.  
  12. rs.MoveFirst
  13.  
  14. With rs
  15.   Do While Not .EOF
  16.     If dDay <> rs.Fields("RDAY") Or dSM <> rs.Fields("RSM") Then dSeq = 0
  17.       dSeq = dSeq + 5
  18.       dDay = rs.Fields("RDAY")
  19.       dSM = rs.Fields("RSM")
  20.         rs.Edit
  21.           rs.Fields("RSEQ") = dSeq
  22.         rs.Update
  23.     End If
  24.       rs.MoveNext
  25.   Loop
  26. End With
  27.  
  28. rs.Close
  29. Set rs = Nothing
  30. End Function
P.S. - As well as Code Tags, Indenting is always critical in posting code. Had the code been properly Indented, the missing End If would have been quite obvious. Just food for thought.
Aug 14 '10 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi ADezii. The If in the OP's first post is an example of a single-line IF statement which does not need an End If. It is an allowed form of syntax, as the MS-Help entry shows (asterisked line below):

Syntax
If condition Then [statements] [Else elsestatements] **

Or, you can use the block form syntax:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If
-Stewart
Aug 14 '10 #4
ADezii
8,834 Recognized Expert Expert
Thanks Stewart, that one went right over my head! (LOL).
Aug 14 '10 #5
gershwyn
122 New Member
Stewart: Thanks for answering, I do like your approach. Unfortunately, this is not a one off process, but something we will have to do to three or four tables periodically. (I have no control over the source data, as it comes from another company's software.)

ADezii: I don't know why running off a Dynaset recordset hadn't occured to me - I used to do them for everything until I realized you don't really need to for working directly on a table. Changing the recordset type got around the index problem and the results are perfect.

PS. Sorry about the IF thing. I hate using the block form when the actual code being carried out is so tiny, but I think we've demonstrated that is does add to the code's readability by splitting it out.
Aug 16 '10 #6

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

Similar topics

1
10999
by: BStorm | last post by:
Does anyone know the best way to update a DataTable row column using specific values for a multipart primary key? For example, updating an OrderAmt column in an OrderDetail table where the...
2
1155
by: Amir Zicherman | last post by:
hi, i have a btree index on col1 in table1. The column has either values 1,2,3, or 4. 4 does not appear that much in the table (only 5 times). there are about 20 million rows in the table. ...
0
944
by: astro | last post by:
I have a non-updatable column in a SQL:-Server view that represents a 1-to {0,1} relation with a child table. I want the end-user to be able to change this value. This view is generated with...
26
30881
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent...
0
4698
by: =?Utf-8?B?Y2NfY3Jhc2g=?= | last post by:
Hello all, This is probably a simple issue but since I am a newbie I am really stuck on it. I have been developing some code and I was using rowPosition to pull data from a Access Data base and...
5
5298
by: sourabhmca | last post by:
Hi friends, I have a table : create table abc (a int,b smalldatetime default getdate()) then insert value like insert into abc(a) values(1) now I want to change default value of column b be...
0
1719
by: sk27ahmed | last post by:
Hi Any one can show me how to access datagridview column value on column checked unchecked. I create one column in datagridview of type checkbox,and on button click i write code to select all...
1
3742
by: billa856 | last post by:
Hi, I am trying to insert Null value in column(ShipDate) in my table.That column(ShipDate)'s type id date/time and format is short date. I am using "" to insert Null in that column(ShipDate)...
0
1052
by: Ben | last post by:
Hi, With this code, when i click on Update button, the value updated of field "wa" in the table is NULL.The update of the other field (not in a DD) works good. It seems that the selectedvalue...
0
1571
by: Mark A | last post by:
Suppose a table like this: CREATE TABLE TABLE_A (COL_1 INT NOT NULL PRIMARY KEY, COL_2 CHAR(1) NOT NULL); and a SQL statement like this: UPDATE TABLE_A SET COL_1 = ? AND COL_2 =? WHERE...
0
7094
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
7123
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
7173
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...
1
6839
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...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.