简体   繁体   中英

Assign an autonumber to query results with VBA

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.

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
  • Save the Module and then go back to your query and preview the query results. If the results are just what you're expecting, ditch this query as a successful test, and use this two query + VBA solution to make an Update Query on the original Table. Updating the existing PART_ID column.

@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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM