【问题标题】:Creating a Calendar in Access 2010在 Access 2010 中创建日历
【发布时间】:2014-06-30 17:52:09
【问题描述】:

我一直在拼命寻找一种在 Access 中制作日历的方法。我知道这是可以做到的,因为我已经看到了很好的例子,但我不知道怎么做。 (另外,我的 VB 知识很少。)

基本上,我希望日历显示一个程序(我们称之为胶囊)被签出以及何时返回的日期范围。

  • DateReserve - 胶囊的预订日期
  • DateReturn - 胶囊需要归还的日期。

例如,如果胶囊 A 于 2014 年 6 月 1 日预订,并将于 2014 年 6 月 14 日返回,我希望日历直观地显示胶囊 A 在此时间段内不可用。这样一来,我们就不会意外重复预订胶囊。

通过我的许多谷歌搜索,我确实找到了 VB 代码,它可以调出一个非常漂亮的日历。我只是无法正确地获取代码以直观地显示胶囊将不可用的日期。 以下是我无法正常工作的代码部分之一:

Private Sub OpenContinuousForm(ctlName As String)
Dim ctlValue As Integer
Dim DaysOfMonth As Long
Dim DateReturn As Date
Dim DateShipOut As Date
Dim DateRangeForProgram As String

DateRangeForProgram = (DateDiff("n", [DateReturn], [DateShipOut]))
On Error GoTo ErrorHandler
ctlValue = Me.Controls(ctlName).Tag
DaysOfMonth = MyArray(ctlValue - 1, 0)

DoCmd.OpenForm "frmCapsulesSchedule", acNormal, , [DateRangeForProgram] = DaysOfMonth

ExitSub:
    Exit Sub
ErrorHandler:
    MsgBox "DATE SHIP OUT FAILED.", , "Error!!!"
    Resume ExitSub

End Sub

如果您需要我提供更多信息,请告诉我。

【问题讨论】:

    标签: ms-access calendar vba ms-access-2010


    【解决方案1】:

    这是我用于日历的代码;任何你看到“教师”、“学校”或“胶囊”的地方都是你放置自己信息的地方:

    Option Compare Database
    Option Explicit
    
    Private intYear As Integer
    Private intMonth As Integer
    Private lngFirstDayOfMonth As Long
    Private intLastDayOfLastMonth As Integer
    Private intFirstWeekday As Integer
    Private intDaysInMonth As Integer
    Private strFormReference As String
    Private MyArray() As Variant
    
    Private Sub cboMonth_Click()
    On Error GoTo Errorhandler
    Call Main
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    Private Sub cboYear_AfterUpdate()
    On Error GoTo Errorhandler
    Call Main
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    
    Private Sub Form_Load()
    On Error GoTo Errorhandler
    'Set the month and date to this current month and date
    With Me
        .cboMonth = Month(Date)
        .cboYear = Year(Date)
    End With
    
    Call Main
    
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    
    Public Sub InitVariables()
    On Error GoTo Errorhandler
    intYear = Me.cboYear
    intMonth = Me.cboMonth
    lngFirstDayOfMonth = CLng(DateSerial(intYear, intMonth, 1))
    intFirstWeekday = getFirstWeekday(lngFirstDayOfMonth)
    intDaysInMonth = getDaysInMonth(lngFirstDayOfMonth)
    'This is where you add the reference for the form
    'It is used in case we wish to add the module to a subform
    
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    Public Sub Main()
    On Error GoTo Errorhandler
    Call InitVariables
    Call InitArray
    Call LoadArray
    Call PrintArray
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    
    Public Sub InitArray()
    'First column will add all dates of the array
    'Second column will add visible property
    'Third column will hold the string variable
    
    Dim i As Integer
    
    On Error GoTo Errorhandler
    
    ReDim MyArray(0 To 41, 0 To 3)
    
    For i = 0 To 41
    
        MyArray(i, 0) = lngFirstDayOfMonth + 1 - intFirstWeekday + i
        If Month(MyArray(i, 0)) = intMonth Then
            MyArray(i, 1) = True
            'This works out the days of the month
            MyArray(i, 2) = i + 2 - intFirstWeekday & vbNewLine
        Else
            MyArray(i, 1) = False
        End If
       
    Next i
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    
    End Sub
    
    Public Sub LoadArray()
    'This sub loads an array with the relevant variables from a query
    Dim db As Database
    Dim rs As Recordset
    Dim rsFiltered As Recordset
    Dim strQuery As String
    Dim i As Integer
    
    On Error GoTo ErrorHandler1
    
    strQuery = "Select * FROM [qryDatesYearsCapsules2]"
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strQuery)
    
    
    
    With rs
    
        If Not rs.BOF And Not rs.EOF Then
        'Ensures the recordset contains records
      
      On Error GoTo ErrorHandler2
    
            For i = 0 To UBound(MyArray)
            'Will loop through the array and use dates to filter down the query
            'It firsts checks that the second column has true for its visible property
                If MyArray(i, 1) = True Then
                    .Filter = "[NewDate]=" & MyArray(i, 0)
                    'To filter you must open a secondary recordset and
                    'Use that as the basis for a query
                    'This makes sense as you are building a query on a query
    
                    Set rsFiltered = .OpenRecordset
                    If Not rsFiltered.BOF And Not rsFiltered.EOF Then
                        'If the recordset is not empty then you are able
                        'to extract the text from the values provided
                        Do While Not rsFiltered.EOF = True
    
                            MyArray(i, 2) = MyArray(i, 2) & rsFiltered!CapsuleSet
               '             MyArray(i, 2) = MyArray(i, 2) & vbNewLine & rsFiltered!Teacher
                            MyArray(i, 2) = MyArray(i, 2) & vbNewLine & rsFiltered!School
               '             MyArray(i, 2) = MyArray(i, 2) & " - " & rsFiltered!NewDate
                            MyArray(i, 2) = MyArray(i, 2) & vbNewLine & vbNewLine
                            
                        rsFiltered.MoveNext
                        Loop
                    End If
                End If
         
            Next i
        
    End If
        .Close
    End With
    
    ExitSub:
        Set db = Nothing
        Set rs = Nothing
        Exit Sub
    ErrorHandler1:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
        
    ErrorHandler2:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
        
    End Sub
    
    Public Sub PrintArray()
    
    Dim strTextBox As String
    Dim i As Integer
    
    On Error GoTo Errorhandler
    
    For i = 0 To 41
        strTextBox = "txt" & CStr(i + 1)
        With Me
            Controls(strTextBox) = ""
            Controls(strTextBox).tag = i + 1
            Controls(strTextBox) = MyArray(i, 2)
        'Debug.Print strTextBox
        'MyArray(i, 2)
        End With
    Next i
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    End Sub
    
    Private Sub OpenContinuousForm(ctlName As String)
    Dim ctlValue As Integer
    Dim DayOfMonth As Long
    
    On Error GoTo Errorhandler
    ctlValue = Me.Controls(ctlName).tag
    DayOfMonth = MyArray(ctlValue - 1, 0)
    DoCmd.OpenForm "frmClassDataEntry", acNormal, , "[NewDate]=" & DayOfMonth, , acDialog
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitSub
    
    End Sub
    
    Private Sub txt1_Click()
    On Error GoTo Errorhandler
    If Me.ActiveControl.Text <> "" Then
        Call OpenContinuousForm(Me.ActiveControl.Name)
    End If
    ExitSub:
        Exit Sub
    Errorhandler:
        MsgBox "There has been an error. Please reload form."
        Resume ExitSub
    End Sub
    

    '重复txt1_Click()的代码到txt42_Click()

    Private Sub Format()
    
      Dim ctl As Control
      Dim lngBackColor As Long
    
      For Each ctl In Me.Detail.Controls
        If DCount("*", "lstCapsules", "[Capsule]='" & ctl.Value & "'") = 0 Then
    
           lngBackColor = 16777215
        Else
           lngBackColor = DLookup("Background", "lstCapsules", "[Capsule]='" & ctl.Value & "'")
        End If
    
        ctl.BackColor = lngBackColor
        
        Next ctl
        
      Set ctl = Nothing
      
    End Sub
    

    我还有一个名为 modFunctions 的模块:

        Option Compare Database
    Option Explicit
    
    Public Function getFirstWeekday(lngFirstDayOfMonth As Long) As Integer
    
    On Error GoTo Errorhandler
    getFirstWeekday = -1
    getFirstWeekday = Weekday(lngFirstDayOfMonth, vbMonday)
    
    ExitFunction:
        Exit Function
    Errorhandler:
        getFirstWeekday = 0
        MsgBox "There has been an error. Please reload the form.", , "Error"
        Resume ExitFunction
    End Function
    
    Public Function getDaysInMonth(lngFirstDayOfMonth As Long) As Integer
    
    On Error GoTo Errorhandler
    getDaysInMonth = -1
    getDaysInMonth = DateDiff("d", lngFirstDayOfMonth, DateAdd("m", 1, lngFirstDayOfMonth))
    ExitFunction:
        Exit Function
    Errorhandler:
        getDaysInMonth = 0
        MsgBox "Something is wrong with the DATES!.", , "Date Error"
        Resume ExitFunction
    End Function
    

    【讨论】:

    • 很高兴您有这样的工作示例。您能否将其缩短为有效重复位和剩余唯一代码的单个示例? OP表示他们已经找到了一些很好的例子。看来他们正在寻找更多的指导。
    • 我是 OP :) 但是是的,我可以缩短它。我发现需要更多代码来使日历正常工作,但我不确定如何发布所有内容......让我看看我能做什么。
    【解决方案2】:

    Access All In One 看到了一个非常有用的 youtube 视频。这是示例中使用的数据库的link

    【讨论】:

    • 是的,这是我一直使用的日历。我非常感谢这个人好心地分享他的数据。我遇到问题的两个部分是 Public Sub LoadArray() 和 Private Sub OpenContinuousForm(ctlName As String)。我不知道如何进行一系列日期
    【解决方案3】:

    您在 openform 命令的Where 条件中的语法不正确。

    如果你用来过滤表单记录源的字段是[DateRangeForProgram],它应该是"[DateRangeForProgram]=" &amp; DaysOfMonth

    此外,如果您尝试将表单打开多天,您可能应该使用 Between 运算符。 datediff 函数的第一个参数指定一个时间间隔,您的时间间隔是分钟。

    您应该发布其余代码,以便整个场景清晰。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多