473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem building ACCESS query for retrival of records.

I want to write such criteria in the query of microsoft ACCESS, which can give records with secondlast date?
table & fields............desired output
no-name-date..............name-date
1-A-07/15/07..............A-07/15/07
2-A-08/18/07..............B-04/12/07
3-B-04/12/07
4-B-08/18/07
5-A-07/03/07
Jul 28 '07 #1
18 2556
ADezii
8,834 Expert 8TB
I want to write such criteria in the query of microsoft ACCESS, which can give records with secondlast date?
table & fields............desired output
no-name-date..............name-date
1-A-07/15/07..............A-07/15/07
2-A-08/18/07..............B-04/12/07
3-B-04/12/07
4-B-08/18/07
5-A-07/03/07
I don't think that what you are requesting can be done with traditional SQL, but I think it can be done in code. There are other Moderators/Experts more proficient in SQL than I, so I'm just going to wait and see what they think. In the mean time, the following SQL will display the Last Date for each name. I know that it is not the solution, but it is a starting point:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDates.name, Max(tblDates.date) AS [Latest Date]
  2. FROM tblDates
  3. GROUP BY tblDates.name
  4. ORDER BY tblDates.name;
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. name    Latest Date
  2. A        8/18/2007
  3. B        8/18/2007
  4. C        12/30/2007
  5.  
Jul 29 '07 #2
Thanks for redirecting me towards the way, probably i can go near the solution
Jul 30 '07 #3
ADezii
8,834 Expert 8TB
Thanks for redirecting me towards the way, probably i can go near the solution
You're quite welcome. I'll continue to monitor this Post, and if no viable solution is found, I'll attempt one through code.
Jul 30 '07 #4
You're quite welcome. I'll continue to monitor this Post, and if no viable solution is found, I'll attempt one through code.
Dear sir,
Can anything done like following ?
1) Last date records are filtered by the first query method you suggested.
2) with second query, all the records from first query are deducted.
3) The third query will display last records by same method from second query, which are records with secondlast date for first query.

You might generalise this equation.
Jul 31 '07 #5
Thank you very much for your motivation, Sir. Your small help has prepared me to solve complicated problems.

I had successfully builded a set of three queries which can : Filter out each individual records with second last date for date field. The SQL code for that three queries is pasted :

Query_1 : dlt
SELECT payments.account_no, Max(payments.payment_date) AS [Latest Date]
FROM payments
GROUP BY payments.account_no
ORDER BY payments.account_no;

Query_2 : dlt_2
SELECT payments.account_no, payments.payment_date, dlt.[Latest Date]
FROM (Customers INNER JOIN dlt ON Customers.account_no = dlt.account_no) INNER JOIN payments ON Customers.account_no = payments.account_no
WHERE ((Not [payment_date]=[Latest Date]));

Query_3 : dlt_3
SELECT dlt_2.account_no, Max(dlt_2.payment_date) AS [Second last Date]
FROM dlt_2
GROUP BY dlt_2.account_no
ORDER BY dlt_2.account_no;

This solution can be helpful to others also. If i am doing any mistake, suggestions are welcome.
Jul 31 '07 #6
ADezii
8,834 Expert 8TB
Thank you very much for your motivation, Sir. Your small help has prepared me to solve complicated problems.

I had successfully builded a set of three queries which can : Filter out each individual records with second last date for date field. The SQL code for that three queries is pasted :

Query_1 : dlt
SELECT payments.account_no, Max(payments.payment_date) AS [Latest Date]
FROM payments
GROUP BY payments.account_no
ORDER BY payments.account_no;

Query_2 : dlt_2
SELECT payments.account_no, payments.payment_date, dlt.[Latest Date]
FROM (Customers INNER JOIN dlt ON Customers.account_no = dlt.account_no) INNER JOIN payments ON Customers.account_no = payments.account_no
WHERE ((Not [payment_date]=[Latest Date]));

Query_3 : dlt_3
SELECT dlt_2.account_no, Max(dlt_2.payment_date) AS [Second last Date]
FROM dlt_2
GROUP BY dlt_2.account_no
ORDER BY dlt_2.account_no;

This solution can be helpful to others also. If i am doing any mistake, suggestions are welcome.
A fine case of deductive reasoning - glad you were able to solve your own problem. Sometimes it just takes a little motivation on our part, and persistence on your part (LOL). I am also happy that you decided to share the solution with us, thanks.
Jul 31 '07 #7
A fine case of deductive reasoning - glad you were able to solve your own problem. Sometimes it just takes a little motivation on our part, and persistence on your part (LOL). I am also happy that you decided to share the solution with us, thanks.

Hello everybody
I had got one more problem.

I want to give serial numbers to each records which are generated in the REPORT by the mean of query. So what to do for generating automic serial no's which are given to each records, each time the report is generated.
Aug 3 '07 #8
ADezii
8,834 Expert 8TB
Hello everybody
I had got one more problem.

I want to give serial numbers to each records which are generated in the REPORT by the mean of query. So what to do for generating automic serial no's which are given to each records, each time the report is generated.
Are these Serial Numbers to be in any specific format or are they to just be numbered sequentially as in 1..2..3..4..5..6..7..8..9..10..........?
Aug 3 '07 #9
Are these Serial Numbers to be in any specific format or are they to just be numbered sequentially as in 1..2..3..4..5..6..7..8..9..10..........?

No specific formet. Instead of serial no, I can call it reacord count.

In General when this report is executed, each time the records will be counted and they will be numbered sequentially as 1,2,3,4,5........ till the last record.

e.g. Report for the records where city = London

Sr.No Name city
1 A London
2 B London
3 X London
Aug 5 '07 #10
dear all
i am pasting the code with errors, an effort to take this problem towards solution. If anyone can turn it into functionality, it will be a great help for me.


' I KNOW THIS CODE IS WITH ERRORS BUT I WANT TO MAKE IT ERROR FREE IF POSSIBLE
' SO THAT I CAN GENERATE A REPORT WHICH ACCEPTS DATE AS PARAMETER
' AND FILTER THAT RECORDS BY DATE AND STORES RESULTS IN TEMPORARY TABLE
' SO THAT RECORD COUNT CAN BE DONE AND THEN REPORT CAN BE GENERATED.

Private Sub Command1_Click()
'The If block closes the recordset if it was previously open before
'running the parameterized query.

With DataEnvironment1
If .rsCommand1.State = adStateOpen Then
.rsCommand1.Close
End If

' This passes in the value entered into the TextBox.

.command1 CDate(text1.Text)

' This If block checks to determine if any records are returned
' by the parameter. Then it shows a report if records are returned.
' Or displays a Message Box if no records are returned.

If .rsCommand1.RecordCount > 0 Then
Set rptashu.DataSource = DataEnvironment1

' code inserted to prepare a new table with record count

Dim i As Long
'Delete current data in A_Temp_Table
sql = "DELETE * FROM temptable"
Con.Excute sql

'select the data needed
sql = "SELECT * FROM dataenvironment1" ' selecting all the fields
Set rs = Con.Excute(sql)
i = 1
While Not rs.EOF
'insert row for temporary table with serial column
sql = "INSERT INTO temptable (Serial,account_no,card_no,name,amount,payment_dat e,Second last Date,Sum Of amount) VALUES(" & i & "," & rs.Fields(1) & "," & rs.Fields(2) & "," & rs.Fields(3) & "," & rs.Fields(4) & "," & rs.Fields(5) & "," & rs.Fields(6) & "," & rs.Fields(7)
Con.Excute sql
rs.MoveNext
Wend
'now, showing Report with data load from temp table
' report shown by parameter paseed as date, and taking all the records from temp table filtered by date and with record count

rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub
Aug 5 '07 #11
ADezii
8,834 Expert 8TB
dear all
i am pasting the code with errors, an effort to take this problem towards solution. If anyone can turn it into functionality, it will be a great help for me.


' I KNOW THIS CODE IS WITH ERRORS BUT I WANT TO MAKE IT ERROR FREE IF POSSIBLE
' SO THAT I CAN GENERATE A REPORT WHICH ACCEPTS DATE AS PARAMETER
' AND FILTER THAT RECORDS BY DATE AND STORES RESULTS IN TEMPORARY TABLE
' SO THAT RECORD COUNT CAN BE DONE AND THEN REPORT CAN BE GENERATED.

Private Sub Command1_Click()
'The If block closes the recordset if it was previously open before
'running the parameterized query.

With DataEnvironment1
If .rsCommand1.State = adStateOpen Then
.rsCommand1.Close
End If

' This passes in the value entered into the TextBox.

.command1 CDate(text1.Text)

' This If block checks to determine if any records are returned
' by the parameter. Then it shows a report if records are returned.
' Or displays a Message Box if no records are returned.

If .rsCommand1.RecordCount > 0 Then
Set rptashu.DataSource = DataEnvironment1

' code inserted to prepare a new table with record count

Dim i As Long
'Delete current data in A_Temp_Table
sql = "DELETE * FROM temptable"
Con.Excute sql

'select the data needed
sql = "SELECT * FROM dataenvironment1" ' selecting all the fields
Set rs = Con.Excute(sql)
i = 1
While Not rs.EOF
'insert row for temporary table with serial column
sql = "INSERT INTO temptable (Serial,account_no,card_no,name,amount,payment_dat e,Second last Date,Sum Of amount) VALUES(" & i & "," & rs.Fields(1) & "," & rs.Fields(2) & "," & rs.Fields(3) & "," & rs.Fields(4) & "," & rs.Fields(5) & "," & rs.Fields(6) & "," & rs.Fields(7)
Con.Excute sql
rs.MoveNext
Wend
'now, showing Report with data load from temp table
' report shown by parameter paseed as date, and taking all the records from temp table filtered by date and with record count

rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub
  1. As far as generating sequential numbers in a Report:
    1. Create an Unbound Text Box within the Detail Section of your Report.
    2. Set the Control Source of this Text Box to =1.
    3. Set the Running Sum to Over All.
    4. The above actions will display a sequential numbered list for each Record in the Report starting at 1.
  2. I'm having a little trouble interpreting your code. It seems as though you are attempting to generate a Report from External Data in an Access Database. This seems to be done via a DataEnvironment within Visual Basic. Is this correct? If this is not the case, please clarify.
Aug 5 '07 #12
  1. As far as generating sequential numbers in a Report:
    1. Create an Unbound Text Box within the Detail Section of your Report.
    2. Set the Control Source of this Text Box to =1.
    3. Set the Running Sum to Over All.
    4. The above actions will display a sequential numbered list for each Record in the Report starting at 1.
  2. I'm having a little trouble interpreting your code. It seems as though you are attempting to generate a Report from External Data in an Access Database. This seems to be done via a DataEnvironment within Visual Basic. Is this correct? If this is not the case, please clarify.
Dear sir,
you r correct. I forgot to mention, so please pardon.
I want to create report using visual basic by data environment.
I am using access query as data provider, by which report is generated in visual basic. I had been successful to generate a report, but I face the problem to give recoard count. so i want to count records and assign sequential record numbers in the report.

The SQL code to link query is as under :
select * from final_qry where payment_date = ?

And code by which report is generated successfully by passing parameter is as under, but without sequential record numbers.

Private Sub Command1_Click()
'The If block closes the recordset if it was previously open before
'running the parameterized query.
With DataEnvironment1
If .rsCommand1.State = adStateOpen Then

.rsCommand1.Close
End If

' This passes in the value entered into the TextBox.

.command1 CDate(text1.Text)

' This If block checks to determine if any records are returned
' by the parameter. Then it shows a report if records are returned.
' Or displays a Message Box if no records are returned.
If .rsCommand1.RecordCount > 0 Then
Set rptashu.DataSource = DataEnvironment1

rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub

I want to learn the method, something by which record numbers can be included in this report.
Aug 6 '07 #13
ADezii
8,834 Expert 8TB
Dear sir,
you r correct. I forgot to mention, so please pardon.
I want to create report using visual basic by data environment.
I am using access query as data provider, by which report is generated in visual basic. I had been successful to generate a report, but I face the problem to give recoard count. so i want to count records and assign sequential record numbers in the report.

The SQL code to link query is as under :
select * from final_qry where payment_date = ?

And code by which report is generated successfully by passing parameter is as under, but without sequential record numbers.

Private Sub Command1_Click()
'The If block closes the recordset if it was previously open before
'running the parameterized query.
With DataEnvironment1
If .rsCommand1.State = adStateOpen Then

.rsCommand1.Close
End If

' This passes in the value entered into the TextBox.

.command1 CDate(text1.Text)

' This If block checks to determine if any records are returned
' by the parameter. Then it shows a report if records are returned.
' Or displays a Message Box if no records are returned.
If .rsCommand1.RecordCount > 0 Then
Set rptashu.DataSource = DataEnvironment1

rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub

I want to learn the method, something by which record numbers can be included in this report.
It seems to me that the problem is how to generate sequential numbers in the underlying SQL, namely: select * from final_qry where payment_date = ?. It would then be a simple matter to display these numbers in the Report. The answer to this dilemma, at least to me, is not that obvious but I'll continue to have a look at it. As previously stated, generating sequential numbers within an Internal Access Report is quite simple, this is not the case here.
Aug 6 '07 #14
It seems to me that the problem is how to generate sequential numbers in the underlying SQL, namely: select * from final_qry where payment_date = ?. It would then be a simple matter to display these numbers in the Report. The answer to this dilemma, at least to me, is not that obvious but I'll continue to have a look at it. As previously stated, generating sequential numbers within an Internal Access Report is quite simple, this is not the case here.
Very correct sir. I had thinked a lot, that if sequential numbers r generated in the underlying SQL, the thing will be very easy. But i found the problem stated below :

One 'form' in visual basic is created by me, which will collect the date as parameter and then this query will be used to give records for that date. The problem is, we did not get the number of records filtered until this query is executed and so in advance we can not set one field for sequential numbers.

So as per your deep experience, is it possible thing to generate sequential numbers in the query itself ? If it seems possible to you, please guide me on the way.
Aug 6 '07 #15
ADezii
8,834 Expert 8TB
Very correct sir. I had thinked a lot, that if sequential numbers r generated in the underlying SQL, the thing will be very easy. But i found the problem stated below :

One 'form' in visual basic is created by me, which will collect the date as parameter and then this query will be used to give records for that date. The problem is, we did not get the number of records filtered until this query is executed and so in advance we can not set one field for sequential numbers.

So as per your deep experience, is it possible thing to generate sequential numbers in the query itself ? If it seems possible to you, please guide me on the way.
I'm still working on it, please be patient.
Aug 6 '07 #16
ADezii
8,834 Expert 8TB
I'm still working on it, please be patient.
It just dawned on me that I had experienced a similar request from a Member a long time ago, and here is how I resolved his problem. The circumstances are quite different, and the solution is a little extreme, but there seemed to be no other viable options and this logic does, in fact, work. I don't think that something like this can be implemented in your specific case, but I'll show you anyway. If you have any other questions, feel free to ask:

NeoPa: (me writing)
Here is a little Off-The-Wall technique that I always use whenever I want Query results to be numbered sequentially starting from 1 regardless of any Sort Order on any Field or Fields, number of Fields, etc. It is a little radical but there seems to be no other way to achieve the same result. Please look at it and let me know what you think. This code must be run prior to executing the Query and it also involves adding a [Counter] Field to the underlying Table then executing the Query with this ([Counter]) as the very 1st Column. It creates a Recordset based on the very Query that you want to sequentially number and writes the incremental values to the [Counter] Field starting at position 1. I'm sure that it can be improved - I just know that it works.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, intRecNo As Integer
  2. Set MyDB = CurrentDb()
  3. Set MyRS = MyDB.OpenRecordset("qryEmployee", dbOpenDynaset)
  4.  
  5. Do While Not MyRS.EOF
  6.   intRecNo = intRecNo + 1
  7.     MyRS.Edit
  8.       MyRS![Counter] = intRecNo
  9.     MyRS.Update
  10.     MyRS.MoveNext
  11. Loop
  12.  
  13. MyRS.Close
No matter what Criteria you specify for qryEmployee, and no matter how many Records are returned, they will always be numbered 1 to Recordset.RecordCount.
Aug 6 '07 #17
It just dawned on me that I had experienced a similar request from a Member a long time ago, and here is how I resolved his problem. The circumstances are quite different, and the solution is a little extreme, but there seemed to be no other viable options and this logic does, in fact, work. I don't think that something like this can be implemented in your specific case, but I'll show you anyway. If you have any other questions, feel free to ask:


Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, intRecNo As Integer
  2. Set MyDB = CurrentDb()
  3. Set MyRS = MyDB.OpenRecordset("qryEmployee", dbOpenDynaset)
  4.  
  5. Do While Not MyRS.EOF
  6.   intRecNo = intRecNo + 1
  7.     MyRS.Edit
  8.       MyRS![Counter] = intRecNo
  9.     MyRS.Update
  10.     MyRS.MoveNext
  11. Loop
  12.  
  13. MyRS.Close
No matter what Criteria you specify for qryEmployee, and no matter how many Records are returned, they will always be numbered 1 to Recordset.RecordCount.
Thank you again.
I had noticed that there is the word expert under your name which was moderator few days ago, so congrats for becoming expert.

Truly u r genius & expert. I like your way to support, that is the right way. Instead of giving straight forward answers u r giving answers in the form of hints or clever language. I like your art.

with the help of your last post i had reached to the solution and now successful generation of report is done with sequential numbers. I am providing the code for reference for others.

1) In command1 i have to select lock type = 4- batch optimistic.
2) code segment (VB)

If .rsCommand1.RecordCount > 0 Then
.rsCommand1.MoveFirst
Dim intRecNo As Integer
Do While Not .rsCommand1.EOF
intRecNo = intRecNo + 1
.rsCommand1.Update
.rsCommand1![Counter] = intRecNo
.rsCommand1.Update
.rsCommand1.MoveNext
Loop

Set rptashu.DataSource = DataEnvironment1
rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub

Thank you again.
Aug 7 '07 #18
ADezii
8,834 Expert 8TB
Thank you again.
I had noticed that there is the word expert under your name which was moderator few days ago, so congrats for becoming expert.

Truly u r genius & expert. I like your way to support, that is the right way. Instead of giving straight forward answers u r giving answers in the form of hints or clever language. I like your art.

with the help of your last post i had reached to the solution and now successful generation of report is done with sequential numbers. I am providing the code for reference for others.

1) In command1 i have to select lock type = 4- batch optimistic.
2) code segment (VB)

If .rsCommand1.RecordCount > 0 Then
.rsCommand1.MoveFirst
Dim intRecNo As Integer
Do While Not .rsCommand1.EOF
intRecNo = intRecNo + 1
.rsCommand1.Update
.rsCommand1![Counter] = intRecNo
.rsCommand1.Update
.rsCommand1.MoveNext
Loop

Set rptashu.DataSource = DataEnvironment1
rptashu.Show
Else
MsgBox "No Titles found"
End If
End With
End Sub

Thank you again.
I'm happy that you arrived at the solution and did, in fact, share it with all of us. I also thank you for your kind compliments, they are greatly appreciated. I'm just so glad that an old solution resurfaced and was able to assist another member. Take care and keep in touch.
Aug 7 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
4
by: John Smith | last post by:
Isn't life a bitch! You know what you want but you don't know how to get it. I have produced 12 queries that calculate a payment profile over 12 months. For a number of the records (ie with...
1
by: Eskil | last post by:
Hi I have a form that supplies my query with information on two different variables. The form uses a lookup to display a list of 5 different customer types and 5 different types of...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
4
by: stpark22 | last post by:
Hello, I'm building a tracking tool using Access 2003, where my users will be entering orders that includes quantity of products sold, and cost per unit. I'm in the process of building several...
7
by: wade.wall | last post by:
Hi all, I am having a problem appending data to an existing table. I have two tables with identical fields and I want to append the data from one table (T2) to the first (T1). T1 has 136...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.