【问题标题】:Move subform to new record from called form将子表单从调用表单移动到新记录
【发布时间】:2020-11-07 20:26:05
【问题描述】:

我正在努力解决here 描述的问题。使用从主窗体调用的弹出窗口,我想向表中添加一条记录,更新显示该表中数据的连续子窗体,然后将子窗体移动到添加的记录。

但是,该解决方案不起作用,并且引发了与 here 中描述的相同的错误。以下是此设置的代码。

Private Sub Form_Load()
   Set prevForm = Screen.ActiveForm
   
   Me.cbo_EventCat = prevForm!cbo_EventCatSel2
   Me.cbo_EventType = prevForm!cbo_EventTypeSel2
   Me.cbo_SeasonStart = 1
   Me.cbo_SeasonEnd = 1
   Me.cbo_CostType = 1
   Me.txt_MinCost = ""
   Me.txt_MaxCost = ""
   Me.box_NewComments.Visible = False
   Me.txt_NewComments.Visible = False
   Me.box_OriginalComments.Visible = False
   Me.txt_OriginalComments.Visible = False
   Me.cbo_ExistingComments_Sel.Visible = False
   Me.box_NewBuiltComments.Visible = False
   Me.txt_NewBuiltComments.Visible = False
End Sub

Private Sub btn_SubmitAndReturn_Click()
   Dim strSQL, strSQLflds, strSQLvals, formName, strSubform As String
   Dim db As DAO.Database
   Dim rs As DAO.Recordset
   Dim recID As Integer
   Dim anyReqFldBlank As Boolean
   
   Set db = CurrentDb
   
   anyReqFldBlank = False
   
   strSQLflds = "INSERT INTO t_EventDetails (fkEventType,fkSeasonStart"
   strSQLvals = "VALUES (" & Me.cbo_EventType & ", " & Me.cbo_SeasonStart
   
   If Me.cbo_SeasonStart = 1 Then
      Me.cbo_SeasonStart.BorderColor = RGB(255, 0, 0)
      anyReqFldBlank = True
   Else
      Me.cbo_SeasonStart.BorderColor = RGB(0, 0, 0)
      anyReqFldBlank = False
   End If
   If Me.cbo_SeasonStart > 2 Then
      If Me.cbo_SeasonEnd = 1 Then
         Me.cbo_SeasonEnd.BorderColor = RGB(255, 0, 0)
         anyReqFldBlank = True
      Else
         Me.cbo_SeasonEnd.BorderColor = RGB(0, 0, 0)
         anyReqFldBlank = False
         strSQLflds = strSQLflds & ",fkSeasonEnd"
         strSQLvals = strSQLvals & ", " & Me.cbo_SeasonEnd
      End If
   End If
   If Me.cbo_CostType = 1 Then
      Me.cbo_CostType.BorderColor = RGB(255, 0, 0)
      anyReqFldBlank = True
   Else
      Me.cbo_CostType.BorderColor = RGB(0, 0, 0)
      anyReqFldBlank = False
      strSQLflds = strSQLflds & ",fkCostType"
      strSQLvals = strSQLvals & ", " & Me.cbo_CostType
      If Me.cbo_CostType = 3 Then
         strSQLflds = strSQLflds & ",MinCost"
         strSQLvals = strSQLvals & ", " & Me.txt_MinCost
      ElseIf Me.cbo_CostType = 5 Then
         strSQLflds = strSQLflds & ",MinCost,MaxCost"
         strSQLvals = strSQLvals & ", " & Me.txt_MinCost & ", " & Me.txt_MaxCost
      Else
      End If
   End If
   If Me.cbo_CommentsSel > 1 Then
      strSQLflds = strSQLflds & ",fkComments"
      strSQLvals = strSQLvals & ", " & Me.cbo_CommentsSel
   End If
   If anyReqFldBlank = True Then
      MsgBox ("Fill in required information.")
      Exit Sub
   Else
      strSQL = strSQL & strSQLflds & ") " & strSQLvals & ");"
      MsgBox ("strSQL = " & strSQL)
      db.Execute strSQL, dbFailOnError
      prevForm!chd_EventDetails2.SetFocus
      prevForm!chd_EventDetails2.Requery
      DoCmd.GoToRecord , , acLast
      DoCmd.Close acForm, "f_AddEventInfo", acSaveNo
   End If
End Sub

我也尝试过这些技术,但遇到了各种问题。

Set rs = db.OpenRecordset("SELECT * FROM t_EventDetails")
rs.MoveLast
DoCmd.GoToRecord , , acGoto, rs!fkID

让 Access 抛出一个错误,说我不能去那个记录。

DoCmd.GoToRecord , , acLast

什么都不做,也不抛出任何错误。

我不知道如何解决这个问题。

【问题讨论】:

    标签: vba ms-access subform


    【解决方案1】:

    尝试使用子表单的RecordsetClone

    Dim frm As Form
    Dim rst As DAO.Recordset
    
    Set frm = Forms!MainForm!SubformControlName.Form
    frm.Requery
    
    Set rst = frm.RecordsetClone
    rst.MoveLast
    ' Sync form to recordset.
    frm.Bookmark = rst.Bookmark
    rst.Close
    
    Set rst = Nothing
    Set frm = Nothing
    

    【讨论】:

      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 2012-08-02
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多