我正在建立一个数据库来跟踪现场人员的待命时间表。通常情况下,员工随叫随到整整一周,但是,他们偶尔会花一些特定时间。我在Access 2003中有一个表单,其中有一个选项组,表示完整周与部分周。如果表单用户选择“部分周”,它将启用一组复选框,让用户指定特定日期(周一,周二,周三等)。我希望能做的是:
如果用户选择“完整周”,表单将创建7条记录,每天一个(周一至周日),其中所有字段都相同,除了“星期几”字段。但是,如果他们选择“Partial Week”,它应该只创建与用户选中复选框一样多的记录(因此,如果他们选择星期一和星期二,表单会创建两个记录:一个星期一等于“星期几”和一个“周日”等于星期二。)
这可能以任何方式/形状/形式吗?
答案 0 :(得分:1)
根据单选按钮和所选复选框的指示,多次使用DoCmd.RunSQL "insert into ..."
。
答案 1 :(得分:0)
Dim db As Database
Set db = CurrentDb()
If FullWeek Or Monday Then
db.Execute "insert into ... " // record for Monday
End If
If FullWeek Or Tuesday Then
db.Execute "insert into ... " // record for Tuesday
End If
// etc...
db.Close
请勿使用DoCmd.RunSQL
。这通常是一个坏主意,因为它的行为取决于用户设置。如果您使用数据库Execute
方法,则不会遇到该问题。
答案 2 :(得分:0)
通过将工作日定义为数据库中的表格,并使用带有复选框或其他内容的Listview,可能有更好的方法来完成您要执行的操作。但是,考虑到您当前的型号,这将完成您所需的目标:
编辑代码:
Option Compare Database
Option Explicit
'Declare a Collection to hold references
'to your checkboxes at the form level:
Private DayCheckBoxes As Collection
'Use the Form_Load Event to set up:
Private Sub Form_Load()
'Initialize the Form Variable:
Set DayCheckBoxes = New Collection
'Add each checkbox to the collection:
With DayCheckBoxes
.Add Me.chkSun
.Add Me.chkMon
.Add Me.chkTue
.Add Me.chkWed
.Add Me.chkThu
.Add Me.chkFri
.Add Me.chkSat
End With
'A local variable to walk the collection:
Dim CurrentCheckBox As CheckBox
'Walk through the controls, and set the .Tag property to
'hold the name for each weekday. Note that in this case,
'the weekday name is extracted from each control name:
For Each CurrentCheckBox In DayCheckBoxes
CurrentCheckBox.Tag = Replace(CurrentCheckBox.Name, "chk", "")
Next
End Sub
Private Sub cmdOK_Click()
Call UpdateSchedule
End Sub
Private Sub UpdateSchedule()
Dim db As Database
Set db = CurrentDb()
'Local variable used to walk the collection:
Dim CurrentCheckBox As CheckBox
'Walk through the collection:
For Each CurrentCheckBox In DayCheckBoxes
'If the checkbox is checked, execute your INSERT
'(Assumes your optionGroup values are 1 and 2 for full and partial, respectively):
If Me.frmFullOrPartial = 1 Or (Me.frmFullOrPartial = 2 And CurrentCheckBox) Then
'I used a really simple INSERT to test everythiing. Put your own SQL in here:
db.Execute "INSERT INTO ScheduledDays (DayOfWeek) VALUES ( '" & CurrentCheckBox.Tag & "' )"
End If
Next
End Sub