我有一个简单的活动出勤表单,其中有一个组合框用于标识“活动”(带有绑定表的组合框)和一个未绑定的文本框,用于输入活动的“日期”。
执行命令按钮(生成活动名单)时,使用活动名称运行选择查询,以过滤通常参加此活动的成员列表(来自表:T:ActivityRoster)。结果将返回到“活动出勤”表单中。然后,用户可以检查出勤情况。 (我将在稍后的活动名册中添加/删除成员)
我现在的挑战是将带有活动日期的查询字段(ActivityID,MemberID,Attended,AmtSpent)插入Table-T:AttendanceHistory。
我已尝试过网络上的工会和其他选项,但似乎都没有适合这种情况。任何建议都将不胜感激。
新代码:
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
答案 0 :(得分:0)
使用VBA可以通过两种方式来实现这一目标:
对于选项1:
您应该在表单代码中添加一个过程:
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
仅当子表单中只有一行时才有效。
对于选项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
这些只是让您走上正轨的例子。
希望这有帮助