根据主窗体中的编号添加新的子窗体记录

时间:2017-05-08 22:38:09

标签: vba ms-access access-vba

我需要在mainfrom文本框中输入一个数值,并在子窗体上创建许多新记录。因此,mainform文本框中的值为4将创建4个新的子表单记录

我正在寻找基本代码可能看起来像的跳跃点示例。

这是我到目前为止所得到的。但是,它并没有创造新的记录。我得到你即将追加0行x次

Private Sub Command153_Click()
    Dim db As Database
    Dim sqlNewSamples As String
    Dim intNumberOfSamples As Integer
    Dim intNOSCounter As Integer

    intNOSCounter = 0
    intNumberOfSamples = Me.NumberOfSamples

    sqlNewSamples = "INSERT INTO tbl_Samples ( GroupID ) " & _
                    "SELECT tbl_Samples.GroupID " & _
                    "FROM tbl_Samples " & _
                    "WHERE (((tbl_Samples.GroupID)=[Forms]![frm_Login]![SampleGroupID]));"

    Do Until intNOSCounter = intNumberOfSamples
        DoCmd.RunSQL sqlNewSamples
        intNOSCounter = intNOSCounter + 1
    Loop   
End Sub
sqlNewSamples = "INSERT INTO [tbl_Samples] ( GroupID ) " & _
                "SELECT [tbl_Samples].GroupID " & _
                "FROM [tbl_Samples] " & _
                "WHERE ((([tbl_Samples].GroupID)=[Forms]![frm_Login]![SampleGroupID]));"

Private Sub Command153_Click()

Dim intNumberOfSamples As Integer
Dim intNOSCounter As Integer

  intNumberOfSamples = Me.NumberOfSamples

  For i = 1 To intNumberOfSamples
   rst.AddNew
   rst!GroupID = Me!SampleGroupID
   rst!SubID = i    '\\if you need distinct records
   rst.Update
Next i

Do Until intNOSCounter = intNumberOfSamples

   intNOSCounter = intNOSCounter + 1
Loop


End Sub
'Final Working Code
Private Sub Command154_Click()

Dim intNumberOfSamples As Integer
Dim intNOSCounter As Integer
Dim NrOfSamples As Integer
Dim strSampleNumber As String

    intNOSCounter = 0
    intNumberOfSamples = Me.NumberOfSamples

Do Until intNOSCounter = intNumberOfSamples

Forms![frm_Login]![Samples].SetFocus
Forms![frm_Login]![Samples].Form.GroupSampleNumber.SetFocus

    intNOSCounter = intNOSCounter + 1
Forms![frm_Login]![Samples].Form.Requery
DoCmd.GoToRecord , , acNewRec

    Loop

Me.SetFocus
Me.NumberOfSamples.SetFocus

End Sub

1 个答案:

答案 0 :(得分:0)

为什么不使用记录集,更新子表格?在此之前,删除子表格中的所有记录。

for i = 1 to intNumberOfSamples
   rst.addnew
   rst!GroupID = me!SampleGroupID
   rst!SubID = i    '\\if you need distinct records
   rst.update
next i