1

我目前使用 If Not RS.EOF 和 Not RS.BOF is a DAO Recordset,但我不能在新的 SQL 后端环境中使用 DAO。代码如下:

Function CloseSession()
'This closes the open session

Dim Rs As DAO.Recordset
Set Rs = CurrentDb.OpenRecordset("SELECT * FROM Tbl_LoginSessions WHERE fldLoginKey =" & LngLoginId)

If Not Rs.EOF And Not Rs.BOF Then
    Rs.Edit
    Rs.Fields("fldLogoutEvent").Value = Now()
    Rs.Update
    Rs.Close
End If

Set Rs = CurrentDb.OpenRecordset("SELECT * FROM [Tbl_Users] WHERE PKUserID =" & LngUserID)

'Flag user as being logged out
    If Not Rs.EOF And Not Rs.BOF Then
        Rs.Edit
        Rs.Fields("fldLoggedIn").Value = 0
        Rs.Fields("FldComputer").Value = ""
        Rs.Update
        Rs.Close
    End If
Set Rs = Nothing
End Function

本质上,我已经开始在 ADODB 中编写代码。然而,在互联网上为 ADODB 研究 If Not RS.EOF 主题时,我完全没有成功。是否有人了解 RS.EOF 和 RS.BOF 的使用情况,这可能有助于我重写的困境?

Function CloseSession()

'/This closes the open session
'/Define the OLE DB connection string.
StrConnectionString = "DRIVER=SQL Server;SERVER=dbswd0027;UID=Mickey01;PWD=Mouse02;DATABASE=Regulatory;"

'/Instantiate the Connection object and open a database connection.
Set cnn = CreateObject("ADODB.Connection")
cnn.Open StrConnectionString

Dim strSQL1 As String
Dim strSQL2 As String
Dim StrLoginName As String
Dim StrComputerName As String

'/passing variables
StrComputerName = FindComputerName
strLoggedIn = "False"

'/Declaring what table you are passing the variables to
 strSQL1 = "Update tTbl_LoginSessions SET fldLogoutEvent = '" & Now() & "'" & _
 " WHERE fldLoginKey = " & LngLoginId

'/Declaring what table you are passing the variables to
 strSQL2 = "Update tTbl_LoginUsers SET fldLoggedIn = '" & strLoggedIn & "', fldComputer = '" & StrComputerName & "'" & _
 " WHERE intCPIIUserID = " & LngUserID

cnn.Execute strSQL1, , adCmdText + adExecuteNoRecords
cnn.Execute strSQL2, , adCmdText + adExecuteNoRecords
'/close connections and clean up
cnn.Close
Set cnn = Nothing

End Function
4

4 回答 4

0

“If Not Rs.EOF And Not Rs.BOF Then”是查看记录集是否为空的一种方式。只有当记录集为空时,Rs.BOF 和 Rs.EOF 才能同时为真。

于 2015-04-22T19:59:49.167 回答
0

在 DAO 情况下,您的代码使用 (DAO) Recordset 对象并避免显式 SQL 语句;在 ADO 情况下,您使用显式 SQL 语句并避免使用 (ADO) Recordset 对象。因此,对您的问题的简短回答是:使用 ADO Recordset 对象,您将拥有BOFEOFDAO 类似的属性(http://msdn.microsoft.com/en-us/library/windows/desktop/ms675787%28v =vs.85%29.aspx)。顺便说一句,您还在 ADO 案例中使用后期绑定,我建议您改用早期绑定(即,添加对 ADO 类型库的引用并使用强类型而非弱类型的对象变量)。

也就是说,在 DAO Recordset 案例中,您通常通过调用Database对象的OpenRecordset方法来获取它的实例;相反,您在调用其Open方法之前直接实例化 ADO 版本,通常传入一个 ADOConnection对象(一个 ADOConnection大致对应一个 DAO Database)。也没有明确的Edit方法:

Dim Connection As New ADODB.Connection, RS As New ADODB.Recordset
Connection.Open StrConnectionString
RS.Open "SELECT * FROM Tbl_LoginSessions WHERE fldLoginKey =" & LngLoginId,
    Connection, adOpenForwardOnly, adLockPessimistic
If Not Rs.EOF And Not Rs.BOF Then
    Rs.Fields("fldLogoutEvent").Value = Now() '!!!though see jacouh's comment
    Rs.Update
    Rs.Close
End If

RS.Open "SELECT * FROM [Tbl_Users] WHERE PKUserID =" & LngUserID,
    Connection, adOpenForwardOnly, adLockPessimistic
'Flag user as being logged out
If Not Rs.EOF And Not Rs.BOF Then
    Rs.Fields("fldLoggedIn").Value = 0
    Rs.Fields("FldComputer").Value = ""
    Rs.Update
    Rs.Close
End If
于 2013-11-14T23:08:42.050 回答
0

我建议你测试一下,我已经编译了,但不要运行它,因为我没有在 db 中创建这样的表:

Function CloseSession()

'/This closes the open session
'/Define the OLE DB connection string.
  Dim LngLoginId, LngUserID
  Dim nDone
  Dim StrConnectionString
  Dim strSQL1 As String
  Dim strSQL2 As String
  Dim strLoggedIn As String
  Dim StrLoginName As String
  Dim StrComputerName As String

  Dim cnn

  StrConnectionString = "DRIVER=SQL Server;SERVER=dbswd0027;UID=Mickey01;PWD=Mouse02;DATABASE=Regulatory;"

'/Instantiate the Connection object and open a database connection.
  Set cnn = CreateObject("ADODB.Connection")
  cnn.Open StrConnectionString

'/passing variables
' StrComputerName = FindComputerName
  StrComputerName = Environ("COMPUTERNAME")

  LngLoginId = 58
  LngUserID = 38
  strLoggedIn = "False"

'/Declaring what table you are passing the variables to
   strSQL1 = "UPDATE tTbl_LoginSessions" _
     & " SET fldLogoutEvent = '" & Format(Now(), "yyyy-mm-ddThh:mm:ss") & "'" & _
     " WHERE fldLoginKey = " & LngLoginId

'/Declaring what table you are passing the variables to
   strSQL2 = "UPDATE tTbl_LoginUsers" _
     & " SET fldLoggedIn = '" & strLoggedIn & "'" _
     & ", fldComputer = '" & StrComputerName & "'" & _
     " WHERE intCPIIUserID = " & LngUserID

  cnn.Execute strSQL1, nDone
  cnn.Execute strSQL2, nDone
'/close connections and clean up
  cnn.Close
  Set cnn = Nothing
'
End Function

此处不使用 RS.EOF,因为我们使用的是直接更新。

于 2013-11-14T23:20:59.110 回答
0

这是我的回答:

Function LogMeIn(sUser As Long)
' was Function LogMeIn()
'/Go to the users table and record that the user has logged in
'/and which computer they have logged in from

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String

'/Dim sUser As Long
Dim strComputerName As String, strLoggedIn As String

'passing variables
strLoggedIn = "True"
strComputerName = FindComputerName()


 'Declaring what table you are passing the variables to
  strSQL = "Update dbo.tTbl_LoginUsers SET fldLoggedIn = '" & strLoggedIn & "',      fldComputer = '" & strComputerName & "'" & _
 " WHERE intCPIIUserID = " & sUser
Debug.Print strSQL
'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
    'Debug.Print strSQL
End With

'close connections
con.Close
Set cmd = Nothing
Set con = Nothing

End Function
于 2013-12-11T16:28:28.823 回答