Code writes to subsequent record instead of current record - ADO DAO RDO RDS

This is a discussion on Code writes to subsequent record instead of current record - ADO DAO RDO RDS ; 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 ...

+ Reply to Thread
Results 1 to 4 of 4

Code writes to subsequent record instead of current record

  1. Default Code writes to subsequent record instead of current record

    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

  2. Default Re: Code writes to subsequent record instead of current record

    hi,

    Memento wrote:
    > 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?

    Your INSERT INTO statement creates this new record. Just write your
    value to the current field:

    Me![Logonnaam] = txtLogonname



    mfG
    --> stefan <--

  3. Default Re: Code writes to subsequent record instead of current record

    Yes, that's what I tried earlier, Stefan, but it simply doesn't work. With my
    code it keeps writing the value of txtLogonname to a subsequent new record.
    This makes that it writes (for example) the value "Stefan" and the value
    "Hoffman" to a record, but it writes "shoffmann" to a subsequent record...


    "Stefan Hoffmann" wrote:

    > hi,
    >
    > Memento wrote:
    > > 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?

    > Your INSERT INTO statement creates this new record. Just write your
    > value to the current field:
    >
    > Me![Logonnaam] = txtLogonname
    >
    >
    >
    > mfG
    > --> stefan <--
    >


  4. Default Re: Code writes to subsequent record instead of current record

    hi,

    Memento wrote:
    > Yes, that's what I tried earlier, Stefan, but it simply doesn't work. With my
    > code it keeps writing the value of txtLogonname to a subsequent new record.
    > This makes that it writes (for example) the value "Stefan" and the value
    > "Hoffman" to a record, but it writes "shoffmann" to a subsequent record...


    Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim i As Integer
    Dim txtLogonname As String
    Dim blnAddUser As Boolean

    Cancel = True ' To control whether the record will be saved.
    blnAddUser = False

    If Not IsNull(txtSurName) And _
    Not IsNull(txtLastName) Then
    For i = 1 To 3
    txtLogonname = LCase(Left(txtSurName, i) & txtLastName)
    If DCount("*", _
    "Users", _
    "Logonnaam = '" & txtLogonname & "'") = 0 Then_
    blnAddUser = True
    Exit For
    End If
    Next i
    End If

    If blnAddUser Then
    ' Is this form based on your [Users] table?
    Me![Logonnaam] = txtLogonname
    Cancel = False
    End If

    End Sub


    mfG
    --> stefan <--


+ Reply to Thread

Similar Threads

  1. Email a single record or current record from a report
    By Application Development in forum ADO DAO RDO RDS
    Replies: 2
    Last Post: 12-19-2007, 09:18 PM
  2. One Access record updated to subsequent Excel row
    By Application Development in forum ADO DAO RDO RDS
    Replies: 1
    Last Post: 11-07-2007, 08:01 AM
  3. No current record error 3021
    By Application Development in forum ADO DAO RDO RDS
    Replies: 1
    Last Post: 09-06-2007, 12:11 AM
  4. No current record
    By Application Development in forum basic.visual
    Replies: 16
    Last Post: 11-15-2006, 09:09 AM
  5. fcc-hook / record = current mailbox
    By Application Development in forum Mutt
    Replies: 3
    Last Post: 04-06-2004, 10:49 AM