|
-
Sep 3rd, 2004, 02:02 PM
#1
Thread Starter
New Member
Access 97- Trying to update fields using Recordset methods
I'm using Access 97 and have a table (named "tblCompiledSorted") where records are set up like this:
Code:
Record No | Identifier
-------------------------------
1 12
2 14
3 14
4 17
5 19
6 19
I'm trying to write a module to change all my "Record No" change to "010" unless the identifier directly below the current row is identical to the identifier directly above it, in which case I want it named "012". Basically, I want my table to look like this when it's done:
Code:
Record No | Identifier
-------------------------------
010 12
012 14
010 14
010 17
012 19
010 19
Here's the code I wrote to try and do this:
Code:
Public Function UpdateRecordNo()
Dim db As Database
Dim strsql As String
Dim rstTop As Recordset, rstBottom As Recordset, rstRecNum As Recordset
Dim strTop As String, strBottom As String, strRecNum As String
Set db = CurrentDb
'Establish rstTop
strsql = "SELECT Identifier FROM tblcompiledsorted"
Set rstTop = db.OpenRecordset(strsql)
strTop = rstTop.Fields("Identifier").Value
'Establish restBottom
strsql = "SELECT Identifier FROM tblcompiledsorted"
Set rstBottom = db.OpenRecordset(strsql)
rstBottom.MoveNext
strBottom = rstBottom.Fields("Identifier").Value
'Establish rstRecNum
strsql = "SELECT * FROM tblcompiledsorted"
Set rstRecNum = db.OpenRecordset(strsql)
strRecNum = rstRecNum.Fields("Identifier").Value
'label Record Numbers
Do While rstRecNum.EOF = False
With rstRecNum
If strTop = strBottom Then
.Edit
![Record No] = "012"
.Update
Else
.Edit
![Record No] = "010"
.Update
End If
End With
rstTop.MoveNext
rstBottom.MoveNext
rstRecNum.MoveNext
Loop
rstTop.Close
rstBottom.Close
rstRecNum.Close
End Function
The problem is, when I run this I get a "No current record" message for the rstBottom.MoveNext command that in the looping section. Since it won't move to the next record, it pretty much screw my entire bit of code. I'm assuming it's because of the rstBottom.MoveNext I have in the 'Establish rstBottom section, but I haven't quite figured that out yet.
Does anyone know why this is happening or have any ideas on how I can fix it? I'd like to salvage this bit of code if possible as starting over would be rather vexing. Thanks in advance!
-
Sep 3rd, 2004, 05:08 PM
#2
This will take care of all your 010 values.
Code:
UPDATE tblCompiledSorted
SET [Record No] = '010'
WHERE Count(Identifier) = 1
Still working on the 012's
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 6th, 2004, 05:54 AM
#3
Thats cause you've done it wrong 
It was a good attempt, but as you have said it doesn't work.
You need three variables to hold very previous, previous and current ID values. A variable for the recordset (only one). Variable for Max records, and one for the current looping record.
Oh and a new column for the OrderID (the one you are changing to 010 or 012)
Set very previous and previous to -1
Loop through all records in the recordset using a for next loop
On pass one
- set the OrderID to 010
- set previous to the value
On pass two
- compare value to previous value
--- set to 012 it matches set to 010 if not
- set very prev to previous
- set previous to current
On the rest of the passes
- compare very prev to prev to current
--- set orderid appropriately
Or something like that.. I've just noticed that the table you resulted in is wrong.. unless you have that
[code]
Record No | Identifier
-------------------------------
010 12
012 14
010 14
010 17
012 19
010 19
[code]
The one in bold or the one above should be the other id value?
Vince
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Sep 7th, 2004, 08:53 AM
#4
Thread Starter
New Member
Originally posted by Ecniv
Or something like that.. I've just noticed that the table you resulted in is wrong.. unless you have that
The one in bold or the one above should be the other id value?
Vince
The one in bold should be 10 because its identifier is 17 and the identifier below it is 19. If the identifier below the 17 was 19, then I'd need the top 17's record number to be 012 and the bottom 17's record number to be 010.
I'm working within a couple of rules I should have specified earlier:
1) There will never be more than 2 identical identifiers.
2) If there is a duplicate identifier, it will be directly below its matching identifier (this if from sorting I've done previously on the table).
I'm still working on this and I appreciate the help. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|