访问-如何将动态加载的无界子表单链接到主表单? [英] Access - How to link dynamically loaded unbounded subforms to main form?

查看:98
本文介绍了访问-如何将动态加载的无界子表单链接到主表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问和vba的新功能.

New to access and vba.

我有一个绑定的主表单(FormA),上面有一个组合框,还有两个未绑定的子表单(subfrmA和subfrmB).(两种形式都附加到一个表上,但是我希望它们加载到放置未绑定子表单作为占位符的主表单上)

I have a bound main form (FormA) with a combobox on it and two unbound subforms (subfrmA & subfrmB). (Both forms are attached to a table however I want them to load onto the main form where I placed an unbound subform as a placeholder)

组合框具有两个值"a"和"b".选择a时,我希望将subfrmA加载到Form A上.选择b时,我希望将subfrmB加载到Form A上.—到目前为止,我认为这部分起作用了

The combobox has two values "a" and "b." When a is selected I want subfrm A to load onto Form A. When b is selected I want subfrmB to load onto Form A. — So far I think have this part working

但是,当我在主窗体上选择一条记录时,关联的子窗体不会出现.当我尝试将子表单链接到主表单时,出现错误消息,提示我无法在未绑定的表单之间建立链接.

However when I select a record on the main form the associated subforms doesn’t appear. When I try to link the subforms to the main form an error message appears saying I can’t build a link between unbound forms.

packageID是主窗体和子窗体之间的链接,snd是所有窗体上的隐藏字段.每当packageID自动更新时,子表单字段中的psckageID也会更新.

The packageID is the link between the main form and subform snd is a hidden field on all forms. Whenever the packageID is automatically updated the psckageID in the subform fields are also updated.

Case"A"                  
            Me.subfrmAB.SourceObject="FormA
            Me.packageDetailsID=Me.subfrmAB.packageDetailsID
Case "B"

                                                              
            Me.subfrmAB.SourceObject="FormB"
            Me.packageDetailsID=Me.subfrmAB.packageDetailsID

所以我最后要做的是创建两个子窗体subfrmA(窗体A)和subfrmB(窗体B).然后,我通过主链接和子链接都链接到父表单.

So what I ended up doing was creating two subforms subfrmA (Form A) and subfrmB (Form B). Then I linked both to the parent form via the master and child links.

根据用户在主窗体的组合框中选择的内容,使一个子窗体可见,而另一子窗体不可见.

I make one of the subforms visible and the other invisible depending on what the user selects in the combobox of the main form.

除了可以加载表格B之外,其他一切工作都很完美,但是容器可以加载.我尝试单独加载表格B,但仍然无法加载.我还删除了subfrmA,但B表格仍无法加载.

Everything works perfect except Form B won’t load, but the container loads. I tried loading Form B separately by itself it still won’t load. I also deleted subfrmA and Form B still doesn’t load.

这是我编辑的代码:

    Select Case Me.Authorization.Text
        Case "A"
              Me.subfrmA.Visible = True
              Me.subfrmB.Visible = False
              Me.subfrmA.SourceObject = "Form.A"
        Case "B"
              Me.subfrmB.Visible = True
              Me.subfrmA.Visible = False
              Me.subfrmB.SourceObject = "Form.B"
    End Select

唯一不起作用的行是Me.subfrmB.SourceObject ="Form.B",实际上是有某些因素阻止了表单的特别加载.我为Form A和Form B编写了相同的代码,但无法找出导致Form B无法加载的问题.

The only line that doesn’t work is the Me.subfrmB.SourceObject="Form.B" and really there’s something that’s preventing the form specifically loading. I wrote the same code for Form A and Form B but can’t figure out what’s wrong with Form B that’s preventing it from loading .

推荐答案

当然可以.这是一个对我有用的简单示例.

Can certainly be done. Here is a simple example that works for me.

主表格绑定到桌面游戏.用作子表单的表单是公断人和团队.

Main form is bound to table Games. Forms used as subform are Umpires and Teams.

组合框属性:

ControlSource:UNBOUND
RowSource:审判员;板; UmpID;团队; HomeTeam; TeamID
RowSourceType:ValueList
绑定列:1
列数:3
ColumnWidths:1.0; 0&"; 0"

ControlSource: UNBOUND
RowSource: Umpires;Plate;UmpID;Teams;HomeTeam;TeamID
RowSourceType: ValueList
BoundColumn: 1
ColumnCount: 3
ColumnWidths: 1.0";0";0"

代码:

Private Sub Combo108_AfterUpdate()
With Me
.ctrAB.SourceObject = .Combo108
.ctrAB.LinkMasterFields = .Combo108.Column(1)
.ctrAB.LinkChildFields = .Combo108.Column(2)
End With
End Sub

您可能会有"A","B"组合框RowSource中的表单名称,然后如果两个表单具有相同的名称键字段,则在RowSource中不需要它们,只需进行硬编码即可.尚不清楚关键字段名称是什么.然后像这样的代码:

You could have "A", "B" for the form names in combobox RowSource and then if both forms have same name key fields, don't need them in RowSource, just hard coded. Not entirely clear what the key field names are. Then code like:

.subfrmAB.SourceObject = "subfrm" & .Combo108
.subfrmAB.LinkMasterFields ="packageDetailsID"
.subfrmAB.LinkChildFields = "packageDetailsID"

如果要保存"A",和"B"表示到主窗体记录,然后将组合框绑定到字段.然后,为了在导航主表单时更改每个记录的子表单,还需要在表单OnCurrent事件中添加代码.

If you want to save "A" and "B" to main form record, then bind the combobox to field. Then for subforms to change for each record while navigating main form, also have code in form OnCurrent event.

在编写表单/子表单之间的交互时需要注意的一些事情:子表单在主表单之前加载-看起来很奇怪,但确实如此.

Something to be aware of when coding interaction between form/subform: subforms load before main form - seems odd but is true.

这篇关于访问-如何将动态加载的无界子表单链接到主表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆