Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / New Users / February 2006

Tip: Looking for answers? Try searching our database.

Help with dates -

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wobbles - 16 Feb 2006 17:14 GMT
I re write this question as I think i failed to provide enought information
for my previous helpers to help me completely (that and the fact it's done my
head in) Thank you by the Way those guys. o.k Here goes .
I have a table Called "tEvent" which stores the start date and an end date
of a booking plus whom has the booking, the fields are named [Start] , [End]
& [LocationId] - I have am trying to check dates as i select them from a
calender routine filling in a field on my form , i would like to check this
date to ensure it is not already booked on any date in "tEvent"  between
existing "Start" and "Finish" dates then either provide an error message or
proceed to the the bookings end date and again checking its validity. i'm a
newbie so please give it to me easy and simple.the form i'm using is called
[Booking Application] and it contains two fields with calender buttons
returning the chosen dates. HELP !!!! PLEASE !!!!
Ross - 17 Feb 2006 10:20 GMT
Hi Wobbles

I am assuming that your form has the following names for the controls:

[Start] calStart
[End] calEnd
User txtUser
LocationID cboLocationID (this is a combo box - that looks up the locations
from a separate table)

If you select the form in design view & open the property window & select
the Event tab, you should see an event called "Before Update".  If you then
click on the ellipsis (...) & choose code builder the VBA Editor will open.

You should see two lines of code like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

End Sub

Copy & paste the following code between the xxxxxxxxxxxxxxxxxxxxx's

xxxxxxxxxxxxxxxxxxxxxxxxxxxx

   Dim cnn     As ADODB.Connection
   Dim rs      As ADODB.Recordset
   Dim strSQL  As String
   
   'create new instances of the connection to the DB and the recordset for
the table tEvent
   Set cnn = New ADODB.Connection
   Set cnn = CurrentProject.Connection
   Set rs = New ADODB.Recordset
   
   'string expression to select all fields from the table tEvent but
limited to the location specified _
   in the Location combo box and ordered by the Start Date
   strSQL = "SELECT * FROM tEvent WHERE [LocationID] = " & Me.cboLocationID
& " ORDER BY [Start] ASC"
   
   With rs
   
       'open the recordset based upon the string expression above
       .ActiveConnection = cnn
       .CursorLocation = adUseServer
       .CursorType = adOpenKeyset
       .LockType = adLockReadOnly
       
       .Open strSQL, , , , adCmdText
       
       'start st the first record
       .MoveFirst
       
       'loop through all the records returned, and check to see if there
are any overlaps in dates _
       for the specified location
       
       'if there is an overlap, then generate a msg for the user and cancel
the update
       Do
       
           'there are three possibilities for an overlap
         
           '(1)    the new start date is before or equal to the
pre-existing end date _
                   and is also after or equal to the pre-existing Start Date
               
           '(2)    the new end date is between (or equals) the pre-existing
end date & start date
           
           '(3)    the new start date is before or equals the pre-existing
start date and the _
                   end date is is after or the same as the pre-existing end
date          
           If Me.calStart.Value <= ![End] And Me.calStart.Value >= ![Start]
Then
         
               MsgBox "sorry, this booking overlaps with a pre-existing
booking.", vbOKOnly + vbExclamation, "Overlap"
               Cancel = True
         
           End If
         
           If Me.calEnd.Value >= ![Start] And Me.calEnd.Value <= ![End] Then
           
               MsgBox "sorry, this booking overlaps with a pre-existing
booking.", vbOKOnly + vbExclamation, "Overlap"
               Cancel = True
               
           End If
           
           If Me.calStart.Value <= ![Start] And Me.calEnd.Value >= ![End]
Then
           
               MsgBox "sorry, this booking overlaps with a pre-existing
booking.", vbOKOnly + vbExclamation, "Overlap"
               Cancel = True
               
           End If
           
           'go to the next record & continue looping unless have reached EOF
           .MoveNext
       
       Loop Until .EOF
   
   End With
   
   'having done all teh checks, just clean up
   
   If rs.State <> adStateClosed Then
       rs.Close
   End If
   
   If cnn.State <> adStateClosed Then
       cnn.Close
   End If
   
   Set rs = Nothing
   Set cnn = Nothing

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You may have to amend the control names in the code to suit the names that
you have used for the various controls.

Basically, what this code does is before a new record is saved, (the Before
Update event happens before a record is saved - so it can be cancelled
easily), it does a lookup in the underlying table for the selected Location
(cboLocationID) and then runs a series of checks against each of the records
that have been returned (these are previous records that have already been
saved - not the new one).

If there is an overlap, thena message box is generated advising the user and
the update is cancelled (Cancel = True)

That Do ... Loop is just there to loop through all the records that have
been returned.

The final bit down the bottom of the code is just there to close the
recordset & the connection & release resources.

I know I went into a lot of detail, but you wanted it step by step.  If you
need any extra help, just yell.

Hope this helps.

Kind regards

Ross Petersen

> I re write this question as I think i failed to provide enought information
> for my previous helpers to help me completely (that and the fact it's done my
[quoted text clipped - 9 lines]
> [Booking Application] and it contains two fields with calender buttons
> returning the chosen dates. HELP !!!! PLEASE !!!!
tina - 17 Feb 2006 19:06 GMT
your logic to identify an overlap makes sense, Ross; suggest you tighten up
the Loop code to make it more efficient. once a single overlap is
identified, there's no need to continue checking those particular dates by
running subsequent If statements, or checking additional records in the
recordset. how about modifying the code inside your Do...Loop a bit, as

   If Me.calStart.Value <= ![End] And _
       Me.calStart.Value >= ![Start] Then
       MsgBox "sorry, this booking overlaps with a pre-existing " _
           & "booking.", vbOKOnly + vbExclamation, "Overlap"
       Cancel = True
       Exit Do
   ElseIf Me.calEnd.Value >= ![Start] And _
       Me.calEnd.Value <= ![End] Then
       MsgBox "sorry, this booking overlaps with a pre-existing " _
           & "booking.", vbOKOnly + vbExclamation, "Overlap"
       Cancel = True
       Exit Do
   ElseIf Me.calStart.Value <= ![Start] And _
       Me.calEnd.Value >= ![End] Then
       MsgBox "sorry, this booking overlaps with a pre-existing " _
           & "booking.", vbOKOnly + vbExclamation, "Overlap"
       Cancel = True
       Exit Do
   End If
   .MoveNext

hth

> Hi Wobbles
>
[quoted text clipped - 159 lines]
> > [Booking Application] and it contains two fields with calender buttons
> > returning the chosen dates. HELP !!!! PLEASE !!!!
Ross - 17 Feb 2006 22:55 GMT
Thanks Tina

Yes you are quite right!  I just did that code very quickly as I was in a
bit of a hurry at the time.  I was taking my wife & her mum out soon after
that.  

Wobbles, if you have a look at Tinas modifications - each If/Then/End If
block now has an Exit Do statement.  What that does is that if there is an
overlap identified with a pre-existing record then the update is cancelled,
so there is no need to go through all the other pre-existing records.

If you have quite a few records for a given location, then that will save
time in running this code & also there will not be the potential to have more
than one Message Box generated if there is more than one overlap.

Thanks Tina, appreciate your help.

Kind regards

Ross Petersen

> your logic to identify an overlap makes sense, Ross; suggest you tighten up
> the Loop code to make it more efficient. once a single overlap is
[quoted text clipped - 214 lines]
> > > [Booking Application] and it contains two fields with calender buttons
> > > returning the chosen dates. HELP !!!! PLEASE !!!!
tina - 18 Feb 2006 01:56 GMT
no problem, Ross, i've had folks help me out the same way plenty of times.
:)

> Thanks Tina
>
[quoted text clipped - 235 lines]
> > > > [Booking Application] and it contains two fields with calender buttons
> > > > returning the chosen dates. HELP !!!! PLEASE !!!!
Wobbles - 19 Feb 2006 22:41 GMT
Thanks Ross and Tina, I am away the fourthcoming week so i will have to wait
until the end of the month to put you wonderful solutions into action , i
have every confidence they are going to solve my problems, Thanks once again !

> no problem, Ross, i've had folks help me out the same way plenty of times.
> :)
[quoted text clipped - 267 lines]
> buttons
> > > > > returning the chosen dates. HELP !!!! PLEASE !!!!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.