Ms access 需要运行时错误“424”对象-MoveFirst错误
我有一个带有下拉列表的登录屏幕,其中有来自tTbl_LoginUsers表的MSID MSID对应于PKUserId,PKUserId是一个整数,如下所示Ms access 需要运行时错误“424”对象-MoveFirst错误,ms-access,vba,Ms Access,Vba,我有一个带有下拉列表的登录屏幕,其中有来自tTbl_LoginUsers表的MSID MSID对应于PKUserId,PKUserId是一个整数,如下所示 PKUserID MSID 1 jjenki02 2 trensc01 我需要下面的代码来说明用于LngLongID的PKUserID,它将MSID的1或2个对应PKUserID放入fldUserName列中的我的tTbl_登录会话中,但执行此操作会产生所需的运行时错误“424”对象。我一辈子
PKUserID MSID
1 jjenki02
2 trensc01
我需要下面的代码来说明用于LngLongID的PKUserID,它将MSID的1或2个对应PKUserID放入fldUserName列中的我的tTbl_登录会话中,但执行此操作会产生所需的运行时错误“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。 .移动首先触发它
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
.MoveFirst行抛出错误,因为您没有名为NewRecordRS的变量。看起来你需要这个
“NewRecordRS.MoveFirst
迈尔斯·莫夫特
或考虑使用记录集对象变量名称在一个带块……/P>中使用一次。 用迈尔斯 .从dbo.tTbl_LoginUsers中打开Select*,其中MSID='&_ MyNum&',MyCon 如果不是.BOF和.EOF,则 .先走一步 LngLoginId=!fldUserName 如果结束 以 注意:如果tTbl_LoginUsers不包含名为fldUserName的字段,则该代码将抛出一个错误项,该错误项在与此行请求的名称或序号对应的集合中找不到:
LngLoginId=!fldUserName 然而,我试图解决对象要求错误,这是您问题的原始主题。如果由于fldUserName未在记录集中退出而遇到错误,则这是另一个错误。由于没有名为NewRecordRS的变量,.MoveFirst行引发错误。看起来你需要这个 “NewRecordRS.MoveFirst 迈尔斯·莫夫特或考虑使用记录集对象变量名称在一个带块……/P>中使用一次。 用迈尔斯 .从dbo.tTbl_LoginUsers中打开Select*,其中MSID='&_ MyNum&',MyCon 如果不是.BOF和.EOF,则 .先走一步 LngLoginId=!fldUserName 如果结束 以 注意:如果tTbl_LoginUsers不包含名为fldUserName的字段,则该代码将抛出一个错误项,该错误项在与此行请求的名称或序号对应的集合中找不到:
LngLoginId=!fldUserName然而,我试图解决对象要求错误,这是您问题的原始主题。如果随后由于fldUserName未在记录集中退出而遇到错误,则是另一个错误。太好了,我使用了With块,现在得到一个运行时错误“3265”:在与请求的名称或序号对应的集合中找不到项。我检查了所有的格式,所有的整数。我们需要更多的信息来诊断这个错误。哪一行触发错误?该表是否包含名为fldUserName的字段?tTbl_LoginSessions包含以下字段:fldloginkey、fldUserName、fldLoginEvent、fld LOGOUTPEVENT、fldComputerName。在这种情况下,您应该会在MyRS.Open处收到一个错误,因为查询包含Where MSID=并且您刚刚向我们显示该表不包含名为MSID的字段。抱歉,tTbl_LoginUsers有以下列:PKUserID、MSID、ACtive、fldLoggedIn、fldComputer、strEmpEmail、strEmpEmail、strEmpLastName、strEmpLastName。我正在尝试查找此表上的MSID,获取相应的PKUserID并将其放置在fldUserName中以用于注销用户。这有用吗?感谢您的耐心。太好了,我使用了With块,现在我得到一个运行时错误“3265”:在与请求的名称或序号对应的集合中找不到项。我检查了所有的格式,所有的整数。我们需要更多的信息来诊断这个错误。哪一行触发错误?该表是否包含名为fldUserName的字段?tTbl_LoginSessions包含以下字段:fldloginkey、fldUserName、fldLoginEvent、fld LOGOUTPEVENT、fldComputerName。在这种情况下,您应该会在MyRS.Open处收到一个错误,因为查询包含Where MSID=并且您刚刚向我们显示该表不包含名为MSID的字段。抱歉,tTbl_LoginUsers有以下列:PKUserID、MSID、ACtive、fldLoggedIn、fldComputer、strEmpEmail、strEmpEmail、strEmpLastName、strEmpLastName。我正在尝试查找此表上的MSID,获取相应的PKUserID并将其放置在fldUserName中以用于注销用户。这有用吗?感谢您的耐心。在dbo.tTbl_LoginSessionsfldUserName、fldLoginEvent、fldComputerName值、'11/4/2013 9:38:26 AM'、'LH7U0CNU139430Z'中插入代码可以很好地将日期/时间和用户放入计算机,但第一个引号应该包含我们试图通过的整数。在dbo.tTbl_LoginSessionsfldUserName中插入,fldLoginEvent,fldComputerName Values,'11/4/2013 9:38:26 AM','LH7U0CNU139430Z'代码非常有效,可以将日期/时间和用户 中文,但第一个引号应该是我们试图通过的整数。