M
Memento
Hello Guys,
I'm having trouble with a form and some code. The goal is to automatically
create a logonname out of a filled-in sur- and lastname. The problem is that
the code works perfectly, but it writes to a next record, instead of the
current record that i'm creating at that moment. The code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
Dim txtLogonname As String
Dim blnAddedUser As Boolean
'check if the fields are filled
If Not IsNull(txtSurName) And Not IsNull(txtLastName) Then
For i = 1 To 3
txtLogonname = LCase(Left(txtSurName, i) & txtLastName)
'check if the new logon already exists in the users table
If IsNull(DLookup("Logonnaam", "Users", "Logonnaam = '" & _
txtLogonname & "'")) Then
CurrentDb.Execute ("INSERT INTO Users ([Logonnaam]) " &
"VALUES (""" & txtLogonname & """);"), dbFailOnError
blnAddedUser = True
Exit For
End If
Next i
If Not blnAddedUser Then
MsgBox "Couldn't create the new logon. Already existing
logonname " & _
"with the same first three letters and last name", _
vbExclamation, "Warning"
End If
Else
MsgBox "Please, fill the fields with the given name and the
lastname", _
vbExclamatoin, "Warning"
End If
End Sub
How can i get this code to write the logonname to the current record, where
it should be, and not to a new subsequent record?
Thanks in advance,
Sven
I'm having trouble with a form and some code. The goal is to automatically
create a logonname out of a filled-in sur- and lastname. The problem is that
the code works perfectly, but it writes to a next record, instead of the
current record that i'm creating at that moment. The code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim i As Integer
Dim txtLogonname As String
Dim blnAddedUser As Boolean
'check if the fields are filled
If Not IsNull(txtSurName) And Not IsNull(txtLastName) Then
For i = 1 To 3
txtLogonname = LCase(Left(txtSurName, i) & txtLastName)
'check if the new logon already exists in the users table
If IsNull(DLookup("Logonnaam", "Users", "Logonnaam = '" & _
txtLogonname & "'")) Then
CurrentDb.Execute ("INSERT INTO Users ([Logonnaam]) " &
"VALUES (""" & txtLogonname & """);"), dbFailOnError
blnAddedUser = True
Exit For
End If
Next i
If Not blnAddedUser Then
MsgBox "Couldn't create the new logon. Already existing
logonname " & _
"with the same first three letters and last name", _
vbExclamation, "Warning"
End If
Else
MsgBox "Please, fill the fields with the given name and the
lastname", _
vbExclamatoin, "Warning"
End If
End Sub
How can i get this code to write the logonname to the current record, where
it should be, and not to a new subsequent record?
Thanks in advance,
Sven