戈德汤普森指出我在正确的位置。因此,在进行了大量研究之后,我不仅发现了如何实现这种类型的代码,而且发现了它为什么有用。DLookup 只需在数据库中搜索记录或查询,然后根据其标准将信息拉入其脚本位置。我需要为按钮标题实现此功能的原因是,我的最终用户能够从表格中更改按钮的标题,而不是操纵实际代码。这是我完成的 VBA 代码,带有工作按钮。
Private Sub Form_Open(Cancel As Integer)
rptBorrower.Caption = DLookup("Caption", "MainMenu", "ReportID = 1")
rptMediaList.Caption = DLookup("Caption", "MainMenu", "ReportID = 2")
rptPastDue.Caption = DLookup("Caption", "MainMenu", "ReportID = 3")
End Sub
'' This code defines the Borrower Button
Private Sub rptBorrower_Click()
rptBorrower.Caption = DLookup("Caption", "MainMenu", "ReportID = 1")
Dim varBorrower As Variant
varBorrower = DLookup("[ReportToRun]", "MainMenu", "[ReportID] = 1")
DoCmd.OpenReport varBorrower, acViewReport
rptBorrower_Click_Exit:
Exit Sub
End Sub
'' This code is defines the Media List Button
Private Sub rptMediaList_Click()
rptMediaList.Caption = DLookup("Caption", "MainMenu", "ReportID = 2")
Dim varMediaList As Variant
varMediaList = DLookup("[ReportToRun]", "MainMenu", "[ReportID] = 2")
DoCmd.OpenReport varMediaList, acViewReport
rptMediaList_Click_Exit:
Exit Sub
End Sub
'' This code defines the Past Due Report Button
Private Sub rptPastDue_Click()
rptPastDue.Caption = DLookup("Caption", "MainMenu", "ReportID = 3")
Dim varPastDue As Variant
varPastDue = DLookup("[ReportToRun]", "MainMenu", "[ReportID] = 3")
DoCmd.OpenReport varPastDue, acViewReport
rptPastDue_Click_Exit:
Exit Sub
End Sub