【问题标题】:Code returns incorrect records/fails to update form correctly代码返回不正确的记录/未能正确更新表单
【发布时间】:2019-04-18 11:01:46
【问题描述】:

因此,我将尽力解释背景,以澄清我希望实现的目标。我有一个带有选项卡控件的表单(事件),该选项卡控件有 3 个不同的页面,每个页面都有不同的子表单。

其中一个子表单(演讲者)有一个按钮控件,用于检查“演讲者”的参加者 ID,如果匹配,则打开“入职”表单以找到匹配的记录。

如果记录不匹配,应该做的是打开表单到新记录并插入 ContactID、AttendeeID 和 EventID。

这是代码的第 4 次迭代,可以在一定程度上工作/不返回错误

Dim myR As Boolean
Dim strSQL As String
Dim myV1 As Integer
Dim myV2 As Integer
Dim myV3 As Integer

'Define the variables to be input
myV1 = Me.AttendeeID
myV2 = Me.ContactID
myV3 = Me.EventID

'Is there a Matching Onboard Record for the current Attendee Record?
myR = DCount("*", "tbl_Onboarding", "[AttendeeID] = " & Me.AttendeeID) > 0

If myR = True Then
  DoCmd.OpenForm "usf_Onboarding", acNormal, , , acFormEdit, acWindowNormal     'Edit Mode
Else        'No Matching Record, so Add one
  If Me.Dirty = True Then Me.Dirty = False      'Save pending Edits
  DoCmd.OpenForm "usf_Onboarding", acNormal, , , acFormAdd, acWindowNormal      'Add Mode

  Forms!usf_Onboarding!txt_ForceFocus.SetFocus

  strSQL = "INSERT INTO tbl_Onboarding (AttendeeID,ContactID,EventID) VALUES (" & myV1 & ", " & myV2 & ", " & myV3 & ")"

  Debug.Print strSQL

  CurrentDb.Execute strSQL, dbFailOnError

  Me.Requery

  DoCmd.Close acForm, "usf_Events", acSaveYes

End If

使用立即我已经确认 myV1、myV2 和 myV3 都保持正确的值,并且我使用断点逐行检查并且所有似乎都运行正常,返回零错误但我仍然有以下问题,具体取决于是否myR 是真/假

  1. 如果记录存在 (myR = TRUE),那么现在是什么原因导致它 仅加载表的第一条记录(不是 与会者 ID 匹配)。
  2. 如果在运行后记录不存在 (myR = FALSE) 表中包含带有联系人ID/AttendeeID/EventID 的新记录 但表单不会更新以显示这一点(表单控件已设置 to locked = False) 这会导致关闭时出错。所以 选择不保存数据(在框上单击是丢弃 更改)允许强制退出,但记录/数据保存为 联系人ID/参加者ID/事件ID

Debug.Print strSQL 也没有做任何奇怪的事情。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    问题 1.“如果记录存在 (myR = TRUE),那么现在某些原因导致它只加载表的第一条记录(不是与参加者 ID 匹配的记录)。”

    我需要在 openform 命令中添加 where 子句

    DoCmd.OpenForm "usf_Onboarding", acNormal, , "AttendeeID = " & Me.AttendeeID, acFormEdit, acWindowNormal 
    

    问题 2.“如果在运行后记录不存在 (myR = FALSE),则表中包含带有联系人 ID/参加者 ID/事件 ID 的新记录,但表单不会更新以显示这一点(表单控件是设置为locked = False)这会导致关闭时出错。因此选择不保存数据(单击框上的是放弃更改)允许强制退出,但记录/数据保存为contactID/AttendeeID/EventID“

    我在创建新记录之前加载表单,然后尝试重新查询另一条新记录,而不是它创建的记录。我只需要先添加记录,然后使用上面的打开代码来加载该记录

    Else        'No Matching Record, so Add one
      If Me.Dirty = True Then Me.Dirty = False      'Save pending Edits
    
      strSQL = "INSERT INTO tbl_Onboarding (AttendeeID,ContactID,EventID) VALUES (" & myV1 & ", " & myV2 & ", " & myV3 & ")"
    
      CurrentDb.Execute strSQL, dbFailOnError
    
      DoCmd.OpenForm "usf_Onboarding", acNormal, , "AttendeeID = " & Me.AttendeeID, acFormEdit, acWindowNormal
    

    【讨论】:

      猜你喜欢
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多