[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?
Re: [RESOLVED] Assiging numbers?
Glad I could help.
Don't forget to mark this as resolved ;)
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?
Re: [RESOLVED] Assiging numbers?
What happens if there are more than 400 male runners?
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.
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:
Sub test()
AddRaceNumbers 2, "M"
End Sub
Sub AddRaceNumbers(ByVal lCategory As Long, ByVal sGender As String)
Dim conMyConnection As ADODB.Connection
Dim rsLastNumber As ADODB.Recordset
Dim rsMissingNumber As ADODB.Recordset
Dim sSQL As String
Dim lRaceNumber As Long
Set conMyConnection = New ADODB.Connection
'I'm assuming this code is in Access
'and taht you are connecting to the db that the code is in.
Set conMyConnection = CurrentProject.Connection
'You will need to re-write this line as I don't know
'how you are deriving the last used number
sSQL = "Select Max(RaceNUM) FROM RaceTable"
'Create the RS
Set rsLastNumber = New ADODB.Recordset
'Open the RS with the last used number
rsLastNumber.Open sSQL, conMyConnection, adOpenKeyset
'Put the last used number into a variable
lRaceNumber = CLng(rsLastNumber.Fields(0))
'Get rid of this RS as we no longer need it
rsLastNumber.Close
Set rsLastNumber = Nothing
'Only return the racenumber from the entrants table
'as it is the only field to be updated
sSQL = "SELECT RaceNumber FROM tblEntrants "
sSQL = sSQL & "WHERE Category=" & CStr(lCategory)
sSQL = sSQL & " AND Gender=""" & sGender & """"
sSQL = sSQL & " AND RaceNumber Is Null"
Set rsMissingNumber = New ADODB.Recordset
'Only the RS that contains the blank racenumbers
With rsMissingNumber
.Open sSQL, conMyConnection, adOpenForwardOnly, adLockPessimistic
'Loop through the RS
Do While Not .EOF
'Increment the race number
lRaceNumber = lRaceNumber + 1
'Check to see if we are
'within the specified number range
'If we excede the limits then exit the loop
If (sGender = "M" And lRaceNumber > 450) _
Or (sGender = "S" And lRaceNumber > 600) Then
MsgBox ("You have reached the limits of our numbering system")
Exit Do
End If
'Update the field
.Fields(0) = lRaceNumber
'Go to the next record
.MoveNext
Loop
'Finished with the RS so it can now be closed
.Close
End With
'Clean up object variables
Set rsMissingNumber = Nothing
Set conMyConnection = Nothing
End Sub
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?
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:
If sGender = "M" Then
sSQL = "select max male number"
Else
sSQL = "select max female number"
End If
Re: [RESOLVED] Assiging numbers?
So put this code where the first sSql goes?
Re: [RESOLVED] Assiging numbers?
Yes, you need to replace the single line that previously set the sSQL variable with the revised code.
Re: [RESOLVED] Assiging numbers?
Alright, thats what I figured. Once again man, you did it. Thank you very much!