【问题标题】:MS Access VBA: Properly Referencing a SubFormMS Access VBA:正确引用子表单
【发布时间】:2017-03-15 23:10:38
【问题描述】:

我在工作中为我的团队构建了一个 MS Access 课程管理工具。我有一个表格,最终用户可以在其中添加特定课程的培训材料。表格中的一个字段要求提供该材料用于的特定医院。要填充此表单,用户单击一个按钮,该按钮会显示一个表单(“sbfrmcoursematerialsite”),其中包含我们系统中所有可能的医院的列表,用户可以选择该列表。使用以下 SQL 代码填充表单:

SELECT pklistEntSites.Site FROM pklistEntSites UNION SELECT "          [Enterprise]" FROM pklistEntSites
ORDER BY pklistEntSites.Site;

此多选连接医院缩写并创建一个字符串以插入“sbfrmTrainingElements”中的“Site”字段(这是名为 txtSite 的 txtBox 的控制源)。我的问题是,当我单击“sbfrmcoursematerialsite”上的“确定”按钮时,我收到一条错误消息:无法执行此操作。 “sbfrmcoursematerialsite”表单中包含的我的 VBA 代码如下。连接函数正在工作,但错误出现在代码中的粗体行上。我是否没有正确引用要添加数据的子表单?

Private Sub cmdOk_Click()
Dim teID As Integer
Me.Refresh
teID = Me.txtTrainingElementID 'Document Training Element ID

DoCmd.SetWarnings False
'Update the Site field in tblTrainingElements for the specific training      element
'DoCmd.RunSQL "UPDATE tblTrainingElements SET [Site] = '" &     Me.txtSelectedSites & "' WHERE [Training Element ID] = " & teID & ";"
'Debug.Print Me.txtSite.Value
Debug.Print [Forms]![frmFullCourseInfo]![sbfrmTrainingElements]! [Site].Value
**[Forms]![frmFullCourseInfo]![sbfrmTrainingElements]![Site].Value =     Me.txtSelectedSites**
DoCmd.SetWarnings True
'Close the site select form
DoCmd.Close
End Sub

Private Sub Form_Load()
'Show current site selection
Me.txtSelectedSites = [Forms]![frmFullCourseInfo]! [sbfrmTrainingElements]![Site]

'Pass the training element ID from the last screen to current form
Dim i As Integer
i = CInt(Me.OpenArgs)
Me.txtTrainingElementID.Value = i

End Sub

Private Sub lboAllSites_Click()
   Dim strSelected As String
   Dim varItem As Variant

    With Me.lboAllSites
        For Each varItem In .ItemsSelected
            strSelected = strSelected & "," & .ItemData(varItem)
        Next varItem
        Me.txtSelectedSites = Mid(strSelected, 2)
    End With
End Sub

【问题讨论】:

  • 尝试添加 Form 对象以引用,如:[Forms]![frmFullCourseInfo]![sbfrmTrainingElements].Form![Site]
  • @Parfait:还要强调sbfrmTrainingElements必须是表单frmFullCourseInfo上的子表单控件的名称。

标签: ms-access vba


【解决方案1】:

必须引用子窗体容器控件。我总是给容器起一个与其所持有的对象不同的名称,例如 ctrTraining。

[表格]![frmFullCourseInfo]![ctrTraining]![站点]

【讨论】:

    【解决方案2】:

    感谢您的帮助。我发现了问题所在。 “站点”字段实际上有一个与之关联的查找(查找是由一位已离开该项目的同事创建的)。我试图通过删除查找选项卡中的信息来删除查找,尽管如此,它仍然保留了某种查找功能。我必须做的是删除这个字段,并创建一个具有相同名称的新字段。现在一切正常。再次感谢!

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 2019-01-03
      • 2023-04-09
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2014-02-14
      相关资源
      最近更新 更多