首页 > 解决方案 > 通过父窗体上的按钮创建新记录并从父窗体插入数据以在子窗体 Access/SQL Server 上查询

问题描述

我有一个主窗体和一个子窗体,显示另一个表中的相关记录。我创建了一个按钮来为该表创建新记录。但是,当表单打开时,我得到一个完全空白的屏幕或表中的第一条记录,即使已经传递了密钥。

我的父表单是 GroupSummaryDetails,我的子表单是 Event。PRAC_ID 是连接到子窗体的父窗体中的主键。Event 表单有一个以 event_ID 作为主键的表,但它们是通过查询 PRAC_ID 上的 Event 表单来连接的。

用于子记录的查询在数据表视图中显示所有预期的记录。

我对 Access 中的表单非常陌生。我在后端运行 Access 2016 和 SQL Server。

如果查看表单本身,子表单将显示一个带有第一条信息记录的屏幕。

在其他各种事情中,我在按钮后面使用 vba 尝试过(当然一次一个):

Dim stDocName As String <br>
Dim stLinkCrteria As String <br>

stLinkCriteria = "[PRAC_ID]=" & "'" & Me![PRAC_ID] & "'"

stDocName = "Event"

DoCmd.OpenForm stDocName, , , stLinkCriteria

 
 'This method, which is an append query attempt to append what appears to be the current two records in the table, which it can't due to primary key restraints and which isn't what I want anyway
DoCmd.OpenQuery "qEventAdd"


'This method opens a completely blanks form, no fields or labels, etc
DoCmd.OpenForm "Event",
    WhereCondition:="PRAC_ID=" & "'" & Me.PRAC_ID & "'"

'This method opens first record in event table
DoCmd.OpenForm "Event", , , , acFormAdd

'This line says you cannot use gotorecord action or method on an object in design view even though it's in form view
DoCmd.GoToRecord acDataForm, "Event", acNewRec

'Using the following two lines it inserts a new record to the table
'but how do I open it?
strSQL = "INSERT INTO dbo_events([PRAC_ID])VALUES(Forms!GroupSummaryDetails!PRAC_ID)"
DoCmd.RunSQL strSQL

标签: sql-serverms-access-2016

解决方案


推荐阅读