Reputation: 481
I have an Access database file which I'm using to process a Bill of Materials output from a CAD package. I'm not exactly using it as a relational DB, more as an intermediate processing step for some data, in order to output some barcodes which will identify parts in our workshop, which will then link back to some CNC machining code files which I'll be generating based on the PARAMETERS
for each part.
Essentially, the stage I'm at is that I have a table which houses data like the following:
REF NB JOB_ID PART_ID DESIGNATION PARAMETERS
A 4 00001 RAFTER L=500 W=56
B 6 00001 RAFTER L=700 W=56 P=25
B 2 00001 DOOR JAMB FH=2090 W=66 OD=0
A 2 00123 SASH STILE SH=500 SSW=56 HANG=3
A 2 00123 SASH RAIL SH=500 SRD=66 HANG=3
There can be hundreds of parts for several different JOB_ID
values, with a selection of different parameters.
I've then saved a query in access which takes all of the above fields and sorts by: JOB_ID
, then by DESIGNATION
, then by REF
. What I then need to do is go through the query and assign each record a PART_ID
such that the combination of JOB_ID
and PART_ID
are unique. So for the above example I might end up with (after sorting, and assigning a PART_ID
):
REF NB JOB_ID PART_ID DESIGNATION PARAMETERS
B 2 00001 00001 DOOR JAMB FH=2090 W=66 OD=0
A 4 00001 00002 RAFTER L=500 W=56
B 6 00001 00003 RAFTER L=700 W=56 P=25
A 2 00123 00001 SASH RAIL SH=500 SRD=66 HANG=3
A 2 00123 00002 SASH STILE SH=500 SSW=56 HANG=3
I need help with the best way to go about this. Presumably I need to open a recordset with the query, loop through the records and assign an incrementing PART_ID.
Specifically, how do I:
Open a recordset with a query? I know how to do this with a table. Loop through that recordset in the most efficient way? Save the updates to the underlying tables?
I'm also open to suggestions for better ways to do this.
Many thanks.
Upvotes: 0
Views: 596
Reputation: 481
@tbur Thanks very much, your answer works perfectly.
I have however made the following change:
Function RunningCount(WhatToCount As String, NumDigits As Integer) As String
Static CountSoFar As Long, var As String
If var <> WhatToCount Then
CountSoFar = 0
var = WhatToCount
End If
CountSoFar = CountSoFar + 1
RunningCount = Left("00000000000000000000", NumDigits - Len(CStr(CountSoFar))) & CountSoFar
End Function
Which gives me the output including leading zeros as I need them (I want to keep each ID to 5 digits to help with later barcode processing, but I've added a variable to select how many digits the output should be).
Upvotes: 0
Reputation: 2454
One thing you must understand is that table data in Access is not sorted unless explicitly ordered when the table is created. Usually, the table sorting you see on-screen is a lie.
Create new query with all the table data, sort Ascending by JOB_ID
and save the query.
Create a second query and use the first query as the source data. We're treating the first query as if it were a table now. Access is OK with this. Go ahead and bring all of the table columns in for right now.
Create a new column next to the existing JOB_ID
field. Do this by
dragging some random field from down to the grid next to JOB_ID
.
Type over that field name this:
TEST_PART_ID:RunningCount([JOB_ID])
.
Open the VBE Editor (Alt-F11
) and enter the code below in a new
module.
Code:
Function RunningCount(WhatToCount As String) As Integer
Static CountSoFar As Long, var As String
If var <> WhatToCount Then
CountSoFar = 0
var = WhatToCount
End If
CountSoFar = CountSoFar + 1
RunningCount = CountSoFar
End Function
PART_ID
column.Upvotes: 1