![]() |
|
|
| Subject:
MS Access Guru needed: Finding the "next" and "previous" record
Category: Computers > Programming Asked by: respree-ga List Price: $5.00 |
Posted:
16 May 2003 09:36 PDT
Expires: 15 Jun 2003 09:36 PDT Question ID: 204654 |
Greetings. I need help writing a Query or Visual Basic code in MS Access. I'm not sure which is the solution. I'm trying to create a table that will find the next and previous sequential records in a table. Additionally, this query (or VBA) would append text to the text value of the 'next' and previous' record, in this case "p-." [SKU] is given, [PREVIOUS] and [NEXT] are calculated based on [SKU]. Note fieldnames have been bracketed. When properly calculated, the newly created table would look something like this: [SKU] [PREVIOUS] [NEXT] B0587159901 p-B0587159979 p-B058715991X B058715991X p-B0587159901 p-B0587159928 B0587159928 p-B058715991X p-B0587159936 B0587159936 p-B0587159928 p-B0587159944 B0587159944 p-B0587159936 p-B0587159952 B0587159952 p-B0587159944 p-B0587159960 B0587159960 p-B0587159952 p-B0587159979 B0587159979 p-B0587159960 p-B0587159901 Note: For the first record [SKU], the [PREVIOUS] field would show the last record. For the last record [SKU], the [NEXT] field would show the first record. Also, the [SKU] numbers are NOT in any ascending or decending order. In the live application, they are in a random order (and must stay that way), so the solution must involving getting the next and previous record, rather than a <> solution. I've posted this question on the MrExcel.com bulletin board, but could not get a decent solution to my problem. Read more here. http://www.mrexcel.com/board2/viewtopic.php?t=49897&highlight= If there is any additional information needed to clarify this question, please ask. Please let me know if writing the VBA involves more than a few lines of code and I will tip accordingly. Any help would be greatly appreciated. | |
| |
|
|
| Subject:
Re: MS Access Guru needed: Finding the "next" and "previous" record
Answered By: hammer-ga on 16 May 2003 13:05 PDT Rated: ![]() |
Hi Respree,
Here is a VBA routine for you. Paste it into a new Access module. The
routine assumes that you have two tables already existing:
Table: tblSKU
Field Name Type
-------------------------------
SKU Text Primary Key
Table: tblLinkedList
Field Name Type
------------------------------
SKU Text Primary Key
PreviousSKU Text
NextSKU Text
NOTE: No line of code should wrap. Code may need to be unwrapped after
pasting into a VBA module.
' ****** Code Begin
Public Sub BuildLinkedList()
On Error GoTo ErrHandler
Dim cnn As ADODB.Connection
Dim rstSKU As ADODB.Recordset
Dim rstSKUClone As ADODB.Recordset
Dim strSQL As String
Dim strSKU As String
Dim strPrev As String
Dim strNext As String
Dim strFirstSKU As String
Dim strLastSKU As String
Set cnn = CurrentProject.Connection
Set rstSKU = New ADODB.Recordset
' Clear existing records from table
cnn.Execute ("DELETE * FROM tblLinkedList;")
' Select all SKUs in the order they currently
' appear in the SKU table. No sort is applied.
strSQL = "SELECT SKU FROM tblSKU;"
rstSKU.Open strSQL, cnn, adOpenStatic, adLockOptimistic
strSQL = ""
If Not rstSKU.BOF And Not rstSKU.EOF Then
rstSKU.MoveFirst
' Set first and last value for
' looparound
strFirstSKU = "p-" & rstSKU!SKU
rstSKU.MoveLast
strLastSKU = "p-" & rstSKU!SKU
rstSKU.MoveFirst
' Clone the recordset so we can
' keep two bookmarks
Set rstSKUClone = rstSKU.Clone
rstSKUClone.MoveNext
' Now rstSKU is on the first record and
' rstSKUClone is on the 'next' record.
strPrev = strLastSKU
Do Until rstSKU.EOF
strSKU = rstSKU!SKU
If Not rstSKUClone.EOF Then
strNext = "p-" & rstSKUClone!SKU
rstSKUClone.MoveNext
Else
strNext = strFirstSKU
End If
strSQL = "INSERT INTO tblLinkedList (SKU, PreviousSKU,
NextSKU) VALUES ('" & strSKU & "', '" & strPrev & "', '" & strNext &
"');"
cnn.Execute (strSQL)
strPrev = "p-" & rstSKU!SKU
rstSKU.MoveNext
Loop
End If
ExitMe:
' Clean up memory
strSQL = ""
strSKU = ""
strPrev = ""
strNext = ""
strFirstSKU = ""
strLastSKU = ""
If rstSKUClone.State = adStateOpen Then
rstSKUClone.Close
End If
Set rstSKUClone = Nothing
If rstSKU.State = adStateOpen Then
rstSKU.Close
End If
Set rstSKU = Nothing
Set cnn = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitMe
End Sub
' ****** Code End
Please let me know if you need clarification on this. Good luck with
your project!
- Hammer | |
| |
| |
respree-ga
rated this answer:
and gave an additional tip of:
$25.00
Excellent service, clear communication, and very pleased with the answer and service received. Highly recommended. Thanks so much for your help. |
|
| Subject:
Re: MS Access Guru needed: Finding the "next" and "previous" record
From: hammer-ga on 17 May 2003 05:41 PDT |
Respree, Thank you for the kind words and the generous tip. It's nice to be appreciated! - Hammer |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |