By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,414 Members | 2,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,414 IT Pros & Developers. It's quick & easy.

Recordset to update queries - help!

P: n/a
Hi

First up, I am using MS Access 2002.

I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my predecessor, I hasten to add) so that each day it creates a
copy of the record for each company, changes the date to today's date,
and prompts the user for any changes of ratings on that day. The
resulting data table grows by approx 600 records per day.

If starting from scratch, the obvious way to do this would be to
record only the changes each day, thus creating a much smaller data
table. with an eye to doing this in te future, i would like to go
through every date in the data table, comparing the rating for each
company with that of the day before, and appending a new record to a
table "tblChanges" each time a change is detected.

I have created 2 queries to extract data for the two days for
comparison, and a third query which links the 2 original queries by
Company_ID, and appends any records where the rating has changed to
the table "tblChanges". This works fine, but I do not fancy running
the query x amount of times, manually entering the new dates for each
comparison.

It seems that using recordsets in VBA is the way to automate this
process. I imagine creating 2 recordsets which can each have the date
variable passed to them from the VBA code - one with data on one day,
and one with data on the next. I then need to run the append query on
these recordsets, to append any changed records to tblChanges.

I have very little experience of using recordsets, and have pieced
together the following code from newsgroups:

------------------------------

Sub FindChanges()

Dim db As DAO.Database
Dim rstOld As DAO.Recordset
Dim rstNew As DAO.Recordset
Dim qdfOld As DAO.QueryDef
Dim qdfNew As DAO.QueryDef
Dim DateOld As Date
Dim DateNew As Date

Set db = CurrentDb()

DateOld = "01/07/03"
DateNew = "01/08/03"

Do
' create the querydef object
Set qdfOld = db.QueryDefs("qryCompareOld")
With qdfOld
'resolve the parameter
.Parameters("Date") = DateOld
'open the recordset based on the Old data querydef
Set rstOld = .OpenRecordset
End With

Set qdfNew = db.QueryDefs("qryCompareNew")
With qdfNew
'resolve the parameter
.Parameters("Date") = DateNew
'open the recordset based on the new data querydef
Set rstNew = .OpenRecordset
End With

' run the append query "qryFindChanges", which compares the
results of CompareOld and CompareNew,
'and appends differences to table "tblChanges"
DoCmd.OpenQuery "qryFindChanges"
With rstOld
.Close
End With

With rstNew
.Close
End With

'Change date variables to compare next day's data for changes
DateOld = DateNew
if DateNew+ 1 > Now() Then Exit Sub
If WorkDay(DateNew + 1) = True Then
DateNew = DateNew + 1
Else
If WorkDay(DateNew + 2) = True Then
DateNew = DateNew + 2
Else: DateNew = DateNew + 3
End If
End If
End If

Loop

' Destroy the object vars
Set qdfOld = Nothing
Set qdfNew = Nothing
Set rstOld = Nothing
Set rstNew = Nothing
Set db = Nothing

End Sub

-------------------------------------

The code currently halts at
..Parameters("Date") = DateOld
with the error "Item not found in this collection", although I thought
Parameters was part of QueryDefs.

I also imagine that an error will occur when running the
"qryFindChanges" query, or at least the recordset data will not be
used in that query. Does the coding for this query have to be
included within the VBA code?

If so, how would this be achieved, and how can I make the results
append to the "tblChanges" table?

Sorry for the essay - I think I can see how this should be done, but
am too new to recordsets to get the coding spot on. Any help at all
would be much appreciated.

Dom
Nov 12 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
How about this:
Create a query with two copies of your table.
Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1
(you'll have to edit the SQL to get the latter join - it can't be built in
the grid directly.)
Use the criteria line to pick out only items where the rating has changed.
Add whatever fields you need for your new table.
Turn the query into a make-table query.

Run it once, and you should be done.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c*************************@posting.google.co m...
Hi

First up, I am using MS Access 2002.

I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my predecessor, I hasten to add) so that each day it creates a
copy of the record for each company, changes the date to today's date,
and prompts the user for any changes of ratings on that day. The
resulting data table grows by approx 600 records per day.

If starting from scratch, the obvious way to do this would be to
record only the changes each day, thus creating a much smaller data
table. with an eye to doing this in te future, i would like to go
through every date in the data table, comparing the rating for each
company with that of the day before, and appending a new record to a
table "tblChanges" each time a change is detected.

I have created 2 queries to extract data for the two days for
comparison, and a third query which links the 2 original queries by
Company_ID, and appends any records where the rating has changed to
the table "tblChanges". This works fine, but I do not fancy running
the query x amount of times, manually entering the new dates for each
comparison.

It seems that using recordsets in VBA is the way to automate this
process. I imagine creating 2 recordsets which can each have the date
variable passed to them from the VBA code - one with data on one day,
and one with data on the next. I then need to run the append query on
these recordsets, to append any changed records to tblChanges.

I have very little experience of using recordsets, and have pieced
together the following code from newsgroups:

------------------------------

Sub FindChanges()

Dim db As DAO.Database
Dim rstOld As DAO.Recordset
Dim rstNew As DAO.Recordset
Dim qdfOld As DAO.QueryDef
Dim qdfNew As DAO.QueryDef
Dim DateOld As Date
Dim DateNew As Date

Set db = CurrentDb()

DateOld = "01/07/03"
DateNew = "01/08/03"

Do
' create the querydef object
Set qdfOld = db.QueryDefs("qryCompareOld")
With qdfOld
'resolve the parameter
.Parameters("Date") = DateOld
'open the recordset based on the Old data querydef
Set rstOld = .OpenRecordset
End With

Set qdfNew = db.QueryDefs("qryCompareNew")
With qdfNew
'resolve the parameter
.Parameters("Date") = DateNew
'open the recordset based on the new data querydef
Set rstNew = .OpenRecordset
End With

' run the append query "qryFindChanges", which compares the
results of CompareOld and CompareNew,
'and appends differences to table "tblChanges"
DoCmd.OpenQuery "qryFindChanges"
With rstOld
.Close
End With

With rstNew
.Close
End With

'Change date variables to compare next day's data for changes
DateOld = DateNew
if DateNew+ 1 > Now() Then Exit Sub
If WorkDay(DateNew + 1) = True Then
DateNew = DateNew + 1
Else
If WorkDay(DateNew + 2) = True Then
DateNew = DateNew + 2
Else: DateNew = DateNew + 3
End If
End If
End If

Loop

' Destroy the object vars
Set qdfOld = Nothing
Set qdfNew = Nothing
Set rstOld = Nothing
Set rstNew = Nothing
Set db = Nothing

End Sub

-------------------------------------

The code currently halts at
.Parameters("Date") = DateOld
with the error "Item not found in this collection", although I thought
Parameters was part of QueryDefs.

I also imagine that an error will occur when running the
"qryFindChanges" query, or at least the recordset data will not be
used in that query. Does the coding for this query have to be
included within the VBA code?

If so, how would this be achieved, and how can I make the results
append to the "tblChanges" table?

Sorry for the essay - I think I can see how this should be done, but
am too new to recordsets to get the coding spot on. Any help at all
would be much appreciated.

Dom

Nov 12 '05 #2

P: n/a
Turtle,

thanks a lot for this - it worked perfectly and saved me a huge amount of time.

Dom
"MacDermott" <ma********@nospam.com> wrote in message news:<8M*******************@newsread1.news.atl.ear thlink.net>...
How about this:
Create a query with two copies of your table.
Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1
(you'll have to edit the SQL to get the latter join - it can't be built in
the grid directly.)
Use the criteria line to pick out only items where the rating has changed.
Add whatever fields you need for your new table.
Turn the query into a make-table query.

Run it once, and you should be done.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c*************************@posting.google.co m...
Hi

First up, I am using MS Access 2002.

I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my predecessor, I hasten to add) so that each day it creates a
copy of the record for each company, changes the date to today's date,
and prompts the user for any changes of ratings on that day. The
resulting data table grows by approx 600 records per day.

If starting from scratch, the obvious way to do this would be to
record only the changes each day, thus creating a much smaller data
table. with an eye to doing this in te future, i would like to go
through every date in the data table, comparing the rating for each
company with that of the day before, and appending a new record to a
table "tblChanges" each time a change is detected.

I have created 2 queries to extract data for the two days for
comparison, and a third query which links the 2 original queries by
Company_ID, and appends any records where the rating has changed to
the table "tblChanges". This works fine, but I do not fancy running
the query x amount of times, manually entering the new dates for each
comparison.

It seems that using recordsets in VBA is the way to automate this
process. I imagine creating 2 recordsets which can each have the date
variable passed to them from the VBA code - one with data on one day,
and one with data on the next. I then need to run the append query on
these recordsets, to append any changed records to tblChanges.

I have very little experience of using recordsets, and have pieced
together the following code from newsgroups:

------------------------------

Sub FindChanges()

Dim db As DAO.Database
Dim rstOld As DAO.Recordset
Dim rstNew As DAO.Recordset
Dim qdfOld As DAO.QueryDef
Dim qdfNew As DAO.QueryDef
Dim DateOld As Date
Dim DateNew As Date

Set db = CurrentDb()

DateOld = "01/07/03"
DateNew = "01/08/03"

Do
' create the querydef object
Set qdfOld = db.QueryDefs("qryCompareOld")
With qdfOld
'resolve the parameter
.Parameters("Date") = DateOld
'open the recordset based on the Old data querydef
Set rstOld = .OpenRecordset
End With

Set qdfNew = db.QueryDefs("qryCompareNew")
With qdfNew
'resolve the parameter
.Parameters("Date") = DateNew
'open the recordset based on the new data querydef
Set rstNew = .OpenRecordset
End With

' run the append query "qryFindChanges", which compares the
results of CompareOld and CompareNew,
'and appends differences to table "tblChanges"
DoCmd.OpenQuery "qryFindChanges"
With rstOld
.Close
End With

With rstNew
.Close
End With

'Change date variables to compare next day's data for changes
DateOld = DateNew
if DateNew+ 1 > Now() Then Exit Sub
If WorkDay(DateNew + 1) = True Then
DateNew = DateNew + 1
Else
If WorkDay(DateNew + 2) = True Then
DateNew = DateNew + 2
Else: DateNew = DateNew + 3
End If
End If
End If

Loop

' Destroy the object vars
Set qdfOld = Nothing
Set qdfNew = Nothing
Set rstOld = Nothing
Set rstNew = Nothing
Set db = Nothing

End Sub

-------------------------------------

The code currently halts at
.Parameters("Date") = DateOld
with the error "Item not found in this collection", although I thought
Parameters was part of QueryDefs.

I also imagine that an error will occur when running the
"qryFindChanges" query, or at least the recordset data will not be
used in that query. Does the coding for this query have to be
included within the VBA code?

If so, how would this be achieved, and how can I make the results
append to the "tblChanges" table?

Sorry for the essay - I think I can see how this should be done, but
am too new to recordsets to get the coding spot on. Any help at all
would be much appreciated.

Dom

Nov 12 '05 #3

P: n/a
Upon returning to this problem, which I thought I had fixed using the
solution below, I have encountered another problem.

The database records data items for each weekday, but not on Saturday
or Sunday. This means that if a rating change takes place on a
Monday, then it will not show up in the query outlined below.
The SQL statement (included below) compares ratings from Day 0 with
those from Day 0+1.

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
WHERE Data.Rating <> Data_1.Rating;

If a rating change took place on 5 Dec (Friday), then this would be
picked up when the query compared 20031204 data with data on 20031205
(YYYMMDD dates). However, data is not stored for Sat or Sun, so if
the change took place on 8 Dec (Monday), the query will not compare
Friday with Monday, but will make Monday - Tuesday the next
comparison, hence not picking up the rating change.

I have tried extending the SQL as follows to compare each day with
day+1 and day+3, so that rating chages on mondays will be picked up
(Friday+3=Monday).

AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
I realise that this would then need filtering, as each rating change
could potentially appear a few times. However, there seems to be a
huge amount of data returned (1000s of records, as opposed to the 180
returned before amending the query), which suggests that there is
something wrong with the SQL. Furthermore, this seems a very clunky
way of doing it.

Is there any way of inserting an "If" statement into the SQL, to tell
the query the if Day+1 does not exist, then (as Day must therefore be
a Friday) compare with Day+3?

Again, thanks in advance for any help anybody can provide.

Dom Boyce
do******@totalise.co.uk (Dom Boyce) wrote in message news:<7c*************************@posting.google.c om>...
Turtle,

thanks a lot for this - it worked perfectly and saved me a huge amount of time.

Dom
"MacDermott" <ma********@nospam.com> wrote in message news:<8M*******************@newsread1.news.atl.ear thlink.net>...
How about this:
Create a query with two copies of your table.
Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1
(you'll have to edit the SQL to get the latter join - it can't be built in
the grid directly.)
Use the criteria line to pick out only items where the rating has changed.
Add whatever fields you need for your new table.
Turn the query into a make-table query.

Run it once, and you should be done.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c*************************@posting.google.co m...
Hi

First up, I am using MS Access 2002.

I have a database which records analyst rating changes for a list of
companies on a daily basis. Unfortunately, the database has been set
up (by my predecessor, I hasten to add) so that each day it creates a
copy of the record for each company, changes the date to today's date,
and prompts the user for any changes of ratings on that day. The
resulting data table grows by approx 600 records per day.

If starting from scratch, the obvious way to do this would be to
record only the changes each day, thus creating a much smaller data
table. with an eye to doing this in te future, i would like to go
through every date in the data table, comparing the rating for each
company with that of the day before, and appending a new record to a
table "tblChanges" each time a change is detected.

I have created 2 queries to extract data for the two days for
comparison, and a third query which links the 2 original queries by
Company_ID, and appends any records where the rating has changed to
the table "tblChanges". This works fine, but I do not fancy running
the query x amount of times, manually entering the new dates for each
comparison.

It seems that using recordsets in VBA is the way to automate this
process. I imagine creating 2 recordsets which can each have the date
variable passed to them from the VBA code - one with data on one day,
and one with data on the next. I then need to run the append query on
these recordsets, to append any changed records to tblChanges.

I have very little experience of using recordsets, and have pieced
together the following code from newsgroups:

------------------------------

Sub FindChanges()

Dim db As DAO.Database
Dim rstOld As DAO.Recordset
Dim rstNew As DAO.Recordset
Dim qdfOld As DAO.QueryDef
Dim qdfNew As DAO.QueryDef
Dim DateOld As Date
Dim DateNew As Date

Set db = CurrentDb()

DateOld = "01/07/03"
DateNew = "01/08/03"

Do
' create the querydef object
Set qdfOld = db.QueryDefs("qryCompareOld")
With qdfOld
'resolve the parameter
.Parameters("Date") = DateOld
'open the recordset based on the Old data querydef
Set rstOld = .OpenRecordset
End With

Set qdfNew = db.QueryDefs("qryCompareNew")
With qdfNew
'resolve the parameter
.Parameters("Date") = DateNew
'open the recordset based on the new data querydef
Set rstNew = .OpenRecordset
End With

' run the append query "qryFindChanges", which compares the
results of CompareOld and CompareNew,
'and appends differences to table "tblChanges"
DoCmd.OpenQuery "qryFindChanges"
With rstOld
.Close
End With

With rstNew
.Close
End With

'Change date variables to compare next day's data for changes
DateOld = DateNew
if DateNew+ 1 > Now() Then Exit Sub
If WorkDay(DateNew + 1) = True Then
DateNew = DateNew + 1
Else
If WorkDay(DateNew + 2) = True Then
DateNew = DateNew + 2
Else: DateNew = DateNew + 3
End If
End If
End If

Loop

' Destroy the object vars
Set qdfOld = Nothing
Set qdfNew = Nothing
Set rstOld = Nothing
Set rstNew = Nothing
Set db = Nothing

End Sub

-------------------------------------

The code currently halts at
.Parameters("Date") = DateOld
with the error "Item not found in this collection", although I thought
Parameters was part of QueryDefs.

I also imagine that an error will occur when running the
"qryFindChanges" query, or at least the recordset data will not be
used in that query. Does the coding for this query have to be
included within the VBA code?

If so, how would this be achieved, and how can I make the results
append to the "tblChanges" table?

Sorry for the essay - I think I can see how this should be done, but
am too new to recordsets to get the coding spot on. Any help at all
would be much appreciated.

Dom

Nov 12 '05 #4

P: n/a
WARNING: AIR CODE

Perhaps you could use something like this:

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date =
IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
Data.Rating <> Data_1.Rating;

Changing from an inner join to a cross-product with WHERE conditions is not
something I have a good reason for - it just felt better to me here. You
could probably do it with an inner join, too.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Upon returning to this problem, which I thought I had fixed using the
solution below, I have encountered another problem.

The database records data items for each weekday, but not on Saturday
or Sunday. This means that if a rating change takes place on a
Monday, then it will not show up in the query outlined below.
The SQL statement (included below) compares ratings from Day 0 with
those from Day 0+1.

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
WHERE Data.Rating <> Data_1.Rating;

If a rating change took place on 5 Dec (Friday), then this would be
picked up when the query compared 20031204 data with data on 20031205
(YYYMMDD dates). However, data is not stored for Sat or Sun, so if
the change took place on 8 Dec (Monday), the query will not compare
Friday with Monday, but will make Monday - Tuesday the next
comparison, hence not picking up the rating change.

I have tried extending the SQL as follows to compare each day with
day+1 and day+3, so that rating chages on mondays will be picked up
(Friday+3=Monday).

AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
I realise that this would then need filtering, as each rating change
could potentially appear a few times. However, there seems to be a
huge amount of data returned (1000s of records, as opposed to the 180
returned before amending the query), which suggests that there is
something wrong with the SQL. Furthermore, this seems a very clunky
way of doing it.

Is there any way of inserting an "If" statement into the SQL, to tell
the query the if Day+1 does not exist, then (as Day must therefore be
a Friday) compare with Day+3?

Again, thanks in advance for any help anybody can provide.

Dom Boyce
do******@totalise.co.uk (Dom Boyce) wrote in message

news:<7c*************************@posting.google.c om>...
Turtle,

thanks a lot for this - it worked perfectly and saved me a huge amount of time.

Dom
"MacDermott" <ma********@nospam.com> wrote in message

news:<8M*******************@newsread1.news.atl.ear thlink.net>...
How about this:
Create a query with two copies of your table.
Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1 (you'll have to edit the SQL to get the latter join - it can't be built in the grid directly.)
Use the criteria line to pick out only items where the rating has changed. Add whatever fields you need for your new table.
Turn the query into a make-table query.

Run it once, and you should be done.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c*************************@posting.google.co m...
> Hi
>
> First up, I am using MS Access 2002.
>
> I have a database which records analyst rating changes for a list of
> companies on a daily basis. Unfortunately, the database has been set
> up (by my predecessor, I hasten to add) so that each day it creates a > copy of the record for each company, changes the date to today's date, > and prompts the user for any changes of ratings on that day. The
> resulting data table grows by approx 600 records per day.
>
> If starting from scratch, the obvious way to do this would be to
> record only the changes each day, thus creating a much smaller data
> table. with an eye to doing this in te future, i would like to go
> through every date in the data table, comparing the rating for each
> company with that of the day before, and appending a new record to a
> table "tblChanges" each time a change is detected.
>
> I have created 2 queries to extract data for the two days for
> comparison, and a third query which links the 2 original queries by
> Company_ID, and appends any records where the rating has changed to
> the table "tblChanges". This works fine, but I do not fancy running
> the query x amount of times, manually entering the new dates for each > comparison.
>
> It seems that using recordsets in VBA is the way to automate this
> process. I imagine creating 2 recordsets which can each have the date > variable passed to them from the VBA code - one with data on one day, > and one with data on the next. I then need to run the append query on > these recordsets, to append any changed records to tblChanges.
>
> I have very little experience of using recordsets, and have pieced
> together the following code from newsgroups:
>
> ------------------------------
>
> Sub FindChanges()
>
> Dim db As DAO.Database
> Dim rstOld As DAO.Recordset
> Dim rstNew As DAO.Recordset
> Dim qdfOld As DAO.QueryDef
> Dim qdfNew As DAO.QueryDef
> Dim DateOld As Date
> Dim DateNew As Date
>
> Set db = CurrentDb()
>
> DateOld = "01/07/03"
> DateNew = "01/08/03"
>
> Do
> ' create the querydef object
> Set qdfOld = db.QueryDefs("qryCompareOld")
> With qdfOld
> 'resolve the parameter
> .Parameters("Date") = DateOld
> 'open the recordset based on the Old data querydef
> Set rstOld = .OpenRecordset
> End With
>
> Set qdfNew = db.QueryDefs("qryCompareNew")
> With qdfNew
> 'resolve the parameter
> .Parameters("Date") = DateNew
> 'open the recordset based on the new data querydef
> Set rstNew = .OpenRecordset
> End With
>
> ' run the append query "qryFindChanges", which compares the
> results of CompareOld and CompareNew,
> 'and appends differences to table "tblChanges"
> DoCmd.OpenQuery "qryFindChanges"
>
>
> With rstOld
> .Close
> End With
>
> With rstNew
> .Close
> End With
>
> 'Change date variables to compare next day's data for changes
> DateOld = DateNew
> if DateNew+ 1 > Now() Then Exit Sub
> If WorkDay(DateNew + 1) = True Then
> DateNew = DateNew + 1
> Else
> If WorkDay(DateNew + 2) = True Then
> DateNew = DateNew + 2
> Else: DateNew = DateNew + 3
> End If
> End If
> End If
>
> Loop
>
> ' Destroy the object vars
> Set qdfOld = Nothing
> Set qdfNew = Nothing
> Set rstOld = Nothing
> Set rstNew = Nothing
> Set db = Nothing
>
> End Sub
>
> -------------------------------------
>
> The code currently halts at
> .Parameters("Date") = DateOld
> with the error "Item not found in this collection", although I thought > Parameters was part of QueryDefs.
>
> I also imagine that an error will occur when running the
> "qryFindChanges" query, or at least the recordset data will not be
> used in that query. Does the coding for this query have to be
> included within the VBA code?
>
> If so, how would this be achieved, and how can I make the results
> append to the "tblChanges" table?
>
> Sorry for the essay - I think I can see how this should be done, but
> am too new to recordsets to get the coding spot on. Any help at all
> would be much appreciated.
>
> Dom

Nov 12 '05 #5

P: n/a
Thanks again for your help. I had to modify the suggested solution,
as "vbFriday" was not recognised in SQL - replacing it with "6", the
day number of Friday, at least allowed the query to run.

however, the returned results were not as desired - they contained a
lot of duplicate records and, een after hours of fiddling, i could not
return the correct data.

By creating two queries, one for Fridays and one for other weekdays, I
can pull the required data out. The SQL for this follows. NB this has
been extended from the last SQL posted, in order to view changes not
only in Rating, but also in Type and Analyst, but the FROM and WHERE
sections illustrate how a full set of data is obtained:

qryFriday
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+3)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))=6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))=6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))=6))
ORDER BY Data_1.Date, Companies.Name;
qryOtherDays
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+1)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))<>6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))<>6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))<>6))
ORDER BY Data_1.Date, Companies.Name;

However, I now need to combine these two queries so I can view the
results in a form. is there a way of doing this, other than making
both queries Append queries, basing a form on the resulting table and
coding a FormLoad event to clear the table then run both append
queries?

Again, any help would be much appreciated. Dom

"MacDermott" <ma********@nospam.com> wrote in message news:<Xv***************@newsread2.news.atl.earthli nk.net>...
WARNING: AIR CODE

Perhaps you could use something like this:

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date =
IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
Data.Rating <> Data_1.Rating;

Changing from an inner join to a cross-product with WHERE conditions is not
something I have a good reason for - it just felt better to me here. You
could probably do it with an inner join, too.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Upon returning to this problem, which I thought I had fixed using the
solution below, I have encountered another problem.

The database records data items for each weekday, but not on Saturday
or Sunday. This means that if a rating change takes place on a
Monday, then it will not show up in the query outlined below.
The SQL statement (included below) compares ratings from Day 0 with
those from Day 0+1.

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
WHERE Data.Rating <> Data_1.Rating;

If a rating change took place on 5 Dec (Friday), then this would be
picked up when the query compared 20031204 data with data on 20031205
(YYYMMDD dates). However, data is not stored for Sat or Sun, so if
the change took place on 8 Dec (Monday), the query will not compare
Friday with Monday, but will make Monday - Tuesday the next
comparison, hence not picking up the rating change.

I have tried extending the SQL as follows to compare each day with
day+1 and day+3, so that rating chages on mondays will be picked up
(Friday+3=Monday).

AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
I realise that this would then need filtering, as each rating change
could potentially appear a few times. However, there seems to be a
huge amount of data returned (1000s of records, as opposed to the 180
returned before amending the query), which suggests that there is
something wrong with the SQL. Furthermore, this seems a very clunky
way of doing it.

Is there any way of inserting an "If" statement into the SQL, to tell
the query the if Day+1 does not exist, then (as Day must therefore be
a Friday) compare with Day+3?

Again, thanks in advance for any help anybody can provide.

Dom Boyce
do******@totalise.co.uk (Dom Boyce) wrote in message

news:<7c*************************@posting.google.c om>...
Turtle,

thanks a lot for this - it worked perfectly and saved me a huge amount of time.
Dom
"MacDermott" <ma********@nospam.com> wrote in message news:<8M*******************@newsread1.news.atl.ear thlink.net>... > How about this:
> Create a query with two copies of your table.
> Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1 > (you'll have to edit the SQL to get the latter join - it can't be built in > the grid directly.)
> Use the criteria line to pick out only items where the rating has changed. > Add whatever fields you need for your new table.
> Turn the query into a make-table query.
>
> Run it once, and you should be done.
>
> HTH
> - Turtle
>
> "Dom Boyce" <do******@totalise.co.uk> wrote in message
> news:7c*************************@posting.google.co m...
> > Hi
> >
> > First up, I am using MS Access 2002.
> >
> > I have a database which records analyst rating changes for a list of
> > companies on a daily basis. Unfortunately, the database has been set
> > up (by my predecessor, I hasten to add) so that each day it creates a > > copy of the record for each company, changes the date to today's date, > > and prompts the user for any changes of ratings on that day. The
> > resulting data table grows by approx 600 records per day.
> >
> > If starting from scratch, the obvious way to do this would be to
> > record only the changes each day, thus creating a much smaller data
> > table. with an eye to doing this in te future, i would like to go
> > through every date in the data table, comparing the rating for each
> > company with that of the day before, and appending a new record to a
> > table "tblChanges" each time a change is detected.
> >
> > I have created 2 queries to extract data for the two days for
> > comparison, and a third query which links the 2 original queries by
> > Company_ID, and appends any records where the rating has changed to
> > the table "tblChanges". This works fine, but I do not fancy running
> > the query x amount of times, manually entering the new dates for each > > comparison.
> >
> > It seems that using recordsets in VBA is the way to automate this
> > process. I imagine creating 2 recordsets which can each have the date > > variable passed to them from the VBA code - one with data on one day, > > and one with data on the next. I then need to run the append query on > > these recordsets, to append any changed records to tblChanges.
> >
> > I have very little experience of using recordsets, and have pieced
> > together the following code from newsgroups:
> >
> > ------------------------------
> >
> > Sub FindChanges()
> >
> > Dim db As DAO.Database
> > Dim rstOld As DAO.Recordset
> > Dim rstNew As DAO.Recordset
> > Dim qdfOld As DAO.QueryDef
> > Dim qdfNew As DAO.QueryDef
> > Dim DateOld As Date
> > Dim DateNew As Date
> >
> > Set db = CurrentDb()
> >
> > DateOld = "01/07/03"
> > DateNew = "01/08/03"
> >
> > Do
> > ' create the querydef object
> > Set qdfOld = db.QueryDefs("qryCompareOld")
> > With qdfOld
> > 'resolve the parameter
> > .Parameters("Date") = DateOld
> > 'open the recordset based on the Old data querydef
> > Set rstOld = .OpenRecordset
> > End With
> >
> > Set qdfNew = db.QueryDefs("qryCompareNew")
> > With qdfNew
> > 'resolve the parameter
> > .Parameters("Date") = DateNew
> > 'open the recordset based on the new data querydef
> > Set rstNew = .OpenRecordset
> > End With
> >
> > ' run the append query "qryFindChanges", which compares the
> > results of CompareOld and CompareNew,
> > 'and appends differences to table "tblChanges"
> > DoCmd.OpenQuery "qryFindChanges"
> >
> >
> > With rstOld
> > .Close
> > End With
> >
> > With rstNew
> > .Close
> > End With
> >
> > 'Change date variables to compare next day's data for changes
> > DateOld = DateNew
> > if DateNew+ 1 > Now() Then Exit Sub
> > If WorkDay(DateNew + 1) = True Then
> > DateNew = DateNew + 1
> > Else
> > If WorkDay(DateNew + 2) = True Then
> > DateNew = DateNew + 2
> > Else: DateNew = DateNew + 3
> > End If
> > End If
> > End If
> >
> > Loop
> >
> > ' Destroy the object vars
> > Set qdfOld = Nothing
> > Set qdfNew = Nothing
> > Set rstOld = Nothing
> > Set rstNew = Nothing
> > Set db = Nothing
> >
> > End Sub
> >
> > -------------------------------------
> >
> > The code currently halts at
> > .Parameters("Date") = DateOld
> > with the error "Item not found in this collection", although I thought > > Parameters was part of QueryDefs.
> >
> > I also imagine that an error will occur when running the
> > "qryFindChanges" query, or at least the recordset data will not be
> > used in that query. Does the coding for this query have to be
> > included within the VBA code?
> >
> > If so, how would this be achieved, and how can I make the results
> > append to the "tblChanges" table?
> >
> > Sorry for the essay - I think I can see how this should be done, but
> > am too new to recordsets to get the coding spot on. Any help at all
> > would be much appreciated.
> >
> > Dom

Nov 12 '05 #6

P: n/a
If you don't need to update the data on the form, you could use a union
query.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Thanks again for your help. I had to modify the suggested solution,
as "vbFriday" was not recognised in SQL - replacing it with "6", the
day number of Friday, at least allowed the query to run.

however, the returned results were not as desired - they contained a
lot of duplicate records and, een after hours of fiddling, i could not
return the correct data.

By creating two queries, one for Fridays and one for other weekdays, I
can pull the required data out. The SQL for this follows. NB this has
been extended from the last SQL posted, in order to view changes not
only in Rating, but also in Type and Analyst, but the FROM and WHERE
sections illustrate how a full set of data is obtained:

qryFriday
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+3)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))=6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))=6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))=6))
ORDER BY Data_1.Date, Companies.Name;
qryOtherDays
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+1)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))<>6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))<>6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))<>6))
ORDER BY Data_1.Date, Companies.Name;

However, I now need to combine these two queries so I can view the
results in a form. is there a way of doing this, other than making
both queries Append queries, basing a form on the resulting table and
coding a FormLoad event to clear the table then run both append
queries?

Again, any help would be much appreciated. Dom

"MacDermott" <ma********@nospam.com> wrote in message

news:<Xv***************@newsread2.news.atl.earthli nk.net>...
WARNING: AIR CODE

Perhaps you could use something like this:

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date =
IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
Data.Rating <> Data_1.Rating;

Changing from an inner join to a cross-product with WHERE conditions is not something I have a good reason for - it just felt better to me here. You could probably do it with an inner join, too.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Upon returning to this problem, which I thought I had fixed using the
solution below, I have encountered another problem.

The database records data items for each weekday, but not on Saturday
or Sunday. This means that if a rating change takes place on a
Monday, then it will not show up in the query outlined below.
The SQL statement (included below) compares ratings from Day 0 with
those from Day 0+1.

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
WHERE Data.Rating <> Data_1.Rating;

If a rating change took place on 5 Dec (Friday), then this would be
picked up when the query compared 20031204 data with data on 20031205
(YYYMMDD dates). However, data is not stored for Sat or Sun, so if
the change took place on 8 Dec (Monday), the query will not compare
Friday with Monday, but will make Monday - Tuesday the next
comparison, hence not picking up the rating change.

I have tried extending the SQL as follows to compare each day with
day+1 and day+3, so that rating chages on mondays will be picked up
(Friday+3=Monday).

AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
I realise that this would then need filtering, as each rating change
could potentially appear a few times. However, there seems to be a
huge amount of data returned (1000s of records, as opposed to the 180
returned before amending the query), which suggests that there is
something wrong with the SQL. Furthermore, this seems a very clunky
way of doing it.

Is there any way of inserting an "If" statement into the SQL, to tell
the query the if Day+1 does not exist, then (as Day must therefore be
a Friday) compare with Day+3?

Again, thanks in advance for any help anybody can provide.

Dom Boyce
do******@totalise.co.uk (Dom Boyce) wrote in message

news:<7c*************************@posting.google.c om>...
> Turtle,
>
> thanks a lot for this - it worked perfectly and saved me a huge amount
of time.
>
> Dom
>
>
> "MacDermott" <ma********@nospam.com> wrote in message

news:<8M*******************@newsread1.news.atl.ear thlink.net>...
> > How about this:
> > Create a query with two copies of your table.
> > Link them on CompanyID and on

MyTable.RecordDate=MyTable_1.RecordDate+1
> > (you'll have to edit the SQL to get the latter join - it can't
be built in
> > the grid directly.)
> > Use the criteria line to pick out only items where the rating has

changed.
> > Add whatever fields you need for your new table.
> > Turn the query into a make-table query.
> >
> > Run it once, and you should be done.
> >
> > HTH
> > - Turtle
> >
> > "Dom Boyce" <do******@totalise.co.uk> wrote in message
> > news:7c*************************@posting.google.co m...
> > > Hi
> > >
> > > First up, I am using MS Access 2002.
> > >
> > > I have a database which records analyst rating changes for a
list of > > > companies on a daily basis. Unfortunately, the database has been set > > > up (by my predecessor, I hasten to add) so that each day it creates a
> > > copy of the record for each company, changes the date to today's

date,
> > > and prompts the user for any changes of ratings on that day.
The > > > resulting data table grows by approx 600 records per day.
> > >
> > > If starting from scratch, the obvious way to do this would be to
> > > record only the changes each day, thus creating a much smaller data > > > table. with an eye to doing this in te future, i would like to go > > > through every date in the data table, comparing the rating for each > > > company with that of the day before, and appending a new record to a > > > table "tblChanges" each time a change is detected.
> > >
> > > I have created 2 queries to extract data for the two days for
> > > comparison, and a third query which links the 2 original queries by > > > Company_ID, and appends any records where the rating has changed to > > > the table "tblChanges". This works fine, but I do not fancy running > > > the query x amount of times, manually entering the new dates for

each
> > > comparison.
> > >
> > > It seems that using recordsets in VBA is the way to automate this > > > process. I imagine creating 2 recordsets which can each have the date
> > > variable passed to them from the VBA code - one with data on one

day,
> > > and one with data on the next. I then need to run the append
query on
> > > these recordsets, to append any changed records to tblChanges.
> > >
> > > I have very little experience of using recordsets, and have

pieced > > > together the following code from newsgroups:
> > >
> > > ------------------------------
> > >
> > > Sub FindChanges()
> > >
> > > Dim db As DAO.Database
> > > Dim rstOld As DAO.Recordset
> > > Dim rstNew As DAO.Recordset
> > > Dim qdfOld As DAO.QueryDef
> > > Dim qdfNew As DAO.QueryDef
> > > Dim DateOld As Date
> > > Dim DateNew As Date
> > >
> > > Set db = CurrentDb()
> > >
> > > DateOld = "01/07/03"
> > > DateNew = "01/08/03"
> > >
> > > Do
> > > ' create the querydef object
> > > Set qdfOld = db.QueryDefs("qryCompareOld")
> > > With qdfOld
> > > 'resolve the parameter
> > > .Parameters("Date") = DateOld
> > > 'open the recordset based on the Old data querydef
> > > Set rstOld = .OpenRecordset
> > > End With
> > >
> > > Set qdfNew = db.QueryDefs("qryCompareNew")
> > > With qdfNew
> > > 'resolve the parameter
> > > .Parameters("Date") = DateNew
> > > 'open the recordset based on the new data querydef
> > > Set rstNew = .OpenRecordset
> > > End With
> > >
> > > ' run the append query "qryFindChanges", which compares the
> > > results of CompareOld and CompareNew,
> > > 'and appends differences to table "tblChanges"
> > > DoCmd.OpenQuery "qryFindChanges"
> > >
> > >
> > > With rstOld
> > > .Close
> > > End With
> > >
> > > With rstNew
> > > .Close
> > > End With
> > >
> > > 'Change date variables to compare next day's data for changes
> > > DateOld = DateNew
> > > if DateNew+ 1 > Now() Then Exit Sub
> > > If WorkDay(DateNew + 1) = True Then
> > > DateNew = DateNew + 1
> > > Else
> > > If WorkDay(DateNew + 2) = True Then
> > > DateNew = DateNew + 2
> > > Else: DateNew = DateNew + 3
> > > End If
> > > End If
> > > End If
> > >
> > > Loop
> > >
> > > ' Destroy the object vars
> > > Set qdfOld = Nothing
> > > Set qdfNew = Nothing
> > > Set rstOld = Nothing
> > > Set rstNew = Nothing
> > > Set db = Nothing
> > >
> > > End Sub
> > >
> > > -------------------------------------
> > >
> > > The code currently halts at
> > > .Parameters("Date") = DateOld
> > > with the error "Item not found in this collection", although I

thought
> > > Parameters was part of QueryDefs.
> > >
> > > I also imagine that an error will occur when running the
> > > "qryFindChanges" query, or at least the recordset data will not be > > > used in that query. Does the coding for this query have to be
> > > included within the VBA code?
> > >
> > > If so, how would this be achieved, and how can I make the results > > > append to the "tblChanges" table?
> > >
> > > Sorry for the essay - I think I can see how this should be done, but > > > am too new to recordsets to get the coding spot on. Any help at all > > > would be much appreciated.
> > >
> > > Dom

Nov 12 '05 #7

P: n/a
This is exactly what I wanted Turtle, thanks again for all your help.

I say exactly what i wanted - it is exactly what I asked for but, as
usual, another issue has cropped up. I now use the union query to
effectively display records from two queries, which detect changes in
one or all of three variables from one day to another for each
Company.

However, i need to include in this query's results:
1. A record each time a completely new copmany is added.
2. Company information on the FIRST day the data begins
Currently, the queries are linked on Company_ID so that each day, each
company is compared with its status the day before. However, when a
new company is added on a particular day, and for companies on the
first day that data was recorded, there is no Company_ID for that
company on the previous day, hence no comparison takes place, and no
record is shown in the query.

Is it possible to add another UNION to the query, which would
calculate something like:

SELECT Data_1.Company_ID, Data_1.Date etc
FROM (not sure what would go here)
WHERE Data.CompanyID Does not exist for any given Data_1.Company_ID

Sorry for being a pain about this - I am fairly new to SQL, but am
being forced to pick it up very quickly, and appreciate all the help I
can get.

Happy New Year,

Dom

"MacDermott" <ma********@nospam.com> wrote in message news:<fI****************@newsread3.news.atl.earthl ink.net>...
If you don't need to update the data on the form, you could use a union
query.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Thanks again for your help. I had to modify the suggested solution,
as "vbFriday" was not recognised in SQL - replacing it with "6", the
day number of Friday, at least allowed the query to run.

however, the returned results were not as desired - they contained a
lot of duplicate records and, een after hours of fiddling, i could not
return the correct data.

By creating two queries, one for Fridays and one for other weekdays, I
can pull the required data out. The SQL for this follows. NB this has
been extended from the last SQL posted, in order to view changes not
only in Rating, but also in Type and Analyst, but the FROM and WHERE
sections illustrate how a full set of data is obtained:

qryFriday
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+3)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))=6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))=6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))=6))
ORDER BY Data_1.Date, Companies.Name;
qryOtherDays
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+1)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))<>6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))<>6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))<>6))
ORDER BY Data_1.Date, Companies.Name;

However, I now need to combine these two queries so I can view the
results in a form. is there a way of doing this, other than making
both queries Append queries, basing a form on the resulting table and
coding a FormLoad event to clear the table then run both append
queries?

Again, any help would be much appreciated. Dom

"MacDermott" <ma********@nospam.com> wrote in message

news:<Xv***************@newsread2.news.atl.earthli nk.net>...
WARNING: AIR CODE

Perhaps you could use something like this:

SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
Data.Analyst_ID, Data.Company_ID
FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
Data_1.Company_ID) AND (Data.Date =
IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
Data.Rating <> Data_1.Rating;

Changing from an inner join to a cross-product with WHERE conditions is not something I have a good reason for - it just felt better to me here. You could probably do it with an inner join, too.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
> Upon returning to this problem, which I thought I had fixed using the
> solution below, I have encountered another problem.
>
> The database records data items for each weekday, but not on Saturday
> or Sunday. This means that if a rating change takes place on a
> Monday, then it will not show up in the query outlined below.
> The SQL statement (included below) compares ratings from Day 0 with
> those from Day 0+1.
>
> SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
> Data.Analyst_ID, Data.Company_ID
> FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
> Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
> WHERE Data.Rating <> Data_1.Rating;
>
>
>
> If a rating change took place on 5 Dec (Friday), then this would be
> picked up when the query compared 20031204 data with data on 20031205
> (YYYMMDD dates). However, data is not stored for Sat or Sun, so if
> the change took place on 8 Dec (Monday), the query will not compare
> Friday with Monday, but will make Monday - Tuesday the next
> comparison, hence not picking up the rating change.
>
> I have tried extending the SQL as follows to compare each day with
> day+1 and day+3, so that rating chages on mondays will be picked up
> (Friday+3=Monday).
>
> AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
>
>
> I realise that this would then need filtering, as each rating change
> could potentially appear a few times. However, there seems to be a
> huge amount of data returned (1000s of records, as opposed to the 180
> returned before amending the query), which suggests that there is
> something wrong with the SQL. Furthermore, this seems a very clunky
> way of doing it.
>
> Is there any way of inserting an "If" statement into the SQL, to tell
> the query the if Day+1 does not exist, then (as Day must therefore be
> a Friday) compare with Day+3?
>
> Again, thanks in advance for any help anybody can provide.
>
> Dom Boyce
>
>
> do******@totalise.co.uk (Dom Boyce) wrote in message news:<7c*************************@posting.google.c om>... > > Turtle,
> >
> > thanks a lot for this - it worked perfectly and saved me a huge amount
of time. > >
> > Dom
> >
> >
> > "MacDermott" <ma********@nospam.com> wrote in message news:<8M*******************@newsread1.news.atl.ear thlink.net>... > > > How about this:
> > > Create a query with two copies of your table.
> > > Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1 > > > (you'll have to edit the SQL to get the latter join - it can't be
built in > > > the grid directly.)
> > > Use the criteria line to pick out only items where the rating has changed. > > > Add whatever fields you need for your new table.
> > > Turn the query into a make-table query.
> > >
> > > Run it once, and you should be done.
> > >
> > > HTH
> > > - Turtle
> > >
> > > "Dom Boyce" <do******@totalise.co.uk> wrote in message
> > > news:7c*************************@posting.google.co m...
> > > > Hi
> > > >
> > > > First up, I am using MS Access 2002.
> > > >
> > > > I have a database which records analyst rating changes for a list of > > > > companies on a daily basis. Unfortunately, the database has been set > > > > up (by my predecessor, I hasten to add) so that each day it creates
a > > > > copy of the record for each company, changes the date to today's date, > > > > and prompts the user for any changes of ratings on that day. The > > > > resulting data table grows by approx 600 records per day.
> > > >
> > > > If starting from scratch, the obvious way to do this would be to
> > > > record only the changes each day, thus creating a much smaller data > > > > table. with an eye to doing this in te future, i would like to go > > > > through every date in the data table, comparing the rating for each > > > > company with that of the day before, and appending a new record to a > > > > table "tblChanges" each time a change is detected.
> > > >
> > > > I have created 2 queries to extract data for the two days for
> > > > comparison, and a third query which links the 2 original queries by > > > > Company_ID, and appends any records where the rating has changed to > > > > the table "tblChanges". This works fine, but I do not fancy running > > > > the query x amount of times, manually entering the new dates for each > > > > comparison.
> > > >
> > > > It seems that using recordsets in VBA is the way to automate this > > > > process. I imagine creating 2 recordsets which can each have the
date > > > > variable passed to them from the VBA code - one with data on one day, > > > > and one with data on the next. I then need to run the append query
on > > > > these recordsets, to append any changed records to tblChanges.
> > > >
> > > > I have very little experience of using recordsets, and have pieced > > > > together the following code from newsgroups:
> > > >
> > > > ------------------------------
> > > >
> > > > Sub FindChanges()
> > > >
> > > > Dim db As DAO.Database
> > > > Dim rstOld As DAO.Recordset
> > > > Dim rstNew As DAO.Recordset
> > > > Dim qdfOld As DAO.QueryDef
> > > > Dim qdfNew As DAO.QueryDef
> > > > Dim DateOld As Date
> > > > Dim DateNew As Date
> > > >
> > > > Set db = CurrentDb()
> > > >
> > > > DateOld = "01/07/03"
> > > > DateNew = "01/08/03"
> > > >
> > > > Do
> > > > ' create the querydef object
> > > > Set qdfOld = db.QueryDefs("qryCompareOld")
> > > > With qdfOld
> > > > 'resolve the parameter
> > > > .Parameters("Date") = DateOld
> > > > 'open the recordset based on the Old data querydef
> > > > Set rstOld = .OpenRecordset
> > > > End With
> > > >
> > > > Set qdfNew = db.QueryDefs("qryCompareNew")
> > > > With qdfNew
> > > > 'resolve the parameter
> > > > .Parameters("Date") = DateNew
> > > > 'open the recordset based on the new data querydef
> > > > Set rstNew = .OpenRecordset
> > > > End With
> > > >
> > > > ' run the append query "qryFindChanges", which compares the
> > > > results of CompareOld and CompareNew,
> > > > 'and appends differences to table "tblChanges"
> > > > DoCmd.OpenQuery "qryFindChanges"
> > > >
> > > >
> > > > With rstOld
> > > > .Close
> > > > End With
> > > >
> > > > With rstNew
> > > > .Close
> > > > End With
> > > >
> > > > 'Change date variables to compare next day's data for changes
> > > > DateOld = DateNew
> > > > if DateNew+ 1 > Now() Then Exit Sub
> > > > If WorkDay(DateNew + 1) = True Then
> > > > DateNew = DateNew + 1
> > > > Else
> > > > If WorkDay(DateNew + 2) = True Then
> > > > DateNew = DateNew + 2
> > > > Else: DateNew = DateNew + 3
> > > > End If
> > > > End If
> > > > End If
> > > >
> > > > Loop
> > > >
> > > > ' Destroy the object vars
> > > > Set qdfOld = Nothing
> > > > Set qdfNew = Nothing
> > > > Set rstOld = Nothing
> > > > Set rstNew = Nothing
> > > > Set db = Nothing
> > > >
> > > > End Sub
> > > >
> > > > -------------------------------------
> > > >
> > > > The code currently halts at
> > > > .Parameters("Date") = DateOld
> > > > with the error "Item not found in this collection", although I thought > > > > Parameters was part of QueryDefs.
> > > >
> > > > I also imagine that an error will occur when running the
> > > > "qryFindChanges" query, or at least the recordset data will not be > > > > used in that query. Does the coding for this query have to be
> > > > included within the VBA code?
> > > >
> > > > If so, how would this be achieved, and how can I make the results > > > > append to the "tblChanges" table?
> > > >
> > > > Sorry for the essay - I think I can see how this should be done, but > > > > am too new to recordsets to get the coding spot on. Any help at all > > > > would be much appreciated.
> > > >
> > > > Dom

Nov 12 '05 #8

P: n/a
You can use an aggregate (or totals) query to get the first (Min) date for
each company in your DATA table.
Then join this back to the DATA table to get the other information you want
for that record.
You can then UNION this query with the ones you already have.

I think it could also be done with a subquery, but it escapes me at the
moment just how that would work.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
This is exactly what I wanted Turtle, thanks again for all your help.

I say exactly what i wanted - it is exactly what I asked for but, as
usual, another issue has cropped up. I now use the union query to
effectively display records from two queries, which detect changes in
one or all of three variables from one day to another for each
Company.

However, i need to include in this query's results:
1. A record each time a completely new copmany is added.
2. Company information on the FIRST day the data begins
Currently, the queries are linked on Company_ID so that each day, each
company is compared with its status the day before. However, when a
new company is added on a particular day, and for companies on the
first day that data was recorded, there is no Company_ID for that
company on the previous day, hence no comparison takes place, and no
record is shown in the query.

Is it possible to add another UNION to the query, which would
calculate something like:

SELECT Data_1.Company_ID, Data_1.Date etc
FROM (not sure what would go here)
WHERE Data.CompanyID Does not exist for any given Data_1.Company_ID

Sorry for being a pain about this - I am fairly new to SQL, but am
being forced to pick it up very quickly, and appreciate all the help I
can get.

Happy New Year,

Dom

"MacDermott" <ma********@nospam.com> wrote in message

news:<fI****************@newsread3.news.atl.earthl ink.net>...
If you don't need to update the data on the form, you could use a union
query.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
Thanks again for your help. I had to modify the suggested solution,
as "vbFriday" was not recognised in SQL - replacing it with "6", the
day number of Friday, at least allowed the query to run.

however, the returned results were not as desired - they contained a
lot of duplicate records and, een after hours of fiddling, i could not
return the correct data.

By creating two queries, one for Fridays and one for other weekdays, I
can pull the required data out. The SQL for this follows. NB this has
been extended from the last SQL posted, in order to view changes not
only in Rating, but also in Type and Analyst, but the FROM and WHERE
sections illustrate how a full set of data is obtained:

qryFriday
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+3)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))=6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))=6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))=6))
ORDER BY Data_1.Date, Companies.Name;
qryOtherDays
SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
Data.[Target price], Companies.Name,
iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
(Data_1.Date=Data.Date+1)) ON Analysts.Analyst_ID = Data.Analyst_ID)
ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
((Weekday([Data].[Date]))<>6)) OR (((Data.Type)<>[Data_1].[Type]) AND
((Weekday([Data].[Date]))<>6)) OR
(((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
((Weekday([Data].[Date]))<>6))
ORDER BY Data_1.Date, Companies.Name;

However, I now need to combine these two queries so I can view the
results in a form. is there a way of doing this, other than making
both queries Append queries, basing a form on the resulting table and
coding a FormLoad event to clear the table then run both append
queries?

Again, any help would be much appreciated. Dom

"MacDermott" <ma********@nospam.com> wrote in message

news:<Xv***************@newsread2.news.atl.earthli nk.net>...
> WARNING: AIR CODE
>
> Perhaps you could use something like this:
>
> SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
> Data.Analyst_ID, Data.Company_ID
> FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
> Data_1.Company_ID) AND (Data.Date =
> IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
> Data.Rating <> Data_1.Rating;
>
> Changing from an inner join to a cross-product with WHERE conditions is
not
> something I have a good reason for - it just felt better to me here.

You
> could probably do it with an inner join, too.
>
> HTH
> - Turtle
> "Dom Boyce" <do******@totalise.co.uk> wrote in message
> news:7c**************************@posting.google.c om...
> > Upon returning to this problem, which I thought I had fixed using
the > > solution below, I have encountered another problem.
> >
> > The database records data items for each weekday, but not on Saturday > > or Sunday. This means that if a rating change takes place on a
> > Monday, then it will not show up in the query outlined below.
> > The SQL statement (included below) compares ratings from Day 0 with > > those from Day 0+1.
> >
> > SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
> > Data.Analyst_ID, Data.Company_ID
> > FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
> > Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
> > WHERE Data.Rating <> Data_1.Rating;
> >
> >
> >
> > If a rating change took place on 5 Dec (Friday), then this would be > > picked up when the query compared 20031204 data with data on 20031205 > > (YYYMMDD dates). However, data is not stored for Sat or Sun, so if > > the change took place on 8 Dec (Monday), the query will not compare > > Friday with Monday, but will make Monday - Tuesday the next
> > comparison, hence not picking up the rating change.
> >
> > I have tried extending the SQL as follows to compare each day with
> > day+1 and day+3, so tha rating chages on mondays will be picked up > > (Friday+3=Monday).
> >
> > AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
> >
> >
> > I realise that this would then need filtering, as each rating change > > could potentially appear a few times. However, there seems to be a > > huge amount of data returned (1000s of records, as opposed to the 180 > > returned before amending the query), which suggests that there is
> > something wrong with the SQL. Furthermore, this seems a very clunky > > way of doing it.
> >
> > Is there any way of inserting an "If" statement into the SQL, to tell > > the query the if Day+1 does not exist, then (as Day must therefore be > > a Friday) compare with Day+3?
> >
> > Again, thanks in advance for any help anybody can provide.
> >
> > Dom Boyce
> >
> >
> > do******@totalise.co.uk (Dom Boyce) wrote in message

news:<7c*************************@posting.google.c om>...
> > > Turtle,
> > >
> > > thanks a lot for this - it worked perfectly and saved me a huge

amount
of time.
> > >
> > > Dom
> > >
> > >
> > > "MacDermott" <ma********@nospam.com> wrote in message

news:<8M*******************@newsread1.news.atl.ear thlink.net>...
> > > > How about this:
> > > > Create a query with two copies of your table.
> > > > Link them on CompanyID and on

MyTable.RecordDate=MyTable_1.RecordDate+1
> > > > (you'll have to edit the SQL to get the latter join - it can't be
built in
> > > > the grid directly.)
> > > > Use the criteria line to pick out only items where the rating
has changed.
> > > > Add whatever fields you need for your new table.
> > > > Turn the query into a make-table query.
> > > >
> > > > Run it once, and you should be done.
> > > >
> > > > HTH
> > > > - Turtle
> > > >
> > > > "Dom Boyce" <do******@totalise.co.uk> wrote in message
> > > > news:7c*************************@posting.google.co m...
> > > > > Hi
> > > > >
> > > > > First up, I am using MS Access 2002.
> > > > >
> > > > > I have a database which records analyst rating changes for a

list of
> > > > > companies on a daily basis. Unfortunately, the database has
been set
> > > > > up (by my predecessor, I hasten to add) so that each day it

creates
a
> > > > > copy of the record for each company, changes the date to
today's date,
> > > > > and prompts the user for any changes of ratings on that day.

The
> > > > > resulting data table grows by approx 600 records per day.
> > > > >
> > > > > If starting from scratch, the obvious way to do this would
be to > > > > > record only the changes each day, thus creating a much smaller data
> > > > > table. with an eye to doing this in te future, i would like
to go
> > > > > through every date in the data table, comparing the rating
for each
> > > > > company with that of the day before, and appending a new
record to a
> > > > > table "tblChanges" each time a change is detected.
> > > > >
> > > > > I have created 2 queries to extract data for the two days
for > > > > > comparison, and a third query which links the 2 original queries by
> > > > > Company_ID, and appends any records where the rating has
changed to
> > > > > the table "tblChanges". This works fine, but I do not fancy

running
> > > > > the query x amount of times, manually entering the new dates
for each
> > > > > comparison.
> > > > >
> > > > > It seems that using recordsets in VBA is the way to automate

this
> > > > > process. I imagine creating 2 recordsets which can each
have the
date
> > > > > variable passed to them from the VBA code - one with data on
one day,
> > > > > and one with data on the next. I then need to run the
append query
on
> > > > > these recordsets, to append any changed records to
tblChanges. > > > > >
> > > > > I have very little experience of using recordsets, and have

pieced
> > > > > together the following code from newsgroups:
> > > > >
> > > > > ------------------------------
> > > > >
> > > > > Sub FindChanges()
> > > > >
> > > > > Dim db As DAO.Database
> > > > > Dim rstOld As DAO.Recordset
> > > > > Dim rstNew As DAO.Recordset
> > > > > Dim qdfOld As DAO.QueryDef
> > > > > Dim qdfNew As DAO.QueryDef
> > > > > Dim DateOld As Date
> > > > > Dim DateNew As Date
> > > > >
> > > > > Set db = CurrentDb()
> > > > >
> > > > > DateOld = "01/07/03"
> > > > > DateNew = "01/08/03"
> > > > >
> > > > > Do
> > > > > ' create the querydef object
> > > > > Set qdfOld = db.QueryDefs("qryCompareOld")
> > > > > With qdfOld
> > > > > 'resolve the parameter
> > > > > .Parameters("Date") = DateOld
> > > > > 'open the recordset based on the Old data querydef
> > > > > Set rstOld = .OpenRecordset
> > > > > End With
> > > > >
> > > > > Set qdfNew = db.QueryDefs("qryCompareNew")
> > > > > With qdfNew
> > > > > 'resolve the parameter
> > > > > .Parameters("Date") = DateNew
> > > > > 'open the recordset based on the new data querydef
> > > > > Set rstNew = .OpenRecordset
> > > > > End With
> > > > >
> > > > > ' run the append query "qryFindChanges", which compares the > > > > > results of CompareOld and CompareNew,
> > > > > 'and appends differences to table "tblChanges"
> > > > > DoCmd.OpenQuery "qryFindChanges"
> > > > >
> > > > >
> > > > > With rstOld
> > > > > .Close
> > > > > End With
> > > > >
> > > > > With rstNew
> > > > > .Close
> > > > > End With
> > > > >
> > > > > 'Change date variables to compare next day's data for changes > > > > > DateOld = DateNew
> > > > > if DateNew+ 1 > Now() Then Exit Sub
> > > > > If WorkDay(DateNew + 1) = True Then
> > > > > DateNew = DateNew + 1
> > > > > Else
> > > > > If WorkDay(DateNew + 2) = True Then
> > > > > DateNew = DateNew + 2
> > > > > Else: DateNew = DateNew + 3
> > > > > End If
> > > > > End If
> > > > > End If
> > > > >
> > > > > Loop
> > > > >
> > > > > ' Destroy the object vars
> > > > > Set qdfOld = Nothing
> > > > > Set qdfNew = Nothing
> > > > > Set rstOld = Nothing
> > > > > Set rstNew = Nothing
> > > > > Set db = Nothing
> > > > >
> > > > > End Sub
> > > > >
> > > > > -------------------------------------
> > > > >
> > > > > The code currently halts at
> > > > > .Parameters("Date") = DateOld
> > > > > with the error "Item not found in this collection", although I thought
> > > > > Parameters was part of QueryDefs.
> > > > >
> > > > > I also imagine that an error will occur when running the
> > > > > "qryFindChanges" query, or at least the recordset data will
not be
> > > > > used in that query. Does the coding for this query have to
be > > > > > included within the VBA code?
> > > > >
> > > > > If so, how would this be achieved, and how can I make the

results
> > > > > append to the "tblChanges" table?
> > > > >
> > > > > Sorry for the essay - I think I can see how this should be

done, but
> > > > > am too new to recordsets to get the coding spot on. Any help
at all
> > > > > would be much appreciated.
> > > > >
> > > > > Dom

Nov 12 '05 #9

P: n/a
Thanks again for your help, Turtle. Used the Min function well then
linked back to original data. quite a messy solution with a lot of
queries based on queries, which will probably need cleaning up, but it
works for now so thanks again.

Dom
"MacDermott" <ma********@nospam.com> wrote in message news:<QW***************@newsread3.news.atl.earthli nk.net>...
You can use an aggregate (or totals) query to get the first (Min) date for
each company in your DATA table.
Then join this back to the DATA table to get the other information you want
for that record.
You can then UNION this query with the ones you already have.

I think it could also be done with a subquery, but it escapes me at the
moment just how that would work.

HTH
- Turtle
"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
This is exactly what I wanted Turtle, thanks again for all your help.

I say exactly what i wanted - it is exactly what I asked for but, as
usual, another issue has cropped up. I now use the union query to
effectively display records from two queries, which detect changes in
one or all of three variables from one day to another for each
Company.

However, i need to include in this query's results:
1. A record each time a completely new copmany is added.
2. Company information on the FIRST day the data begins
Currently, the queries are linked on Company_ID so that each day, each
company is compared with its status the day before. However, when a
new company is added on a particular day, and for companies on the
first day that data was recorded, there is no Company_ID for that
company on the previous day, hence no comparison takes place, and no
record is shown in the query.

Is it possible to add another UNION to the query, which would
calculate something like:

SELECT Data_1.Company_ID, Data_1.Date etc
FROM (not sure what would go here)
WHERE Data.CompanyID Does not exist for any given Data_1.Company_ID

Sorry for being a pain about this - I am fairly new to SQL, but am
being forced to pick it up very quickly, and appreciate all the help I
can get.

Happy New Year,

Dom

"MacDermott" <ma********@nospam.com> wrote in message

news:<fI****************@newsread3.news.atl.earthl ink.net>...
If you don't need to update the data on the form, you could use a union
query.

HTH
- Turtle

"Dom Boyce" <do******@totalise.co.uk> wrote in message
news:7c**************************@posting.google.c om...
> Thanks again for your help. I had to modify the suggested solution,
> as "vbFriday" was not recognised in SQL - replacing it with "6", the
> day number of Friday, at least allowed the query to run.
>
> however, the returned results were not as desired - they contained a
> lot of duplicate records and, een after hours of fiddling, i could not
> return the correct data.
>
> By creating two queries, one for Fridays and one for other weekdays, I
> can pull the required data out. The SQL for this follows. NB this has
> been extended from the last SQL posted, in order to view changes not
> only in Rating, but also in Type and Analyst, but the FROM and WHERE
> sections illustrate how a full set of data is obtained:
>
> qryFriday
> SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
> Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
> Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
> Data.[Target price], Companies.Name,
> iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
> Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
> Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
> Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
> Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
> FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
> AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
> (Data_1.Date=Data.Date+3)) ON Analysts.Analyst_ID = Data.Analyst_ID)
> ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
> Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
> WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
> ((Weekday([Data].[Date]))=6)) OR (((Data.Type)<>[Data_1].[Type]) AND
> ((Weekday([Data].[Date]))=6)) OR
> (((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
> ((Weekday([Data].[Date]))=6))
> ORDER BY Data_1.Date, Companies.Name;
>
>
> qryOtherDays
> SELECT Data_1.Date, Data_1.Rating AS [New Rating], Data_1.Type AS [New
> Type], Analysts_1.LastName AS [New Analyst], Data.Rating AS [Old
> Rating], Data.Type AS [Old Type], Analysts.LastName AS [Old Analyst],
> Data.[Target price], Companies.Name,
> iif([Data].[Analyst_ID]<>[Data_1].[Analyst_ID],IIf(nz([New
> Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
> Type],"RTA","RA"),IIf([New Type]<>[Old Type],"TA","A")), IIf(nz([New
> Rating])<>nz([Old Rating]),IIf([New Type]<>[Old
> Type],"RT","R"),IIf([New Type]<>[Old Type],"T",""))) AS [Change Type]
> FROM (Companies INNER JOIN (Analysts INNER JOIN (Data INNER JOIN Data
> AS Data_1 ON (Data.Company_ID = Data_1.Company_ID) AND
> (Data_1.Date=Data.Date+1)) ON Analysts.Analyst_ID = Data.Analyst_ID)
> ON Companies.Company_ID = Data.Company_ID) INNER JOIN Analysts AS
> Analysts_1 ON Data_1.Analyst_ID = Analysts_1.Analyst_ID
> WHERE (((nz([Data].[Rating]))<>nz([Data_1].[Rating])) AND
> ((Weekday([Data].[Date]))<>6)) OR (((Data.Type)<>[Data_1].[Type]) AND
> ((Weekday([Data].[Date]))<>6)) OR
> (((Data.Analyst_ID)<>[Data_1].[Analyst_ID]) AND
> ((Weekday([Data].[Date]))<>6))
> ORDER BY Data_1.Date, Companies.Name;
>
> However, I now need to combine these two queries so I can view the
> results in a form. is there a way of doing this, other than making
> both queries Append queries, basing a form on the resulting table and
> coding a FormLoad event to clear the table then run both append
> queries?
>
> Again, any help would be much appreciated. Dom
>
>
>
> "MacDermott" <ma********@nospam.com> wrote in message news:<Xv***************@newsread2.news.atl.earthli nk.net>... > > WARNING: AIR CODE
> >
> > Perhaps you could use something like this:
> >
> > SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
> > Data.Analyst_ID, Data.Company_ID
> > FROM Data , Data AS Data_1 WHERE (Data.Company_ID =
> > Data_1.Company_ID) AND (Data.Date =
> > IIF(WeekDay(Data.Date)=vbFriday,Data_1.Date+3,Data _1.Date+1) AND
> > Data.Rating <> Data_1.Rating;
> >
> > Changing from an inner join to a cross-product with WHERE conditions is
not > > something I have a good reason for - it just felt better to me here. You > > could probably do it with an inner join, too.
> >
> > HTH
> > - Turtle
> > "Dom Boyce" <do******@totalise.co.uk> wrote in message
> > news:7c**************************@posting.google.c om...
> > > Upon returning to this problem, which I thought I had fixed using the > > > solution below, I have encountered another problem.
> > >
> > > The database records data items for each weekday, but not on Saturday > > > or Sunday. This means that if a rating change takes place on a
> > > Monday, then it will not show up in the query outlined below.
> > > The SQL statement (included below) compares ratings from Day 0 with > > > those from Day 0+1.
> > >
> > > SELECT Data.Date, Data.Rating, Data.Type, Data.[Target price],
> > > Data.Analyst_ID, Data.Company_ID
> > > FROM Data INNER JOIN Data AS Data_1 ON (Data.Company_ID =
> > > Data_1.Company_ID) AND (Data.Date = Data_1.Date+1)
> > > WHERE Data.Rating <> Data_1.Rating;
> > >
> > >
> > >
> > > If a rating change took place on 5 Dec (Friday), then this would be > > > picked up when the query compared 20031204 data with data on 20031205 > > > (YYYMMDD dates). However, data is not stored for Sat or Sun, so if > > > the change took place on 8 Dec (Monday), the query will not compare > > > Friday with Monday, but will make Monday - Tuesday the next
> > > comparison, hence not picking up the rating change.
> > >
> > > I have tried extending the SQL as follows to compare each day with
> > > day+1 and day+3, so that rating chages on mondays will be picked up > > > (Friday+3=Monday).
> > >
> > > AND (Data.Date = Data_1.Date+1) OR (Data.Date = Data_1.Date+3)
> > >
> > >
> > > I realise that this would then need filtering, as each rating change > > > could potentially appear a few times. However, there seems to be a > > > huge amount of data returned (1000s of records, as opposed to the 180 > > > returned before amending the query), which suggests that there is
> > > something wrong with the SQL. Furthermore, this seems a very clunky > > > way of doing it.
> > >
> > > Is there any way of inserting an "If" statement into the SQL, to tell > > > the query the if Day+1 does not exist, then (as Day must therefore be > > > a Friday) compare with Day+3?
> > >
> > > Again, thanks in advance for any help anybody can provide.
> > >
> > > Dom Boyce
> > >
> > >
> > > do******@totalise.co.uk (Dom Boyce) wrote in message news:<7c*************************@posting.google.c om>... > > > > Turtle,
> > > >
> > > > thanks a lot for this - it worked perfectly and saved me a huge
amount
of time.
> > > >
> > > > Dom
> > > >
> > > >
> > > > "MacDermott" <ma********@nospam.com> wrote in message news:<8M*******************@newsread1.news.atl.ear thlink.net>... > > > > > How about this:
> > > > > Create a query with two copies of your table.
> > > > > Link them on CompanyID and on MyTable.RecordDate=MyTable_1.RecordDate+1 > > > > > (you'll have to edit the SQL to get the latter join - it can't be
built in
> > > > > the grid directly.)
> > > > > Use the criteria line to pick out only items where the rating has
changed. > > > > > Add whatever fields you need for your new table.
> > > > > Turn the query into a make-table query.
> > > > >
> > > > > Run it once, and you should be done.
> > > > >
> > > > > HTH
> > > > > - Turtle
> > > > >
> > > > > "Dom Boyce" <do******@totalise.co.uk> wrote in message
> > > > > news:7c*************************@posting.google.co m...
> > > > > > Hi
> > > > > >
> > > > > > First up, I am using MS Access 2002.
> > > > > >
> > > > > > I have a database which records analyst rating changes for a list of > > > > > > companies on a daily basis. Unfortunately, the database has been
set > > > > > > up (by my predecessor, I hasten to add) so that each day it
creates
a
> > > > > > copy of the record for each company, changes the date to today's
date, > > > > > > and prompts the user for any changes of ratings on that day. The > > > > > > resulting data table grows by approx 600 records per day.
> > > > > >
> > > > > > If starting from scratch, the obvious way to do this would be to > > > > > > record only the changes each day, thus creating a much smaller
data > > > > > > table. with an eye to doing this in te future, i would like to
go > > > > > > through every date in the data table, comparing the rating for
each > > > > > > company with that of the day before, and appending a new record
to a > > > > > > table "tblChanges" each time a change is detected.
> > > > > >
> > > > > > I have created 2 queries to extract data for the two days for > > > > > > comparison, and a third query which links the 2 original queries
by > > > > > > Company_ID, and appends any records where the rating has changed
to > > > > > > the table "tblChanges". This works fine, but I do not fancy running > > > > > > the query x amount of times, manually entering the new dates for
each > > > > > > comparison.
> > > > > >
> > > > > > It seems that using recordsets in VBA is the way to automate this > > > > > > process. I imagine creating 2 recordsets which can each have the
date
> > > > > > variable passed to them from the VBA code - one with data on one
day, > > > > > > and one with data on the next. I then need to run the append query
on
> > > > > > these recordsets, to append any changed records to tblChanges. > > > > > >
> > > > > > I have very little experience of using recordsets, and have pieced > > > > > > together the following code from newsgroups:
> > > > > >
> > > > > > ------------------------------
> > > > > >
> > > > > > Sub FindChanges()
> > > > > >
> > > > > > Dim db As DAO.Database
> > > > > > Dim rstOld As DAO.Recordset
> > > > > > Dim rstNew As DAO.Recordset
> > > > > > Dim qdfOld As DAO.QueryDef
> > > > > > Dim qdfNew As DAO.QueryDef
> > > > > > Dim DateOld As Date
> > > > > > Dim DateNew As Date
> > > > > >
> > > > > > Set db = CurrentDb()
> > > > > >
> > > > > > DateOld = "01/07/03"
> > > > > > DateNew = "01/08/03"
> > > > > >
> > > > > > Do
> > > > > > ' create the querydef object
> > > > > > Set qdfOld = db.QueryDefs("qryCompareOld")
> > > > > > With qdfOld
> > > > > > 'resolve the parameter
> > > > > > .Parameters("Date") = DateOld
> > > > > > 'open the recordset based on the Old data querydef
> > > > > > Set rstOld = .OpenRecordset
> > > > > > End With
> > > > > >
> > > > > > Set qdfNew = db.QueryDefs("qryCompareNew")
> > > > > > With qdfNew
> > > > > > 'resolve the parameter
> > > > > > .Parameters("Date") = DateNew
> > > > > > 'open the recordset based on the new data querydef
> > > > > > Set rstNew = .OpenRecordset
> > > > > > End With
> > > > > >
> > > > > > ' run the append query "qryFindChanges", which compares the > > > > > > results of CompareOld and CompareNew,
> > > > > > 'and appends differences to table "tblChanges"
> > > > > > DoCmd.OpenQuery "qryFindChanges"
> > > > > >
> > > > > >
> > > > > > With rstOld
> > > > > > .Close
> > > > > > End With
> > > > > >
> > > > > > With rstNew
> > > > > > .Close
> > > > > > End With
> > > > > >
> > > > > > 'Change date variables to compare next day's data for changes > > > > > > DateOld = DateNew
> > > > > > if DateNew+ 1 > Now() Then Exit Sub
> > > > > > If WorkDay(DateNew + 1) = True Then
> > > > > > DateNew = DateNew + 1
> > > > > > Else
> > > > > > If WorkDay(DateNew + 2) = True Then
> > > > > > DateNew = DateNew + 2
> > > > > > Else: DateNew = DateNew + 3
> > > > > > End If
> > > > > > End If
> > > > > > End If
> > > > > >
> > > > > > Loop
> > > > > >
> > > > > > ' Destroy the object vars
> > > > > > Set qdfOld = Nothing
> > > > > > Set qdfNew = Nothing
> > > > > > Set rstOld = Nothing
> > > > > > Set rstNew = Nothing
> > > > > > Set db = Nothing
> > > > > >
> > > > > > End Sub
> > > > > >
> > > > > > -------------------------------------
> > > > > >
> > > > > > The code currently halts at
> > > > > > .Parameters("Date") = DateOld
> > > > > > with the error "Item not found in this collection", although I
thought > > > > > > Parameters was part of QueryDefs.
> > > > > >
> > > > > > I also imagine that an error will occur when running the
> > > > > > "qryFindChanges" query, or at least the recordset data will not
be > > > > > > used in that query. Does the coding for this query have to be > > > > > > included within the VBA code?
> > > > > >
> > > > > > If so, how would this be achieved, and how can I make the results > > > > > > append to the "tblChanges" table?
> > > > > >
> > > > > > Sorry for the essay - I think I can see how this should be done,
but > > > > > > am too new to recordsets to get the coding spot on. Any help at
all > > > > > > would be much appreciated.
> > > > > >
> > > > > > Dom

Nov 12 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.