Setting address defaults

J

JBruno

We are creating a new database. Our members have multiple addresses and we
would like to set the system so that it will default to the select address
based on dates. For example, the members have summer and winter addresses. I
would like to set the system to default to the summer address from May -
October and the winter address from November - April. I want to avoid having
to manually switch the address. Also, the dates will not be the same for each
member some stay during the summer and others leave at different times.
Thanks for your help!
 
J

John Nurick

Depending on your taste, either
* set up an Addresses table with a many-to-one relationship with the Members
table and a "season" field, or
* include two sets of address fields in the Members table.

If you only have two "seasons" to contend with, include date fields in the
Members table for StartWinter (default value 1 November) and StartSummer
(default Value 1 May). Then you can use a function like this

Function IsSummer(TheDate As Date, _
StartWinter As Date, StartSummer As Date) As Boolean
Dim dtStartWinter As Date
Dim dtStartSummer As Date

'Adjust fixed dates to current year
dtStartWinter = DateSerial(Year(D), Month(StartWinter), Day(StartWinter))
dtStartSummer = DateSerial(Year(D), Month(StartSummer), Day(StartSummer))

If D >= dtStartSummer And D < dtStartWinter Then
IsSummer = True
Else
IsSummer = False
End If
End Function

in a query to decide which address to use for each member each time you do a
mailing.

If you may have more than two seasons (e.g. if some members go north in the
summer, south in the winter and to Scotland in August) things get a bit more
complicated but not impossible.
 
K

Ken Sheridan

The logical design for this would be to have a table Members with a primary
key column MemberID and a table Addresses with a foreign key column MemberID
referencing the primary key of Members. I'd suggest that to cater for the
varying departure dates of members you have columns of integer number data
type, MonthFrom, DayFrom, MonthTo and DayTo, in the Addresses table so if a
member departs for their winter address on 23 October and returns on 6 April
say the values in these four columns would be 10, 23, 4, 5 for the row in the
table with their winter address and 4,6, 10, 22 for their summer address.
There is no reason of course why someone can't have more than two addresses
so long as the dates don't overlap.

To determine which address is the right one for the current date a simple
function which returns True or False for the dates in each row would be an
appropriate way to go. The following is adapted from some similar code of my
own to suit your scenario and should do the trick, I think. Just paste the
function into a standard module in your database:

Function IsCurrentAddress(intMonthFrom As Integer, _
intDayFrom As Integer, _
intMonthTo As Integer, _
intDayTo As Integer, _
Optional varCurrentDate As Variant) As Boolean

Dim dtmDateFrom As Date, dtmDateTo As Date
Dim intMonthDayFrom As Integer, intMonthDayTo As Integer
Dim intMonthDayCurrentDate As Integer
Dim intYearFrom As Integer, intYearTo As Integer

If IsMissing(varCurrentDate) Then varCurrentDate = VBA.Date

' get dates as integers in format mmdd
intMonthDayFrom = Format(intMonthFrom, "00") & _
Format(intDayFrom, "00")
intMonthDayTo = Format(intMonthTo, "00") & _
Format(intDayTo, "00")
intMonthDayCurrentDate = Format(varCurrentDate, "mmdd")

If intMonthDayFrom < intMonthDayTo Then
' date range starts and finishes in current year
intYearFrom = Year(varCurrentDate)
intYearTo = Year(varCurrentDate)
Else
' date range spans end of year
If intMonthDayCurrentDate <= intMonthDayTo Then
' date range starts in previous year and
' finishes in current year
intYearTo = Year(varCurrentDate)
intYearFrom = intYearTo - 1
Else
' date range starts in current year and
' finishes in next year
intYearFrom = Year(varCurrentDate)
intYearTo = intYearFrom + 1
End If
End If

dtmDateFrom = DateSerial(intYearFrom, intMonthFrom, intDayFrom)
dtmDateTo = DateSerial(intYearTo, intMonthTo, intDayTo)

IsCurrentAddress = (varCurrentDate >= dtmDateFrom And _
varCurrentDate <= dtmDateTo)

End Function

The function takes the four month/day values as its arguments, plus an
optional date value which defaults to the current date. So to return a set
of addresses for the current date you'd cal it in a query like so:

SELECT FirstName, LastName, Address
FROM Members INNER JOIN Addresses
ON Members.MemberID = Addresses.MemberID
WHERE IsCurrentAddress(MonthFrom, DayFrom, MonthTo, DayTo);

If you wanted to specify a day you'd just add the date as an additional
argument, so to list everyone's addresses on Christmas day this year:

SELECT FirstName, LastName, Address
FROM Members INNER JOIN Addresses
ON Members.MemberID = Addresses.MemberID
WHERE IsCurrentAddress(MonthFrom, DayFrom, MonthTo, DayTo, #12/25/2005#);

or you could reference a text box control on a dialogue form as a parameter
(giving the text box a DefaultValue property of Date() so it opens with the
current date in place) and open a form or report based on the query from a
button on the form:

PARAMETERS Forms!YourForm!txtDate DATETIME;
SELECT FirstName, LastName, Address
FROM Members INNER JOIN Addresses
ON Members.MemberID = Addresses.MemberID
WHERE IsCurrentAddress(MonthFrom, DayFrom, MonthTo, DayTo,
Forms!YourForm!txtDate);
 
K

Ken Sheridan

With respect, John, I'd strongly recommend against the latter option below.
It constitutes the encoding of data as column headings. In the database
relational model data should be stored as values at column positions in rows
in tables and in no other way.
 
F

FOSTER HAZELWOOOD

JBruno said:
We are creating a new database. Our members have multiple addresses and we
would like to set the system so that it will default to the select address
based on dates. For example, the members have summer and winter addresses. I
would like to set the system to default to the summer address from May -
October and the winter address from November - April. I want to avoid having
to manually switch the address. Also, the dates will not be the same for each
member some stay during the summer and others leave at different times.
Thanks for your help!
 
Top