子表单仅显示单条记录

发布于 2024-07-24 05:32:41 字数 383 浏览 6 评论 0原文

我有一个嵌入在表单(用户)中的子表单(用户子表单)。 我想要实现的是加载时,子表单应显示表中的所有用户以及主表单上的控件以充当过滤器。

如果从主窗体的组合框中选择用户 A,则子窗体中仅显示用户 A 的详细信息
如果从主窗体的组合框中清除选择(即 userA),则所有记录将在子窗体中再次弹出。

这听起来很简单,但对于初学者来说,当我加载表单时,子表单中仅显示 1 条记录,如果我在用户组合框中进行选择,则仅显示该用户的记录。 如果我能看到的只是一次记录一次,无论是否进行选择,它就违背了拥有子表单的目的。

我正在使用 Access '03。 有人能帮忙吗。 我这里有一个示例数据库,它已经成功实现了这一点,但我似乎找不到他们的设置与我的设置有何不同。

I have a subform(Users subform) embedded in a form(users). What I am trying to achieve is on load, the subform should show ALL users in the table and the controls on the main form to act as a filter.
i.e.
if user A is selected from a combobox in the main form, only User A's details show in the subform
if the selection (ie userA) is cleared from the combobox in the main form, ALL records will popup again in the subform.

This sound very simple but for starters, when I load my form, only 1 record is showing in the subform, If i make a selection in the user combobox, then that user's records show ONLY. It defeats the purpose of having a subform if all I can see is one record it a time with or without making a selection.

I am using Access '03. Can anyone help with this. I have a sample database here that has managed to achieve this but I cant seem to find a difference in their settings compared to mine.

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

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

发布评论

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

评论(4

秋心╮凉 2024-07-31 05:32:41

主窗体不应有记录源,也不应有链接标准。

过滤器控件的更新后事件应该更新子表单的过滤器(正如 CodeSlave 的代码所做的那样),您可以分配子表单的记录源(我倾向于后者,因为那样我就不必担心所有过滤器带来的问题)。

我过去曾多次这样做过,但通常不使用子表单。 我不喜欢数据表并使用连续表单,并将过滤控件放在表单的标题中。 然后,您可以避免引用子表单控件的困难(尽管这非常简单,因为您可以使用 With/End With 块,如 Praesagus 的帖子中所示)。

关键点是您希望主表单 UNBOUND,并且您希望放入条件的控件触发子表单的过滤。

The main form should have no recordsource, and there should be no link criteria.

The after update events of your filter controls should update the filter for the subform (as CodeSlave's code does it), you can assign the recordsource of the subform (I tend to prefer the latter, as then I don't have to worry about all the issues that come with filters).

I have done this many times in the past, but usually didn't use a subform. I am not fond of datasheets and use continuous forms, and put the filtering controls in the form's header. You then avoid the difficulties of referring to the subform control (though that's pretty easy as you can use a With/End With block, as seen in Praesagus's post).

The key point is that you want the main form UNBOUND and you want the controls where you put in criteria to trigger the filtering of the subform.

静谧 2024-07-31 05:32:41

我假设您已将子表单设置为“连续表单”或“数据表”。

当您的表单打开时,您的过滤器中是否还有内容? 您可能希望在主窗体打开时显式清除它,然后刷新数据。

编辑
这是我之前做过的事情:

Private Sub cmdCannedFilter_Click()
On Error GoTo Err_Click    
Dim strFilter As String
    strFilter = "(1 = 1)"
    strFilter = strFilter & " " & _
            "and (((someTable.Active)=Yes) "   

    Me![List_SubForm].Form.FilterOn = False
    Me![List_SubForm].Form.Filter = strFilter
    Me![List_SubForm].Form.FilterOn = True

Exit_Click:
    Exit Sub

Err_Click:
    MsgBox Err.Description
    Resume Exit_Click
End Sub

在本例中,这是从主窗体上的按钮运行的,而 List_SubForm 是(显然)子窗体。 我认为对我来说关键是必须关闭过滤器然后再打开。 我不记得为什么。

(1 = 1) 是因为我在其他地方做了类似的事情,我在它们飞行时构建查询,这意味着我不必每次都决定是否需要插入 < code> 和 是否存在。

你也在做类似的事情吗?

I presume that you have the sub form set up as either a "Continuous Form" or a "Data Sheet".

Does your filter still have something in it when your form opens? You might want to explicitly clear it when the master form opens and then refresh the data.

Edit
Here's something I've done before:

Private Sub cmdCannedFilter_Click()
On Error GoTo Err_Click    
Dim strFilter As String
    strFilter = "(1 = 1)"
    strFilter = strFilter & " " & _
            "and (((someTable.Active)=Yes) "   

    Me![List_SubForm].Form.FilterOn = False
    Me![List_SubForm].Form.Filter = strFilter
    Me![List_SubForm].Form.FilterOn = True

Exit_Click:
    Exit Sub

Err_Click:
    MsgBox Err.Description
    Resume Exit_Click
End Sub

In this case, this is running from a button on the master form, and List_SubForm is (obviously) the subform. I think the key thing for me was having to turn the filter off and then on again. I don't remember why.

The (1 = 1) is because I did something similar somewhere else where I was building up the query on they fly and it meant I didn't have to decide each time if I needed to plug an and in there or not.

Are you doing something similar?

梦幻之岛 2024-07-31 05:32:41

听起来您这里有两个不同的记录集。 选择用户时会显示用户详细信息。 当未选择任何用户时,您希望查看所有用户。 如果是这种情况,请使用两个子表单并在它们之间切换 - 我们将它们称为 fAllUsers 和 fUserDetails。 我们将子窗体控件名称命名为 sfUser。 我假设用户表单上的主键和 fUserDetails 上的外键是 userID。 确保子表单没有任何与父表单链接的字段。
家长:

Private Sub Form_Current()
    Dim sForm as string
    Dim sFilter as string
    Dim lUserID as long
    lUserID =nz(me!userID,0)
    sForm="fUserDetails"
    sFilter="userID=" & lUserID
    if lUserID<>0 then 
        sForm="fAllUsers"
        sFilter=""
    end if
    with sfUser
        .sourceobject=sForm
        .filter=sFilter
        .filteron=true
    end with
End Sub

希望有帮助。

It sounds like you have two different recordsets here. User details show when a user is selected. When no user is selected, you want to see all users. If that is the case, use two subforms and switch between them - we'll call them fAllUsers and fUserDetails. Lets call the subform control name sfUser. I am assuming that the the primary key on the user form and a foreign key on fUserDetails is userID. Make sure the subform does not have any fields linked with the parent.
In the parent:

Private Sub Form_Current()
    Dim sForm as string
    Dim sFilter as string
    Dim lUserID as long
    lUserID =nz(me!userID,0)
    sForm="fUserDetails"
    sFilter="userID=" & lUserID
    if lUserID<>0 then 
        sForm="fAllUsers"
        sFilter=""
    end if
    with sfUser
        .sourceobject=sForm
        .filter=sFilter
        .filteron=true
    end with
End Sub

Hope that helps.

风吹雪碎 2024-07-31 05:32:41

我有一个名为“TmprryQryFrLnkFldsCrtn0123456789”的随机(脏话)查询,我错误地删除了它,这给我带来了子表单上只显示一条记录的问题。 我只是碰巧将我拥有的备份数据库与我遇到问题的数据库进行了比较。

一旦我恢复查询,我的所有结果都会再次显示。

I had a random (Expletive) Query named "TmprryQryFrLnkFldsCrtn0123456789" that I deleted by mistake that gave me the problem of only one record showing up on my subform. I just happen to compare a backup DB I had to the one I was having a problem with.

Once I restored the query all my results were showing up again.

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