Access 2007 基于主窗体中的字段筛选子窗体?

发布于 2024-11-18 18:43:56 字数 635 浏览 7 评论 0原文

我的任务是使用 Access 2007 创建一个简单的应用程序来维护用户的收藏品集合。我已经创建并实现了一些请求。它们是:

  1. 一个列出他所有收藏品的主窗体
  2. 该主窗体下面有一个选项卡式控件,每个选项卡都包含一个子窗体,该子窗体实际上根据主窗体的不同标准“过滤”数据。例如,第一个子表单采用主表单中收藏人物的名称,并在子表单中显示使用该名称的所有其他记录。换句话说,如果人物是“Darth Vader”,子表单将列出所有名称为“Darth Vader”的收藏品。

我已经根据用户请求创建了该应用程序,但到目前为止,有一件事困扰着我们俩。子窗体的第一条记录与主窗体相同。我们都觉得这是多余的,而且很烦人。当然,我的 Access 技能充其量也很弱,所以这可能是一个简单的修复,但是有没有办法删除子表单中的重复记录?我尝试在子表单中实现 where 子句,声明主表单中不包含“图形 ID”。问题是,它的行为就像一个参数提示,当我打开子窗体或主窗体时询问主窗体的FigureID。如果我输入人物 ID,它会起作用,但提示显然是不想要的。

仅供参考:

  1. 主表单基于一个查询,该查询基本上从“Figures”表和其他相关表中选择所有记录
  2. 当我将子表单控件拖放到选项卡控件上时,创建了子表单,在其中链接了必要的主字段和子字段

I was tasked with creating a simple app to maintain a user's collectibles collection using Access 2007. There were some requests, which I have created and implemented. Those being:

  1. One main form listing all of his collectibles
  2. That same main form has a tabbed control below, with each tab containing a subform that in effect "filters" data based on different criteria from the main form. For example, the 1st subform takes the name of the collectible figure in the main form, and displays all other records using that name in the subform. In other words, if the figure is "Darth Vader", the subform would list all collectibles that have the name "Darth Vader".

I have created the application as per user request, but there is one thing that is bothering both of us so far. The subform's first record is the same as the main form. We both find that redundant, and annoying. Granted, my Access skills is weak at best, so this may be a simple fix, but is there a way to remove the duplicate record in the subform? I have tried implementing a where clause in the subform stating to not include the "Figure ID" in the main form. Problem is, it is acting like a Parameter prompt, asking for the main form's FigureID when I open the subform, or the main form. If I type in the Figure ID, it works, but the prompt is something that is obviously not wanted.

FYI:

  1. The main form is based on a query that basically selects all records from the "Figures" table and other related tables
  2. The subform was created when I dropped the subform control onto the tab control, where I linked the necessary master and child fields

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-11-25 18:43:56

假设您有一个名为 frmMain 的表单。该表单在其记录源中包含两个字段:FigureID;和图名。该表单还包括一个名为 txtFigureID 的文本框控件,该控件绑定到FigureID 记录源字段。

frmMain 还包含一个基于名为 frmSub 的窗体的子窗体控件。 frmSub 的记录源还包括FigureID 和Figure_name 字段。子窗体控件的链接主/子字段属性是Figure_name。因此,frmSub 将显示Figure_name 与frmMain 当前记录中相应值匹配的所有行。

现在,如果您希望 frmSub 排除 frmMain 中当前记录的特定记录(由唯一的FigureID 值标识),请在 frmSub 的记录源查询中添加一个 WHERE 子句:

WHERE FigureID <> Forms!frmMain!txtFigureID

我只是在这里猜测,但希望描述是足够接近您的实际情况,很有用。如果没有,请向我们展示您用作子表单记录源的 SQL。

编辑:只有在第一次打开frmMain时才会出现参数提示。之后,您可以在 frmMain 中的记录之间导航,而 frmSub 只会显示您想要查看的记录...而不会再次要求您提供参数值。

发生这种情况的原因是因为子表单在其父表单之前加载...因此当子表单加载时父表单上的控件不可用。

我认为解决办法可能是在其记录源中保存没有 WHERE 条件的子表单。然后,当主窗体加载时,它可以重写子窗体的记录源以包含 WHERE 条件。

因此,在 frmMain 的加载事件中:

Private Sub Form_Load()
    Dim strSql As String
    strSql = "SELECT FigureID, Figure_name FROM YourTable" & vbCrLf & _
        "WHERE FigureID <> Forms!frmMain!txtFigureID"
    Debug.Print strSql
    Me.subformControlName.Form.RecordSource = strSql
End Sub

注意 subformControlName。它是一个控件,而不是一个表单。子表单控件可能与其包含的表单具有相同的名称。但它可能是一个不同的名字。

Say you have a form named frmMain. That form includes two fields in its record source: FigureID; and Figure_name. The form also includes a text box control named txtFigureID which is bound to the FigureID record source field.

frmMain also contains a subform control based on a form named frmSub. The record source for frmSub also includes FigureID and Figure_name fields. The subform control's link master/child field property is Figure_name. Therefore frmSub will display all the rows where Figure_name matches the corresponding value in the frmMain's current record.

Now, if you want frmSub to exclude the specific record (as identified by the unique FigureID value) which is the current record in frmMain, add a WHERE clause to frmSub's record source query:

WHERE FigureID <> Forms!frmMain!txtFigureID

I'm only guessing here, but hope that description is close enough to your actual situation to be useful. If not, show us the SQL you're using as the record source for your subform.

Edit: You get the parameter prompt only when you first open frmMain. Afterwards, you can navigate between records in frmMain, and frmSub shows you only the records you want to see ... without asking you again to supply a parameter value.

The reason that happens is because the subform loads before its parent form ... so a control on the parent form is not available when the subform loads.

I think the cure may be to save the subform without the WHERE condition in its record source. Then, when the main form loads, it can re-write the subform's record source to include the WHERE condition.

So, in frmMain's load event:

Private Sub Form_Load()
    Dim strSql As String
    strSql = "SELECT FigureID, Figure_name FROM YourTable" & vbCrLf & _
        "WHERE FigureID <> Forms!frmMain!txtFigureID"
    Debug.Print strSql
    Me.subformControlName.Form.RecordSource = strSql
End Sub

Watch out for subformControlName. It's a control, not a form. The subform control may have the same name as the form it contains. But it could be a different name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文