简体   繁体   English

可以编写 Access VBA 代码以从查询和表单字段插入到表中吗?

[英]Can Access VBA code be written to insert into a table from both a Query and a Form field?

I have a simple Activity Attendance Form that has a combo box for use to identify an “Activity” (combo box with a bound table) and an unbound text box to enter the “date” of the Activity.我有一个简单的活动出勤表,它有一个组合框,用于识别“活动”(带有绑定表格的组合框)和一个未绑定的文本框,用于输入活动的“日期”。

在此处输入图片说明

When a command button (Generate Activity Roster) is executed, a Select Query is run using the Activity Name to filter the list of members who normally attend this activity (from a table: T:ActivityRoster).当执行命令按钮(生成活动名册)时,将使用活动名称运行选择查询以过滤通常参加此活动的成员列表(来自表:T:ActivityRoster)。 The results are returned to the Activity Attendance form.结果将返回到活动出勤表。 The user can then check off the attendance.然后用户可以核对出勤情况。 (I will work on the add/delete members from the Activity Roster later) (稍后我将处理从活动名册中添加/删除成员的工作)

My challenge is now to Insert the Query fields (ActivityID, MemberID, Attended, AmtSpent) with the Activity Date into the Table- T:AttendanceHistory.我现在面临的挑战是将带有活动日期的查询字段(ActivityID、MemberID、Attended、AmtSpent)插入到表 T:AttendanceHistory 中。

在此处输入图片说明

I have tried unions and other options from the web, but none seem to fit this scenario.我已经尝试过网络上的工会和其他选项,但似乎没有一个适合这种情况。 Any suggestions would be greatly appreciated.任何建议将不胜感激。

new code:新代码:

 Option Compare Database Option Explicit Private Sub cmdSaveAttRec_Click() Dim actDate As Date, val1 As Long, val2 As Long, val3 As Boolean, val4 As Currency Dim db As DAO.Database, rsIn As DAO.Recordset, rsOut As DAO.Recordset Dim strSQL As String Set db = CurrentDb() strSQL = "SELECT [T:ActivityList].ActivityName, [T:ActivityRoster].ActivityID, [T:ActivityRoster].MemberID, [T:MemberInfo].MemLastNam, [T:MemberInfo].MemFirstNam, [T:MemberInfo].MemMidIni, [T:ActivityRoster].Attended, [T:ActivityRoster].AmtSpent FROM [T:MemberInfo] INNER JOIN ([T:ActivityList] INNER JOIN [T:ActivityRoster] ON [T:ActivityList].ActivityID = [T:ActivityRoster].ActivityID) ON [T:MemberInfo].MemberID = [T:ActivityRoster].MemberID WHERE ((([T:ActivityList].ActivityName)=[Forms]![F:ActivityAttendance]![cboActivityName]))" Set rsIn = db.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly) Set rsOut = db.OpenRecordset("T:AttendanceHistory", dbOpenDynaset, dbEditAdd) actDate = Me.ActivityDate.Value ' Here you must replace this with the actual control name With rsIn .MoveFirst Do val1 = !ActivityID ' Or the column name of your recordset val2 = !MemberID val3 = !Attended val4 = !AmtSpent With rsOut .AddNew !ActivityDate = actDate !ActivityID = val1 !MemberID = val2 !Attended = val3 !AmtSpent = val4 .Update End With .MoveNext Loop Until .EOF .Close End With End Sub

There are two ways you can acomplish this using VBA:有两种方法可以使用 VBA 完成此操作:

  1. Read the values from the controls and insert them in your table从控件中读取值并将它们插入表中
  2. Execute the query directly from VBA, reading the values and insert them into your table直接从 VBA 执行查询,读取值并将它们插入到表中

For option 1:对于选项 1:

You should add a procedure to your form code:您应该在表单代码中添加一个过程:

sub readValuesFromSubForm()
   dim actDate as date, val1 as String
   dim db as DAO.Database, rs as DAO.recordset
   set db = currentDb()
   set rs = db.openrecordset("tbl_output", dbOpenDynaset, dbEditAdd)

   actDate = me.actDate.value  ' Here you must replace this with the actual control name
   ' Read the values from the subform controls:
   val1 = me.subForm.Form.txtVal1.value ' Replace with actual subform and control names

   ' Insert the new row into your table
   with rs
       .addNew
           !actDate = actDate
           !val1 = val1
       .update
       .close
   end with
   db.close
end sub

This only works if there's only one row in the sub form.这仅在子表单中只有一行时才有效。

For option 2:对于选项 2:

sub readValuesFromQuery()
    dim actDate as date, val1 as String
    dim db as DAO.Database, rsIn as DAO.recordset, rsOut as DAO.recordset
    dim strSQL as string
    set db = currentDb()
    strSQL = "select ..." ' Put your query here
    set rsIn = db.openRecordset(strSQL, dbOpenDynaset, dbReadOnly)
    set rsOut = db.openrecordset("tbl_output", dbOpenDynaset, dbEditAdd)
    actDate = me.actDate.value  ' Here you must replace this with the actual control name
    with rsIn
        .moveFirst
        do
           val1 = !val1 ' Or the column name of your recordset
           with rsOut
               .addNew
                   !actDate = actDate
                   !val1 = val1
               .update
           end with
           .moveNext
        loop until .EOF
        .close
    end with
end sub

These are only examples to put you in the right track.这些只是让你走上正轨的例子。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM