這可能是最簡單的修復方法,但我需要獲取自動編號並將其存儲到公用變量中,該變量將用於標識用戶所處的會話。此用戶名在用戶註銷時使用關閉會議。 Bolded代碼在ACCESS中嚴格使用,但現在我已將表移至SQL,現在此代碼不起作用。因此,下面的代碼需要修改以適應其餘的代碼。我需要Recordsource是dbo.tTbl_LoginSessions。 LngLoginID稍後使用。如果有人能幫助我,我將非常感激。我所學到的是一個存儲過程不起作用,@@ IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT是類似的功能,但我聽說這些可能是可疑的。這封電子郵件讓我看起來比我看起來更聰明,但相信我,我不是。因此,我需要寶寶的步驟。MoveFirst DoEvents MoveLast
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
讓我不寒而慄看着VB6 ......但無論如何,範圍身份將正常工作假設你是直接操作 – Charleh
後抓住它會是什麼,我需要更換Rs.MoveFirst行,DOEVENTS ,Rs.MoveLast,LngLoginId = Rs(0),因爲我放在那裏的所有東西似乎都出錯了,似乎對我沒有用處?如果某人在同一時間正確登錄,會發生什麼? –
我現在在我的手機上,但不久就要回家了,當我回來的時候生病會幫助你! – Charleh