这可能是最简单的解决方法,但我需要获取自动编号并将其存储到一个公共变量中,该变量将用于识别用户所在的会话。当用户注销以关闭会话时使用此 ID。大胆的代码在 ACCESS 中严格使用,但我现在已将表移至 SQL,现在此代码不起作用。因此,需要修改下面的代码以适应此代码的其余部分。我需要 Recordsource 是 dbo.tTbl_LoginSessions。LngLoginID 稍后使用。如果有人可以帮助我,我将不胜感激。我了解到的是存储过程不起作用,@@IDENTITY、SCOPE_IDENTITY 和 IDENT_CURRENT 是类似的功能,但我听说这些可能是可疑的。这封电子邮件让我看起来比看起来更聪明,但相信我,我不是。因此,我需要婴儿步骤。
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 ('" & StrMSID & "','" & Now() & "','" & StrComputerName & "')"
'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
这是旧代码,在我转换之前。除自动编号外的所有功能均有效
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