How be sure user enters the 1st date of a month, quarter, or year in a date field on a form

I have a date field on a form named txtStartDate.  I want to be sure the user enters the 1st date of a month, quarter, or year in the field.

I also have three checkboxes on the form...  chkbxMontlhly, chkbxQuarterly, and chkbxYearly.  Only one can be selected.

So in other words...

If they have selected chkbxMonthly they have to enter either 1/1/xxxx, 2/1/xxxx, 3/1/xxxx, 4/1/xxxx, 5/1/xxxx, 6/1/xxxx, 7/1/xxxx, 8/1/xxxx, 9/1/xxxx, 10/1/xxxx, 11/1/xxxx, or 12/1/xxxx.

If they have selected chkbxQuarterly they have to enter either 1/1/xxxx, 4/1/xxxx, 7/1/xxxx, or 10/1/xxxx.

If they have selected chkbxYearly they can only enter 1/1/xxxx.

How can I do this?
SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Without trying to guess what all your code looks like, and what triggers you have, etc, here is a start at some editing logic for the date text input.  It could and should be even more robust, checking for 2 slashes, etc, but I didn't have a lot of time to spend.  But wanted to share some based ideas.

This just displays some info in the debug immediate message pane, you can decide if you want error messages to the screen, etc.  Maybe roll this into a function that validates the field, again, really depends on how you are writing your code.

I'd probably make the monthly, quarterly and yearly selection a radio set rather than checkboxes I think, to keep it simpler.

Sub Test()

    Const cMonthly = 1
    Const cQuarterly = 2
    Const cYearly = 3
    
    ' Set a variable based on yor checkboxes
    intMode = 3
    
    ' Get date string from form text field, break apart
    strDate = "2/1/2017"
    arrDate = Split(strDate, "/")
    intMonth = arrDate(0)
    intDay = arrDate(1)
    intYear = arrDate(2)
    
    ' Edit day, must be a 1 for all cases
    If intDay = 1 Then
        Debug.Print "Day good"
    Else
        Debug.Print "Day not 1"
    End If
    
    ' Check month,varies by mode
    Select Case intMode
        Case cMonthly
            Select Case intMonth
                Case 1 To 12
                    Debug.Print "Month good"
                Case Else
                    Debug.Print "Month not 1 - 12"
            End Select
        Case cQuarterly
            Select Case intMonth
                Case 1, 4, 7, 10
                    Debug.Print "Month good"
                Case Else
                    Debug.Print "Month not 1, 4, 7 or 10"
            End Select
        Case cYearly
            Select Case intMonth
                Case 1
                    Debug.Print "Month good"
                Case Else
                    Debug.Print "Month not 1"
            End Select
    End Select
    
    ' Check Year
    If intYear < 1700 Or intYear > 3000 Then
        Debug.Print "Year bad"
    Else
        Debug.Print "Year good"
    End If
End Sub

Open in new window


»bp
0
PatHartmanCommented:
Dates are not strings.  There are functions that let you easily pick a date apart.  Month(), Day(), Year() for starters.

Instead of using three checkboxes, the code will be simpler if you use one Option Group.

Public Func GoodDate(DateIn as Variant, DateType as Variant) As Boolean
If IsDate(DateIn) Then
Else
    GoodDate = False
    Exit Func
End If

if Day(DateIn) = 1 Then
Else
    GoodDate = False
    Exit Function
End If

Select Case DateType
    Case 1 'year
        If Month(DateIn) = 1  Then
        Else
            GoodDate = False
            Exit Function
        End If
    Case 2 'quarter
        Select Case Month(DateIn)
            Case 1, 4, 7, 10
            Case Else
                GoodDate = False
                Exit Function
        End Select
End Select
GoodDate = True
End Func

Open in new window

0
Gustav BrockCIOCommented:
This is one of those cases where you could serve the users rather than bother them with tasks that "the system should be able to figure this out".
So, after entering the date, set it to the start of the selected period:

Option Compare Database
Option Explicit

Private Sub SetFirstDate()

    Dim PickDate    As Date
    
    PickDate = Me!FirstDate.Value
    If Not IsNull(PickDate) Then
        Select Case Me!Period.Value
            Case 1  ' Month
                PickDate = DateSerial(Year(PickDate), Month(PickDate), 1)
            Case 2  ' Quarter
                PickDate = DateSerial(Year(PickDate), DatePart("q", PickDate) * 3 - 2, 1)
            Case 3  ' Year
                PickDate = DateSerial(Year(PickDate), 1, 1)
        End Select
        If DateDiff("d", Me!FirstDate.Value, PickDate) <> 0 Then
            Me!FirstDate.Value = PickDate
        End If
    End If
    
End Sub

Private Sub FirstDate_AfterUpdate()

    SetFirstDate
    
End Sub

Private Sub Period_AfterUpdate()

    SetFirstDate

End Sub

Open in new window

See the attached demo.

/gustav
DbPickDate.accdb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
To all...  I'm still working on this.  Back in a day or two.  Sorry for the delay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.