Date Problems (1 Viewer)

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Hi all,

I'm trying to prevent records being entered outside a time period & I'm struggling to work out the best method of doing so

I've been trying
Code:
If Me.txtStartDate <= #3/13/2010# Or Me.txtStartDate >= #3/12/2011# Then
MsgBox "This date may be incorrect please check your records & try again"
Me.txtStartDate = ""
Me.txtStartedHidden = ""
End if


This does seem to work but I need to be more flixable & allow 2 weeks either side of the two dates but not sure on the correct way of coding it.

I need something like

Code:
 If me.txtStartDate <= now() -14 or Me.txtStartDate >=Now() +14 then
MsgBox "This date may be incorrect please check your records & try again"
Me.txtStartDate = ""
Me.txtStartedHidden = ""
End if

Can someone please show me the correct way?

Thanks ;)
 

Brianwarnock

Retired
Local time
Today, 16:47
Joined
Jun 2, 2003
Messages
12,701
Doesn't your "something like" work?
Personally I would use Datadd instead of the + - and also Date() instead of Now() but that's personal preference as for as the concept.

Brian

I wonder if you need ( )
 

vbaInet

AWF VIP
Local time
Today, 16:47
Joined
Jan 22, 2010
Messages
26,374
I would be inclined to Format() the value of your textbox too.
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Hi,

The following code now works after formating the date field to short date
Code:
Private Sub txtStartDate_AfterUpdate()
If Me.txtStartDate <= Date - 14 Or Me.txtStartDate >= Date + 14 Then
MsgBox "This date may be incorrect please check your records & try again"
Me.txtStartDate = ""
Me.txtStartedHidden = ""
End If
End Sub
But I think that using the date add method you suggested would be better, if I can figure out what I'm doing wrong when i try this.

Code:
If Me.txtStartDate Between DateAdd("ww", -2, Date()) And DateAdd("ww", 2, Date())

I get a complile Error: "Expected then or goto" & can't work out where the problem is.

Anyone know where I'm going wrong?


Thanks
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Well the bit of code posted as an If but no Then

Brian

Yes, I tried to add "then" to the end of the line but it errors out :confused:

Must be something wrong with "Between" as that is highlighted in red

Code:
If Me.txtStartDate Between DateAdd("ww", -2, Date()) And DateAdd("ww", 2, Date())
 

Brianwarnock

Retired
Local time
Today, 16:47
Joined
Jun 2, 2003
Messages
12,701
Perhaps you cant use Between... And in VBA. Never have but thought you could,

Brian
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Perhaps you cant use Between... And in VBA. Never have but thought you could,

Brian

Mmm, I found that solution from another post somewhere & perhaps was based on a query.

Oh, well.

My other method seems to work & happy to use it if noone else says otherwise


Thanks Brian :)
 

DCrake

Remembered
Local time
Today, 16:47
Joined
Jun 8, 2005
Messages
8,632
You cannot use the following logic

Code:
If x Between y And z Then
You need

Code:
If X <= y Or x  >= z Then
Other than that


Code:
Select Case ActualDate
     Case < DateAdd("d",-14,Date())
         Too Early
     Case > DateAdd("d",14,Date())
         Too Late
     Case Else
         Within Range
End Select
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Code:
Select Case ActualDate
     Case < DateAdd("d",-14,Date())
         Too Early
     Case > DateAdd("d",14,Date())
         Too Late
     Case Else
         Within Range
End Select


Hi DCrake,

Where do you put your code so I can try it out?
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Code:
If X <= y Or x  >= z Then

That's the method I can only seem to get to work
Code:
If Me.txtStartDate <= Date - 7 Or Me.txtStartDate >= Date + 7 Then
MsgBox "This date may be incorrect please check your records & try again"
Me.txtStartDate = ""
Me.txtStartedHidden = ""

End If
 

DCrake

Remembered
Local time
Today, 16:47
Joined
Jun 8, 2005
Messages
8,632
Right I create a little function

Code:
Function TestDate(ActualDate As Date) As Integer

Select Case ActualDate
     Case Is < DateAdd("d", -14, Date)
        TestDate = 1
     Case Is > DateAdd("d", 14, Date)
         TestDate = 2
     Case Else
         TestDate = 0
End Select



End Function

Then on the AfterUpdate of the textbox that holds the date

Code:
If TestDate(Me.txtStartDate) > 0 Then
         MsgBox "This date may be incorrect please check your records & try again"
         Me.txtStartDate = ""
         Me.txtStartedHidden = ""
End If
 

Groundrush

Registered User.
Local time
Today, 16:47
Joined
Apr 14, 2002
Messages
1,376
Right I create a little function

Code:
Function TestDate(ActualDate As Date) As Integer

Select Case ActualDate
     Case Is < DateAdd("d", -14, Date)
        TestDate = 1
     Case Is > DateAdd("d", 14, Date)
         TestDate = 2
     Case Else
         TestDate = 0
End Select



End Function

Then on the AfterUpdate of the textbox that holds the date

Code:
If TestDate(Me.txtStartDate) > 0 Then
         MsgBox "This date may be incorrect please check your records & try again"
         Me.txtStartDate = ""
         Me.txtStartedHidden = ""
End If

Thank You, I understand now :)
 

Brianwarnock

Retired
Local time
Today, 16:47
Joined
Jun 2, 2003
Messages
12,701
Surely if you are going to the trouble of the Select Case then the message output should be as in the earlier example pointing the user as to too early or too late.
Can't it all be done in the afterupdate event?

Brian
 

Users who are viewing this thread

Top Bottom