1

我有一个登录屏幕,其中有一个下拉列表,其 MSID 来自 Table tTbl_LoginUsers MSID 对应于 PKUserId(它是一个整数),如下所示

PKUserID    MSID
1           jjenki02
2           trensc01 

我需要下面的代码来记下要用于 LngLongID 的 PKUserID,它将 MSID 的 1 或 2 个对应的 PKUSERID 放入我的 tTbl_LoginSessions 中的 fldUserName 列中,但是执行这会给我运行时错误“424”所需的对象。我不能为我的生活弄清楚为什么?有任何想法吗?

Private Sub CboUser_Click()
Dim MyCon As ADODB.Connection
Dim MyRS As ADODB.Recordset

    Set MyCon = New ADODB.Connection
    MyCon.Open "DRIVER=SQL Server;SERVER=dbswd****;UID=*****;PWD=*****;DATABASE=Regulatory;"
    Set MyRS = New ADODB.Recordset

'/The following code focuses on the DropDown field that the User selects their MSID
'/if MyNum = MSID, then MoveFirst to get the PKUserID(Column 1)from tTbl_LoginUsers
CboUser.SetFocus
MyNum = CboUser.Text
MyRS.Open "Select * from dbo.tTbl_LoginUsers Where MSID = '" & MyNum & "'", MyCon
Debug.Print strSQL

NewRecordRS.MoveFirst

LngLoginId = NewRecordRS!fldUserName


End Sub

新调整代码:tTbl_LoginSessions 具有以下字段:fldloginkey、fldUserName、fldLoginEvent、fld LogoutEvent、fldComputerName。.MoveFirst 触发它

Private Sub CboUser_Click()
Dim MyCon As ADODB.Connection
Dim MyRS As ADODB.Recordset

    Set MyCon = New ADODB.Connection
    MyCon.Open "DRIVER=SQL Server;SERVER=dbswd0027;UID=Mickey01;PWD=Mouse02;DATABASE=Regulatory;"
    Set MyRS = New ADODB.Recordset

With MyRS
'/The following code focuses on the DropDown field that the User selects their MSID
'/if that MyNum = MSID, then MoveFirst to get the PKUserID(Column 1)from tTbl_LoginUsers
     CboUser.SetFocus
     MyNum = CboUser.Text
     MyRS.Open "Select * from dbo.tTbl_LoginUsers Where MSID = '" & MyNum & "'", MyCon
     Debug.Print strSQL

   If Not (.BOF And .EOF) Then
        .MoveFirst
        LngLoginId = !fldUserName
    End If
End With
Debug.Print strSQL
'NewRecordRS.MoveFirst
'LngLoginId = NewRecordRS!fldUserName


End Sub

这是将该 PKUSERID 放入登录会话的代码:

Function CreateSession()
'/This function records the details regarding the login details of the person

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

Set NewIdentRS = New ADODB.Recordset

Dim LngLoginId As String, StrComputerName As String


'passing variables
StrMSID = LngLoginId
'old 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 & "')"
     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


    End With

    con.Close
    Set cmd = Nothing
    Set con = Nothing



End Function

我也有这个:

Public Function GrabMSID(frmObject)
GrabMSID = LngUserID
End Function
'Call it this way:
MyMSID = GrabMSID(Me)


Option Compare Database
Public LngLoginId As Long
4

1 回答 1

1

.MoveFirst行会引发错误,因为您没有名为NewRecordRS的变量。看起来你需要这个......

'NewRecordRS.MoveFirst
MyRS.MoveFirst

With或者考虑在一个块中使用一次记录集对象变量名......

With MyRS
    .Open "Select * from dbo.tTbl_LoginUsers Where MSID = '" & _
        MyNum & "'", MyCon
    If Not (.BOF And .EOF) Then
        .MoveFirst
        LngLoginId = !fldUserName
    End If
End With

注意:如果tTbl_LoginUsers不包含名为fldUserName的字段,则该代码将在此行抛出错误(“在与请求的名称或序号相对应的集合中找不到项目”):

        LngLoginId = !fldUserName

但是,我试图解决“需要对象”错误,这是您问题的原始主题。如果您因为fldUserName未在记录集中退出而遇到错误,则这是一个不同的错误。

于 2013-11-01T21:42:47.947 回答