title: MoveFirst DoEvents MoveLast
body: <p>This is probably the simplest fix, but I need to get the Autonumber and store it to a public variable that will be used to identify the session the user is in. This ID is used when the User logs off to close a session. The <strong>Bolded</strong> code was used strictly in ACCESS, but I have now moved the tables over to SQL and now this code does not work. Hence,the code below needs to be modified to fit with the rest of this code. I need the Recordsource to be the dbo.tTbl_LoginSessions. The LngLoginID is used later. I would be very grateful if someone could help me with this. What I have learned is that a Stored Procedure will not work, @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions but I have heard that these can be suspect. This email makes me appear smarter than I appear, but trust me I am not. Therefore, I need baby steps.</p>
  
  <pre><code>    Function CreateSession()
  'This closes the open session
  Dim con As ADODB.Connection
  Dim cmd As ADODB.Command
  Dim strSQL As String
  Dim WhoAmI As Long
  
  Dim StrLoginName As String, StrComputerName As String
  
  'passing variables
  StrMSID = StrLoginName
  StrComputerName = FindComputerName
  
  'Declaring what table you are passing the variables to
  strSQL = "Insert into dbo.tTbl_LoginSessions(fldUserName, fldLoginEvent, fldComputerName) Values ('" &amp; StrMSID &amp; "','" &amp; Now() &amp; "','" &amp; StrComputerName &amp; "')"
  
  'connect to SQL Server
  Set con = New ADODB.Connection
  With con
  .ConnectionString = cSQLConn
  .Open
  End With
  'write back
  Set cmd = New ADODB.Command
  With CMD
  .ActiveConnection = con
  .CommandText = strSQL
  .CommandType = adCmdText
  .Execute
  End With
  
  '/Next get the autonumber and store it to a public variable that will be used to
  '/identify this session.
  
  '/This id is used when user logs off to close this session.
  **Rs.MoveFirst**    
  **DoEvents**
  **Rs.MoveLast**
  **LngLoginId = Rs(0)** 
  Debug.Print strSQL
  'close connections
  con.Close
  Set cmd = Nothing
  Set con = Nothing
  
  
  
  End Function 
  </code></pre>
  
  <p>THIS WAS THE OLD CODE, BEFORE I CONVERTED IT. Everything but the Autonumber works</p>
  
  <pre><code> Function CreateSession(WhoAmi As Long)
      '/This function records the details regarding the login details of the person
  
      Dim Rs As DAO.Recordset
  
  
  Set Rs = CurrentDb.OpenRecordset("Tbl_LoginSessions")
      Rs.AddNew
      Rs.Fields("fldUserName").Value = StrLoginName
      Rs.Fields("fldComputerName").Value = StrComputerName
      Rs.Fields("fldLoginEvent").Value = Now()
      Rs.Update
      '/Next get the autonumber and store it to a public variable that will be used to
      '/identify this session.
      '/This id is used when user logs off to close this session.
      Rs.MoveFirst
      DoEvents
      Rs.MoveLast
      LngLoginId = Rs(0)
  
      Rs.Close
  Set Rs = Nothing
  
  End Function
  </code></pre>