Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2008, 06:35 AM
Registered User
 
Join Date: Mar 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Linking table Date fields by month

Hello,

I have a database design problem that I cannot wrap my head around. There are three tables roughly like this:

Code:
tblUsers      tblAccuracy      tblRecords
-----         ---------        -------
UserID        UserID           UserID
Name          MonthYear        Date
...           Accuracy         NoOfRecords
tblUsers and tblRecords are joined by UserID (Text) in a one-to-many relationship.

tblUsers and tblAccuracy are joined by UserID (Text) in a one-to-many relationship.

tblAccuracy stores User accuracies on a monthly basis where [MonthYear] is a Date/Time column formatted to "mmm yyyy" with each entry being the first of each month (eg. 01/01/2008 to display Jan 2008)

tblRecords stores [NoOfRecords] on a daily basis for each [UserID] where tblRecords.[Date] is a Date/Time column.

---

The problem is that I need a way to query these tables to return a [UserID] from tblUsers, the list of all records entered by that [UserID] for a given month on a daily basis from tblRecords and a single [Accuracy] figure for that [UserID], for that [MonthYear] from tblAccuracy.

tblAccuracy and tblRecords have no primary keys and I'm not sure how to normalize them, or if it is even possible. [MonthYear] is stored eg. as 01/01/2008 for January and [Date] has many records for January and I don't know how to relate them.

I hope someone here can help :S

 
Old April 2nd, 2008, 07:11 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am working on this, but how do you store a date in this format "mmm yyyy"?


mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old April 2nd, 2008, 07:21 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Another design issue. Here is what I did:

Here is my basic table data:

tblUsers
UserID Name
1 Mike
2 Calvin
3 Sam

tblRecords
UserID Date NoOfRecords
Calvin 4/2/2008 2
Calvin 4/2/2008 5
Mike 4/2/2008 1
Sam 4/2/2008 3
Sam 4/2/2008 7

tblAccuracy
UserID MonthYear Accuracy
Mike 4/2/2008 2
Calvin 4/2/2008 3
Sam 4/2/2008 1

Date issues notwithstanding, I created to queries to compile the data:


qryRecords
SELECT tblUsers.UserID, tblUsers.Name, Sum(tblRecords.NoOfRecords) AS SumOfNoOfRecords, DatePart("m",[tblRecords.Date]) AS RunMonth
FROM tblUsers LEFT JOIN tblRecords ON tblUsers.UserID = tblRecords.UserID
GROUP BY tblUsers.UserID, tblUsers.Name, DatePart("m",[tblRecords.Date])
HAVING (((DatePart("m",[tblRecords.Date]))=4));

Results:
UserID Name SumOfNoOfRecords RunMonth
1 Mike 1 4
2 Calvin 7 4
3 Sam 10 4

qryAccuracy
SELECT tblUsers.UserID, tblUsers.Name, tblAccuracy.Accuracy, DatePart("m",[MonthYear]) AS RunMonth
FROM tblUsers LEFT JOIN tblAccuracy ON tblUsers.UserID = tblAccuracy.UserID
GROUP BY tblUsers.UserID, tblUsers.Name, tblAccuracy.Accuracy, DatePart("m",[MonthYear])
HAVING (((DatePart("m",[MonthYear]))=4));

Results:
UserID Name Accuracy RunMonth
1 Mike 2 4
2 Calvin 3 4
3 Sam 1 4

Then I created a query called "qryRecords_Accuracy"

qryRecords_Accuracy
SELECT qryRecords.Name, qryRecords.SumOfNoOfRecords, qryAccuracy.Accuracy
FROM qryRecords INNER JOIN qryAccuracy ON qryRecords.UserID = qryAccuracy.UserID;

Results:
Name SumOfNoOfRecords Accuracy
Mike 1 2
Calvin 7 3
Sam 10 1

Did that help?

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old April 2nd, 2008, 07:30 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I didn't mention. I coded in the Month to be 4, but you can either pass that parameter when you open the query, or you can leave it blank and get all the records by Month for each user. The problem with the last method is that it will compile one year on top of the next unless you also specify DatePart("yyyy", [Date])

Also, I see a lot of people doing this, but there are reserved names in SQL that you don't want to use for column names, like DATE.

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old April 3rd, 2008, 04:35 AM
Registered User
 
Join Date: Mar 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, thanks for taking an interest.

I have since with help from another forum solved this problem quite elegantly like this:

Code:
SELECT *
FROM tblAccuracy INNER JOIN tblRecords
     ON tblAccuracy.UserID = tblRecords.UserID
     WHERE MonthYear = getDateMonth()
     AND (tblRecords.vDate BETWEEN getDateMonth() AND getDateSerial())
Where vDate is the changed Date as you also pointed out, and the VBA functions are very simple and just pass values from a form.
The only one slightly out is:

Code:
Public Function getDateSerial() As Date

    Dim c As Control
    Set c = Forms![frmBonusReport]![cbDateMonth]
    getDateSerial = DateSerial(Year(c.Value), Month(c.Value) + 1, 0)

End Function
Still, since you put a lot of thought to helping me out, I will time permitting today follow through with the design you suggested and let you know if it also works :)

Thanks again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help linking userID fields RJ2001 SQL Server 2000 0 August 8th, 2007 08:33 PM
Month and Year to Date on a Report Mitch Access 2 February 20th, 2007 10:23 AM
query Current Month, Month+1, Month+2, Month+3 anterior Access 2 September 24th, 2006 08:25 PM
Linking 2 data fields from 1 table aRtware Access 7 May 28th, 2006 11:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.