Results 1 to 16 of 16

Thread: [RESOLVED] Assiging numbers?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Resolved [RESOLVED] Assiging numbers?

    I have a query that finds certain data based upon what I want it to. It looks up info in tblEntrants, looking for category=2, gender=M, and racenumber is null. Basically I am trying to see if a RaceNumber exists. If it doesn't, assign the person(s) the next highest number between a start and stop number. Here is my code so far:

    Code:
    SELECT tblEntrants.Category, tblEntrants.Gender, tblEntrants.LastName, tblEntrants.RaceNumber
    FROM tblEntrants
    WHERE (((tblEntrants.Category)=2) AND ((tblEntrants.Gender)="M") AND ((tblEntrants.RaceNumber) Is Null))
    ORDER BY tblEntrants.LastName;
    This works perfect to get the names of the males in category 2 without a race number. I also have looked up the males in category 2 with race numbers. The male racenumbers are between 51-450. So far, 51-117 have been named and there are like 76 that havent. How can I assign racenumbers to the males that don't have one, based upon looking up the already existing numbers?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Assiging numbers?

    Any ideas?

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Assiging numbers?

    Step 1/ Determine the next available racenumber (using a recordset that will return 1 row only).
    Step 2/ Open a recordset containing all male entrants who don't have a race number.
    Step 3/ Loop through the second rs and insert the value for the next racenumber, incrementing the race number on each pass.
    Step 4/ Commit the changes to the 2nd recordset back into the database.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Assiging numbers?

    Well i created a query that finds the next highest number for the male and female after looking at the query i made to find all the male/female's w/ racenumbers. Would I do the other steps in a query or should I code it in a Module? I understand what you are saying in your steps, just cant think of how to implement it. I appreciate the help so far.

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Assiging numbers?

    OK, the following should help.
    A few things to note about this code.
    1/ I don't know how you are determining the next racenumber, so you will have to change the first SQL statement.
    2/ For the second recordset you only need to return the racenumber, as it is the only field that you are updating. If for some reason you need to add more fields to this RS (I can't think why) then you will need to update the field index where you are updating the racenumber.


    VB Code:
    1. Sub AddRaceNumbers()
    2. Dim conMyConnection As ADODB.Connection
    3. Dim rsLastNumber As ADODB.Recordset
    4. Dim rsMissingNumber As ADODB.Recordset
    5. Dim sSQL As String
    6. Dim lRaceNumber As Long
    7.  
    8.     Set conMyConnection = New ADODB.Connection
    9.    
    10.     'I'm assuming this code is in Access
    11.     'and taht you are connecting to the db that the code is in.
    12.     Set conMyConnection = CurrentProject.Connection
    13.    
    14.     'You will need to re-write this line as I don't know
    15.     'how you are deriving the last used number
    16.     sSQL = "Select Max(RaceNUM) FROM RaceTable"
    17.    
    18.     'Create the RS
    19.     Set rsLastNumber = New ADODB.Recordset
    20.    
    21.     'Open the RS with the last used number
    22.     rsLastNumber.Open sSQL, conMyConnection, adOpenKeyset
    23.    
    24.     'Put the last used number into a variable
    25.     lRaceNumber = CLng(rsLastNumber.Fields(0))
    26.    
    27.     'Get rid of this RS as we no longer need it
    28.     rsLastNumber.Close
    29.     Set rsLastNumber = Nothing
    30.    
    31.     'Only return the racenumber from the entrants table
    32.     'as it is the only field to be updated
    33.     sSQL = "SELECT RaceNumber FROM tblEntrants WHERE Category=2 AND Gender=""M"" AND RaceNumber Is Null"
    34.    
    35.    
    36.     Set rsMissingNumber = New ADODB.Recordset
    37.    
    38.     'Open the RS that contains the blank racenumbers
    39.     With rsMissingNumber
    40.         .Open sSQL, conMyConnection, adOpenForwardOnly, adLockPessimistic
    41.        
    42.         'Loop through the RS
    43.         Do While Not .EOF
    44.             'Increment the race number
    45.             lRaceNumber = lRaceNumber + 1
    46.             'Update the field
    47.             .Fields(0) = lRaceNumber
    48.             'Go to the next record
    49.             .MoveNext
    50.         Loop
    51.         'Finished with the RS so it can now be closed
    52.         .Close
    53.     End With
    54.    
    55.     'Clean up object variables
    56.     Set rsMissingNumber = Nothing
    57.     Set conMyConnection = Nothing
    58. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: Assiging numbers?

    OMG, dude, it works perfect. THANK YOU so much!! Thanks for taking time to help me. I owe you one.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Assiging numbers?

    Glad I could help.
    Don't forget to mark this as resolved
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: [RESOLVED] Assiging numbers?

    Oh, one more thing. Sorry. But is there any way I can make it so the males and females in category 2 dont go above a certain number? males are supposed to be between 51-450 and females 451-600. It is just how they want them numbered. Is this possible?

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Assiging numbers?

    What happens if there are more than 400 male runners?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: [RESOLVED] Assiging numbers?

    Then its their problem. I doubt though that there will be more than 400 male runners, running 10 K in under 40 mins. If their is, then they can number them some other way.

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Assiging numbers?

    OK, I've revised the code and made the following changes.

    1/ Made Category and Gender parameters of the procedure. (Should have done this the first time). This allows you to call the same procedure for all combos of category and gender.

    2/ Added a check in the update loop to see if you are outside the limits and stop the updates when this occurs.

    3/ the test procedure is just to show you how to call the main proc.


    VB Code:
    1. Sub test()
    2.     AddRaceNumbers 2, "M"
    3. End Sub
    4.  
    5. Sub AddRaceNumbers(ByVal lCategory As Long, ByVal sGender As String)
    6. Dim conMyConnection As ADODB.Connection
    7. Dim rsLastNumber As ADODB.Recordset
    8. Dim rsMissingNumber As ADODB.Recordset
    9. Dim sSQL As String
    10. Dim lRaceNumber As Long
    11.  
    12.     Set conMyConnection = New ADODB.Connection
    13.    
    14.     'I'm assuming this code is in Access
    15.     'and taht you are connecting to the db that the code is in.
    16.     Set conMyConnection = CurrentProject.Connection
    17.    
    18.     'You will need to re-write this line as I don't know
    19.     'how you are deriving the last used number
    20.     sSQL = "Select Max(RaceNUM) FROM RaceTable"
    21.    
    22.     'Create the RS
    23.     Set rsLastNumber = New ADODB.Recordset
    24.    
    25.     'Open the RS with the last used number
    26.     rsLastNumber.Open sSQL, conMyConnection, adOpenKeyset
    27.    
    28.     'Put the last used number into a variable
    29.     lRaceNumber = CLng(rsLastNumber.Fields(0))
    30.    
    31.     'Get rid of this RS as we no longer need it
    32.     rsLastNumber.Close
    33.     Set rsLastNumber = Nothing
    34.    
    35.     'Only return the racenumber from the entrants table
    36.     'as it is the only field to be updated
    37.     sSQL = "SELECT RaceNumber FROM tblEntrants "
    38.     sSQL = sSQL & "WHERE Category=" & CStr(lCategory)
    39.     sSQL = sSQL & " AND Gender=""" & sGender & """"
    40.     sSQL = sSQL & " AND RaceNumber Is Null"
    41.    
    42.    
    43.     Set rsMissingNumber = New ADODB.Recordset
    44.    
    45.     'Only the RS that contains the blank racenumbers
    46.     With rsMissingNumber
    47.         .Open sSQL, conMyConnection, adOpenForwardOnly, adLockPessimistic
    48.        
    49.         'Loop through the RS
    50.         Do While Not .EOF
    51.             'Increment the race number
    52.             lRaceNumber = lRaceNumber + 1
    53.             'Check to see if we are
    54.             'within the specified number range
    55.             'If we excede the limits then exit the loop
    56.             If (sGender = "M" And lRaceNumber > 450) _
    57.             Or (sGender = "S" And lRaceNumber > 600) Then
    58.                 MsgBox ("You have reached the limits of our numbering system")
    59.                 Exit Do
    60.             End If
    61.             'Update the field
    62.             .Fields(0) = lRaceNumber
    63.             'Go to the next record
    64.             .MoveNext
    65.         Loop
    66.         'Finished with the RS so it can now be closed
    67.         .Close
    68.     End With
    69.    
    70.     'Clean up object variables
    71.     Set rsMissingNumber = Nothing
    72.     Set conMyConnection = Nothing
    73. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: [RESOLVED] Assiging numbers?

    In the first sSql "Select Max(RaceNumber) FROM qCat2MNum". How would I put my code in? I mean which query do I use. I have one for male "qCat2MNum" and one for female "qCat2FNum". Should I just combine them into one query?

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Assiging numbers?

    Use an IF statement based on the value in the sGender variable to determine what SQL statement to use to get the last race number

    VB Code:
    1. If sGender = "M" Then
    2.     sSQL = "select max male number"
    3. Else
    4.     sSQL = "select max female number"
    5. End If
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: [RESOLVED] Assiging numbers?

    So put this code where the first sSql goes?

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] Assiging numbers?

    Yes, you need to replace the single line that previously set the sSQL variable with the revised code.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    73

    Re: [RESOLVED] Assiging numbers?

    Alright, thats what I figured. Once again man, you did it. Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width