Solved

MS Access VBA Help with date input safety catch

Posted on 2013-01-10
2
250 Views
Last Modified: 2013-01-10
Here is a piece of code that allows the user to input start and end dates to obtain a report.

Problem:
If the user inputs an end date before the start date, the program goes into a tail spin.
I.E.
Start date 1-10-13
End date   1-12-12

How can I add a safety net to my code - start date must be older than end date?

Code:

Dim strSQL As String
Dim dtmNextDate As Date

If Me.cboEmp.ListIndex = -1 Then
    MsgBox " Please select employee"
    Me.cboEmp.SetFocus
    Exit Sub
End If

If Me.cboReason.ListIndex = -1 Then
    MsgBox " Please select reason"
    Me.cboReason.SetFocus
    Exit Sub
End If

If Me.txtEnd & "" = "" Then
   MsgBox " Please select start date"
   Me.txtStart.SetFocus
   Exit Sub

End If

If Me.txtStart & "" = "" Then
   MsgBox " Please select end date"
   Me.txtEnd.SetFocus
   Exit Sub

End If

dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"

End Sub

Open in new window

0
Comment
Question by:DJPr0
2 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38762747
Try this -  I've added the check for startdate < enddate following your other validation checks:

Dim strSQL As String
Dim dtmNextDate As Date

If Me.cboEmp.ListIndex = -1 Then
    MsgBox " Please select employee"
    Me.cboEmp.SetFocus
    Exit Sub
End If

If Me.cboReason.ListIndex = -1 Then
    MsgBox " Please select reason"
    Me.cboReason.SetFocus
    Exit Sub
End If

If Me.txtEnd & "" = "" Then
   MsgBox " Please select start date"
   Me.txtStart.SetFocus
   Exit Sub

End If

If Me.txtStart & "" = "" Then
   MsgBox " Please select end date"
   Me.txtEnd.SetFocus
   Exit Sub

End If

If cdate(Me.txtStart) > cdate(Me.txtEnd) hen
   MsgBox " Please ensure start dtae is less than end date"
   Me.txtEnd.SetFocus
   Exit Sub

End If



dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"

End Sub

Open in new window

0
 

Author Closing Comment

by:DJPr0
ID: 38763106
Thanks mbizup!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now