Solved

MS Access VBA Help with date input safety catch

Posted on 2013-01-10
2
280 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

830 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