M
Marcelo Henderson via AccessMonster.com
I'm trying to make a calendar schedule table with 2 fields: SchDate, SchTime.
where is the error?
Option Compare Database
Public Function MakeSchedule(strTable As String, _
dtmStart As Date, _
dtmEnd As Date, _
dtmDayStart As Date, _
dtmDayEnd As Date, _
intMinuteInterval As Integer, _
ParamArray varDays() As Variant)
' Accepts: Name of schedule table to be created: String.
' Start date for calendar: DateTime.
' End date for calendar: DateTime.
' Time when first 'time-slot' starts each day: DateTime
' Time when last 'time-slot' starts each day: DateTime
' Length of each 'time-slot' in schedule in minutes: Integer
' Days of week to be included in calendar
' as value list, e,g 2,3,4,5,6 for Mon-Fri
' (use 0 to include all days of week)
Dim cmd As ADODB.Command
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim strSQL As String
Dim dtmDate As Date
Dim dtmTime As Variant
Dim varDay As Variant
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
Set cat = New Catalog
cat.ActiveConnection = CurrentProject.Connection
' does table exist? If so delete it
On Error Resume Next
Set tbl = cat(strTable)
If Err = 0 Then
strSQL = "DROP TABLE " & strTable
cmd.CommandText = strSQL
cmd.Execute
End If
On Error GoTo 0
' create new table
strSQL = "CREATE TABLE " & strTable & _
"(SchDate DATETIME, SchTime DATETIME" & _
"CONSTRAINT PRIMARY KEY (SchDate))"
cmd.CommandText = strSQL
cmd.Execute
' fill table with dates of selected days of week
For dtmDate = dtmStart To dtmEnd
For Each varDay In varDays()
If Weekday(dtmDate) = varDay Or varDay = 0 Then
cmd.CommandText = strSQL1
strSQL1 = "INSERT INTO " & strTable & "(SchDate) " & _
"VALUES(#" & Format(dtmDate, " mm/dd/yyyy ") & "#)"
For dtmTime = dtmDate + dtmDayStart To dtmDate + _
dtmDayEnd Step intMinuteInterval / 1440
cmd.CommandText = strSql2
strSql2 = "INSERT INTO " & strTable & "(SchTime) " & _
"VALUES(#" & Format(dtmTime, "hh:nn:ss") & "#)"
cmd.CommandText = strSQL1 & strSql2
cmd.Execute
Next dtmTime
End If
Next varDay
Next dtmDate
Set cmd = Nothing
End Function
where is the error?
Option Compare Database
Public Function MakeSchedule(strTable As String, _
dtmStart As Date, _
dtmEnd As Date, _
dtmDayStart As Date, _
dtmDayEnd As Date, _
intMinuteInterval As Integer, _
ParamArray varDays() As Variant)
' Accepts: Name of schedule table to be created: String.
' Start date for calendar: DateTime.
' End date for calendar: DateTime.
' Time when first 'time-slot' starts each day: DateTime
' Time when last 'time-slot' starts each day: DateTime
' Length of each 'time-slot' in schedule in minutes: Integer
' Days of week to be included in calendar
' as value list, e,g 2,3,4,5,6 for Mon-Fri
' (use 0 to include all days of week)
Dim cmd As ADODB.Command
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim strSQL As String
Dim dtmDate As Date
Dim dtmTime As Variant
Dim varDay As Variant
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
Set cat = New Catalog
cat.ActiveConnection = CurrentProject.Connection
' does table exist? If so delete it
On Error Resume Next
Set tbl = cat(strTable)
If Err = 0 Then
strSQL = "DROP TABLE " & strTable
cmd.CommandText = strSQL
cmd.Execute
End If
On Error GoTo 0
' create new table
strSQL = "CREATE TABLE " & strTable & _
"(SchDate DATETIME, SchTime DATETIME" & _
"CONSTRAINT PRIMARY KEY (SchDate))"
cmd.CommandText = strSQL
cmd.Execute
' fill table with dates of selected days of week
For dtmDate = dtmStart To dtmEnd
For Each varDay In varDays()
If Weekday(dtmDate) = varDay Or varDay = 0 Then
cmd.CommandText = strSQL1
strSQL1 = "INSERT INTO " & strTable & "(SchDate) " & _
"VALUES(#" & Format(dtmDate, " mm/dd/yyyy ") & "#)"
For dtmTime = dtmDate + dtmDayStart To dtmDate + _
dtmDayEnd Step intMinuteInterval / 1440
cmd.CommandText = strSql2
strSql2 = "INSERT INTO " & strTable & "(SchTime) " & _
"VALUES(#" & Format(dtmTime, "hh:nn:ss") & "#)"
cmd.CommandText = strSQL1 & strSql2
cmd.Execute
Next dtmTime
End If
Next varDay
Next dtmDate
Set cmd = Nothing
End Function