If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > Date Error Handling

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-09, 09:26
tlugdon tlugdon is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
Date Error Handling

Hey All,

I created a program that sends data to an Access table. I want to add in some sort of date error handling so if they put in a future date, or a date that it is in 2008 it will pop-up an error. Im not sure how to work the code in VB. Thanks


Code:
Private Sub CommandButton12_Click()
Set objAccess = CreateObject("Access.Application")
Set accApp = CreateObject("Access.Application")

' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim db As Database, rs As Recordset, r As Long
    Set db = OpenDatabase("S:\Senior Operations Supervisors\Subscriber\Subscriber Survival\attendance.mdb")
    ' open the database
    Set rs = db.OpenRecordset("Attendance", dbOpenTable)
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("Lawson") = Range("C42").Value
            .Fields("Date") = Range("D42").Value
            .Fields("Name") = Range("E42").Value
            .Fields("Occurence") = Range("F42").Value
            .Fields("Comments") = Range("C45").Value
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
Reply With Quote
  #2 (permalink)  
Old 07-06-09, 10:21
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
So you just want to make sure the date is in the current year? Use the Year function on the date and compare it with the current year, if they are different, raise an error.

Ax
Reply With Quote
  #3 (permalink)  
Old 07-06-09, 16:29
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
And, DATE is a horrible name for your date field. DATE is a reserved word in Access, VB, as well as every database known to man, along with most programming languages...

Try to use a more descriptive column name. AttendanceDate. SubscriptionDate. Etc.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #4 (permalink)  
Old 07-06-09, 16:33
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Also, for date comparisons, you can compare dates to see if the date entered is greater than the current date.

Code:
If CDate(Date1) > CDate(Date2) then
  ' Date1 is greater than date 2.  The CDate function used in case the entered value is a string
End If
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On