使用vba在Access 2010中使用其他表单中的ID打开表单

时间:2013-06-11 14:18:46

标签: vba access-vba ms-access-2010

我正在尝试从另一种形式的按钮打开表单。带有按钮的表单contract_all表单有一个字段ID,我想打开包含该ID信息的表单。第二种形式,即契约,具有附加信息,并具有允许编辑该特定合同的按钮。我设法获得了一些东西,但它给了我一个'运行时错误2489.表单合同没有打开' 代码如下。提前谢谢。

Private Sub Command74_Click()
    ID = [Forms]!Contracts_all![ID]
    DoCmd.GoToRecord acDataForm, "Contracts", ID
End Sub

2 个答案:

答案 0 :(得分:1)

Dim Rs As Recordset
Dim Test As Integer
Dim varBookmark As Variant

DoCmd.OpenForm "Contracts"


Set Rs = Forms!Contracts.RecordsetClone

    Rs.FindFirst ("[ID] = '" & Me![ID] & "'")

varBookmark = Rs.Bookmark
Forms!Contracts.Form.Bookmark = varBookmark

If Rs.NoMatch Then
  MsgBox "That does not exist in this database."
Else
End If

答案 1 :(得分:1)

我有答案..谢谢你的帮助..我最后做的是从Contracts_all表格获取ID,每次我重新打开合同表格。这是我的代码

Private Sub next_Click()
    On Error GoTo Err_next_Click
    DoCmd.SelectObject acForm, "contracts_all"
    DoCmd.GoToControl "ID"

    DoCmd.GoToRecord , , acNext
    DoCmd.OpenForm "Contracts", , , "ID = " & Forms!contracts_all![ID]

Exit_next_Click:
    Exit Sub

 Err_next_Click:
    MsgBox Err.Description
    Resume Exit_next_Click

 End Sub