rob p said:
I need to get two dates (for a range) and store in a table. I want to do
this so I don't have to reenter each time I need the range. The date range
would be used for querys and reports. I want to enter the dates from a form.
Last, I only want two dates stored in the table. Either two of the same or a
range. These would be updated each time I opened and entered into the form.
How do I keep the table from growing past 2 entries?
thanks.
Create a table with two fields, for example called tblDefaults with the
date fields StartDateDef and EndDateDef. Create one record in this table and
set each field to nominal values.
On your ReportOptions form create two unbound text boxes , for example
called , StartDate and EndDate. Set their formats to some kind of date, and
their default values to GetStartDateDef() and GetEndDateDef() respectively.
You could also set their "After Update" events to the code I've supplied
below.
Put the four functions supplied below in the form's code section.
Good luck
Jack
Public Function GetEndDateDef() As Date
' sets the default End date in the last value table
Dim dbs As ADODB.Connection, rst As ADODB.Recordset
Dim TheEDate As Date
' Return reference to current database.
Set dbs = CurrentProject.Connection ' open the databass
Set rst = New Recordset ' open a recordset
' open the the table
rst.Open "tblDefaults", dbs, adOpenStatic, adLockReadOnly
' get the end date
TheEDate = rst!EndDateDef
rst.Close
dbs.Close
Set dbs = Nothing
Set rst = Nothing
GetEndDateDef = TheEDate
End Function
Public Function GetStartDateDef() As Date
' Gets the default start date in the last value table
Dim dbs As ADODB.Connection, rst As ADODB.Recordset
Dim TheSDate As Date
' Return reference to current database.
Set dbs = CurrentProject.Connection ' open the databass
Set rst = New Recordset ' open a recordGet
' open the the table
rst.Open "tblDefaults", dbs, adOpenStatic, adLockReadOnly
' get the start date
TheSDate = rst!StartDateDef
rst.Close
dbs.Close
Set dbs = Nothing
Set rst = Nothing
GetStartDateDef= TheSDate
End Function
Public Function SetDefStartDate(TheSDate As Date)
' sets the default start date in the last value table
Dim dbs As ADODB.Connection, rst As ADODB.Recordset
' Return reference to current database.
Set dbs = CurrentProject.Connection ' open the databass
Set rst = New Recordset ' open a recordset
' open the table
rst.Open "tblDefaults", dbs, adOpenDynamic, adLockOptimistic
' save the start date field
rst!StartDateDef = TheSDate
rst.Update
rst.Close
dbs.Close
Set dbs = Nothing
Set rst = Nothing
End Function
Public Function SetDefEndDate(TheEDate As Date)
' sets the default end date in the defaults table
Dim dbs As ADODB.Connection, rst As ADODB.Recordset
' Return reference to current database.
Set dbs = CurrentProject.Connection ' open the databass
Set rst = New Recordset ' open a recordset
' open the table
rst.Open "tblDefaults", dbs, adOpenDynamic, adLockOptimistic
' save the end date field
rst!EndDateDef = TheEDate
rst.Update
rst.Close
dbs.Close
Set dbs = Nothing
Set rst = Nothing
End Function
Private Sub StartDate_AfterUpdate()
' stores the latest start date that the user enters
If IsDate(Me.StartDate) Then
SetDefStartDate Me.StartDate
End If
End Sub
Private Sub EndDate_AfterUpdate()
' stores the latest end date that the user enters
If IsDate(Me.EndDate) Then
SetDefEndDate Me.EndDate
End If
End Sub