MS Access标题分配按钮

时间:2013-03-22 23:07:12

标签: database ms-access button dao caption

我正在尝试在MS Access的“代码生成器”部分编写代码,该代码将从记录中的按钮中提取标题。我有一个名为Main Menu的表,其中包含“RecordID,Caption和Report to Run”字段。我需要此按钮根据MainMenu表中的标题名称分配标题。

实施例。按钮A(记录ID = 1,Caption =借款人,报告运行= rptCurrentBorrowers)。

通过阅读按钮A的记录中的标题,点击我需要此按钮的标题为=借阅者。我无法在需要从MainMenu表中提取的按钮中专门编写标题。

2 个答案:

答案 0 :(得分:1)

我的猜测是这个表单显示来自其他表的记录,当您浏览这些记录时,您希望按钮标题根据[Main Menu]表上的查找进行更改。

如果是这样,那么该表单On Current事件中类似于以下内容的声明(Sub Form_Current())可能会起到作用:

me.Button_A.Caption = DLookup("Caption", "Main Menu", "RecordID=" & me.RecordID.Value)

答案 1 :(得分:1)

戈登汤普森指出我在正确的位置。因此,经过大量的研究,我不仅能够发现如何实现这种类型的代码,还能发现它为何有用。 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