如何使用表单中的查询记录将多个记录添加到表中

时间:2018-12-26 05:30:14

标签: ms-access

我有一个GoatMasterTable。我已经基于GoatmasterTable创建了针对特定条件的查询“ Query1”。我已在新表单中将query1转换为“ Query1”表单,但标题信息很少,并使用此Query1表单显示了多个记录。

我想将标题信息和每一行查询写到一个名为Medicine的表中。

当我尝试使用以下代码时,查询的第一行重复查询行的数量。我无法从查询表单

的其余记录中选择第二行,依此类推

我正在使用以下代码

Private Sub exitprograme5()

Dim i As Integer
Dim Db As Database
Dim Rs As Recordset
Dim Trn As String
Set Db = CurrentDb
Set Rs = Db.OpenRecordset("Query1")

Do While Not Rs.EOF
     'To add Next De-Warming Kid Entry
     'MsgBox " Kid De-Warming Record writing"
     Dim Db1 As Database
     Dim Rs1 As Recordset
     Set Db1 = CurrentDb
     Set Rs1 = Db.OpenRecordset("Medicine")

     Rs1.AddNew
     Rs1.Fields("dateofMedication") = Me.Text62.Value
     Rs1.Fields("InjuctionType") = Me.Combo68.Value
     Rs1.Fields("Medicine") = Me.Text58.Value
     Rs1.Fields("Dose") = Me.Text60.Value
     Rs1.Fields("Problem Summary") = "De-Warming to Kids "
     Rs1.Fields("TagID") = Me.Query1.Form!TagID
     Rs1.Fields("BreedType") = Me.Query1.Form!BreedType
     Rs1.Fields("GoatGender") = Me.Query1.Form!GoatGender
     Rs1.Fields("FemaleStatus") = Me.Query1.Form!FemaleStatus
     Rs1.Fields("BodyDescription") = Me.Query1.Form!BodyDescription
     Rs1.Fields("Category") = Me.Query1.Form!Category
     Rs1.Fields("HealthStatus") = Me.Query1.Form!HealthStatus
     Rs1.Update
     Rs1.Close
     Set Rs1 = Nothing
     Db1.Close
     Rs.MoveNext
Loop
'DoCmd.Close
Rs.Close
Set Rs = Nothing
Db.Close

End Sub

1 个答案:

答案 0 :(得分:0)

代码未引用rs的字段。应该用Me.Query1.Form代替rs!。在循环外部打开和关闭Rs1和Db1。

INSERT SELECT操作SQL可能更有效。以下假设字段是除dateofMedication外的文本类型。如果任何字段是数字类型,请删除撇号定界符。

Private Sub exitprograme5()

  'To add Next De-Warming Kid Entry
  'MsgBox " Kid De-Warming Record writing"
  CurrentDb.Execute "INSERT INTO Medicine(dateofMedication, InjuctionType, Medicine, Dose, " & _
  "[Problem Summary], TagID, BreedType, GoatGender, FemaleStatus, BodyDescription, Category, HealthStatus) " & _
  "SELECT #" & Me.Text62 & "#,'" & Me.Combo68 & "','" & Me.Text58 & "','" Me.Text60 & "','De-Warming to Kids'," & _
  "TagID,BreedType,GoatGender,FemaleStatus,BodyDescription,Category,HealthStatus FROM Query1"

End Sub