How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes.

It’s a reusable form that allows you to pick a time from an Access form.

There are probably different ways to do this but here is the way I would do it.

On the form that has the time fields, use the click event of the textbox to open a form time picker as a dialog box. Here is a screen shot:

Here is the code:



Private Sub StartTime_Click()
    DoCmd.OpenForm "frmTimeSelector", acNormal, , , , acDialog, "Start Time"
End Sub

Private Sub EndTime_Click()
    DoCmd.OpenForm "frmTimeSelector", acNormal, , , , acDialog, "End Time"
End Sub

Here is the code for the Access Form Time Picker form (frmTimeSelector)


Option Compare Database

Dim m_WhichTime As String

Private Sub btnCancel_Click()
    DoCmd.Close
End Sub

Private Sub btnOK_Click()
    Dim strHour As String, strMinute As String, strTime As String
    Dim dteTime As Date
    
    strTime = Me.cboHour & ":" & Me.cboMinute & " " & Me.cboTimeOfDay
    dteTime = strTime
    
    Select Case m_WhichTime
        Case "StartTime"
            Forms("frmCases").Controls("StartTime") = dteTime
        Case "EndTime"
            Forms("frmCases").Controls("EndTime") = dteTime
    End Select
       
    Me.Dirty = False
    
    
    Me.Visible = False
    
    DoCmd.Close acForm, Me.Name
    
    
End Sub


Private Sub Form_Open(Cancel As Integer)
    If Len(Me.OpenArgs) Then
        
        'set the caption for the form based on how to form was opened
        Me.Caption = "Select the " & Me.OpenArgs & " for this case"
        
        
        'set the modular variable based on how to form was opened
        Select Case Me.OpenArgs
            Case "Start Time"
                m_WhichTime = "StartTime"
            Case "End Time"
                m_WhichTime = "EndTime"
        End Select
        
    End If
End Sub


I am setting a modular variable called “m_WhichTime” so I know what was clicked when the form opened until it’s closed and time to update the underlying form StartDate or EndDate.

This is the result:

(Video coming soon)




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How to pick a file to load In VBA

Picking a file to load in your Microsoft App is a very important skill to know. In this blog post you will see how to do it. First you need to set a reference to the MS office object library From VBE editor –> select Tools > MS office object library (click check mark) Sub […]

What is the purpose of the Me keyword in Access VBA?

What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]

How do I run VBA code when form opens

How do I run VBA code when form opens? There are probably several ways people do it, and some may say “He’s not doing it right. It’s done this way…” Good for you. This is the way I do it now, and it has worked well for me. 1. Find the form you want to […]

Simple Custom Progress Bar in Microsoft Access Forms Example

So I had a question come in regarding showing the progress of a task. Access has an ActiveX control that you can use called the ProgressBar and you can follow the guide in the image above to add it to your form. On you long running process you can show the progress bar so the […]

Tags:
Previous Post

How To Escape Apostrophe In SQL Update Query

Next Post

How To Parse A Flat File In Excel VBA

Leave a Reply

Your email address will not be published. Required fields are marked *