S
Sam D
Hi and thanks for your thoughts,
A Students table (and form) has LastName, FirstName and UserName fields
(controls). I've written a function to create/check UserName.
This code is executing in the forms beforeupdate event:
UserName = GetUniqueUserName(StudentID, LastName, FirstName, UserName)
The function is:
Public Function GetUniqueUserName(StudentID, LastName, FirstName,
CurrentUserName) As String
Dim tmpUserName As String
Dim newUserName As String
Dim c As Long
'is there already a CurrentUserName
If IsNull(CurrentUserName) Then
tmpUserName = LCase(Left(FirstName, 1) & Left(LastName, 4))
Else
tmpUserName = CurrentUserName
End If
'force username to be at least 5 characters
'simply append left part of abcde to ensure this is so
If Len(tmpUserName) < 5 Then
tmpUserName = tmpUserName & Left("abcde", 5 - Len(tmpUserName))
End If
'need to check this username is unique
newUserName = tmpUserName
c = 1
While DCount("*", "Students", "Username = '" & newUserName & "' AND
StudentID <> " & StudentID) > 0
newUserName = tmpUserName & c
c = c + 1
Wend
'return the username
GetUniqueUserName = newUserName
End Function
All works great when entering new students one at a time via the form.
However if a class of student names is "Paste Appended" then problems occur:
e.g.
Davis Sam
Davis Steve
Fails on Davis Steve as the function does not detect Davis Sam's username
when doing the DCount. ie. Davis Sam has UserName = "sdavi" yet DCount
returns 0.
Is there a command to force each new record to be written to the table as
the PasteAppend occurs so DCount can use it?
Sam
A Students table (and form) has LastName, FirstName and UserName fields
(controls). I've written a function to create/check UserName.
This code is executing in the forms beforeupdate event:
UserName = GetUniqueUserName(StudentID, LastName, FirstName, UserName)
The function is:
Public Function GetUniqueUserName(StudentID, LastName, FirstName,
CurrentUserName) As String
Dim tmpUserName As String
Dim newUserName As String
Dim c As Long
'is there already a CurrentUserName
If IsNull(CurrentUserName) Then
tmpUserName = LCase(Left(FirstName, 1) & Left(LastName, 4))
Else
tmpUserName = CurrentUserName
End If
'force username to be at least 5 characters
'simply append left part of abcde to ensure this is so
If Len(tmpUserName) < 5 Then
tmpUserName = tmpUserName & Left("abcde", 5 - Len(tmpUserName))
End If
'need to check this username is unique
newUserName = tmpUserName
c = 1
While DCount("*", "Students", "Username = '" & newUserName & "' AND
StudentID <> " & StudentID) > 0
newUserName = tmpUserName & c
c = c + 1
Wend
'return the username
GetUniqueUserName = newUserName
End Function
All works great when entering new students one at a time via the form.
However if a class of student names is "Paste Appended" then problems occur:
e.g.
Davis Sam
Davis Steve
Fails on Davis Steve as the function does not detect Davis Sam's username
when doing the DCount. ie. Davis Sam has UserName = "sdavi" yet DCount
returns 0.
Is there a command to force each new record to be written to the table as
the PasteAppend occurs so DCount can use it?
Sam