在我的用户搜索表单中,我显示一个用户,当我点击搜索按钮时,我想在我的主要用户详细信息表单中打开此记录。
我目前的代码是我的UserSearch表单,如下所示。 User#是主UserDetails表单中的主键字段
Private Sub cmdSearch_Click()
'open record in user details form
'record set is set to that users number so when the form is closed
'the main form will display the selected record
Dim strUserNo As String
strUserNo = Me.cboUser_.Value
rstUserSearch.FindFirst ("[User#] = " & strUserNo & "'")
DoCmd.OpenForm "frmUserDetails", , , "[User#] = " & strUserNo
End Sub
在我的主窗体中调用它的代码如下:
Private Sub Form_Load()
'create a connection with the current database
Call setDatabaseConnection
'create a set of user detail records
Set rstUserDetails = dbase.OpenRecordset("tblUser", dbOpenDynaset)
'if record opened from another form
Dim strUser As String
If Not IsNull(Forms!frmUserDetails.OpenArgs) Then
strUser = Forms!frmUserDetails.OpenArgs
rstUserDetails.FindFirst ("[User#] = '" & strUser & "' ")
End If
'make all text boxes read-only and greyed out ...
txtuser_.Locked = False
txtuser_.BackColor = &HCCCECC
txtForename.Locked = False
txtForename.BackColor = &HCCCECC
txtSurname.Locked = False
txtSurname.BackColor = &HCCCECC
txtDepartment.Locked = False
txtDepartment.BackColor = &HCCCECC
txtEmail.Locked = False
txtEmail.BackColor = &HCCCECC
txtPhoneNo.Locked = False
txtPhoneNo.BackColor = &HCCCECC
txtComputerNo.Locked = False
txtComputerNo.BackColor = &HCCCECC
End Sub
这样做只会打开表单,但不会显示所需的记录,所以任何帮助都会很棒!
我还添加了以下内容,但收到错误'object variable not set'
Dim db As Database
Set rstUserSearch = db.OpenRecordset("SELECT tblUser.* FROM tblUser ORDER BY tblUser.[User#];")
答案 0 :(得分:0)
要拨打您使用的表单:
DoCmd.OpenForm "frmUserDetails", , , "[User#] = " & strUserNo
我认为应该是
DoCmd.OpenForm "frmUserDetails", , , "[User#] = '" & strUserNo & "'"
第二,在你的Form_Load
事件中,你得到了:
If Not IsNull(Forms!frmUserDetails.OpenArgs) Then
strUser = Forms!frmUserDetails.OpenArgs
rstUserDetails.FindFirst ("[User#] = '" & strUser & "' ")
End If
我认为你可以删除所有这些,因为你正在打开你的表单应用WHERE CONDITION(一个字符串表达式,它是一个有效的SQL WHERE子句,没有单词WHERE。)。您可以按[User#](主键字段)进行过滤,以便它可以正常工作。