Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Recordset statement help

Status
Not open for further replies.

cranebill

IS-IT--Management
Jan 4, 2002
1,113
US
I have a query that i built that consists of 2 rows. A date from the first row is to be put in 2 places on a report, and a date from the second row is to be put in 2 other places on the same report. I posted this in Other Access problems a guy wrote back and said i needed to use vba to do this. This is what he suggested:

Dim db as Database
Dim rs as Recordset
Dim Date1 As Date
Dim Date2 As Date

Set db = CurrentDB()
Set rs = db.OpenRecordset("NameOfQuery")

Date1 = rs!DateFieldName
rs.MoveNext
Date2 = rs!DateFieldName

Me.ReportControlName = Date1
Me.ReportControlName = Date2

rs.Close
Set rs = Nothing
Set db = Nothing

ok now that was to put the dates in one spot each on the form, but come to find out i needed two so i modified it to look like this:

Private Sub Report_Open(Cancel As Integer)
Dim db As Database
Dim rs As Recordset
Dim Date1 As Date
Dim Date2 As Date

Set db = CurrentDb()
Set rs = db.OpenRecordset("LastInspectionDate")

Date1 = rs!DateOf
rs.MoveNext
Date2 = rs!DateOf

Me.Current1 = Date1
Me.Current2 = Date1
Me.Last1 = Date2
Me.Last2 = Date2

rs.Close
Set rs = Nothing
Set db = Nothing


I get a too few parameters in the statement
Set rs = db.OpenRecordset("LastInspectionDate")

LastInspectionDate is the name of the query
DateOf is my Field in my Query
Current1, Current2, Last1, Last2 are the textboxes that need to be filled. Did I do this right or wrong?

Any help would be greatly appreciated.

Bill

 
Is LastInspectionDate a parameter query? =============
Jeremy Wallace
Designing, Developing, and Deploying Access Databases Since 1995

Take a look at the Developer's section of the site for some helpful fundamentals.
 
Im not exactly sure what you mean... you help me all the time Jeremy i appreciate that... seems everytime i post a question you happen to pop in. :)... anyway LastInspectionDate is a query that pulls a list of inspection for a customer and set to a decending order by date and is set to only show the top two results. Hope that answers your question.
 
Glad to be of so much help.

Can you post the sql from that query? That will help. =============
Jeremy Wallace
Designing, Developing, and Deploying Access Databases Since 1995

Take a look at the Developer's section of the site for some helpful fundamentals.
 
SELECT TOP 2 InspectionTable.InspectionID, InspectionTable.CompanyID, InspectionTable.DateOf
FROM InspectionTable
WHERE (((InspectionTable.CompanyID)=[Forms]![Main Menu]![Customer ID]) AND ((InspectionTable.DateOf)<Date()))
ORDER BY InspectionTable.InspectionID DESC;
 
You have to open the report only after the form has been opened and there's data in the Customer ID field.

Are you doing that?

Jeremy =============
Jeremy Wallace
Designing, Developing, and Deploying Access Databases Since 1995

Take a look at the Developer's section of the site for some helpful fundamentals.
 
Main Menu Form stays open so the data is there to refer to :) See the report works..... but i need the two dates instead of the one i originally thought because i originally only needed the most recent, but now this came upand i tried various query configs which just didnt work. So i posted and got the VBA does it seem like it should work?

 
ok i re ran it.... this is exactly what it says

Run-Time Error '3061'

Too Few Parameters. Expected 1
 
Bill,

Your query is a parameter query. Set your parameters in your SQL statement, then get your dates. Try this:

Private Sub Report_Open(Cancel As Integer)
Dim db As Database
Dim rs As Recordset
Dim Date1 As Date
Dim Date2 As Date
Dim CompanyID As Long
Dim dt as Date

CompanyID = [forms]![Main Menu]![Customer ID]
dt = date()

Set db = CurrentDb()

set rs = db.OpenRecordset(&quot;SELECT TOP 2 InspectionTable.InspectionID, InspectionTable.CompanyID, InspectionTable.DateOf
FROM InspectionTable
WHERE (((InspectionTable.CompanyID)= &quot; & CompanyID & &quot;) AND ((InspectionTable.DateOf)< #&quot; & dt & &quot;#))
ORDER BY InspectionTable.InspectionID DESC;&quot;)


Date1 = rs!DateOf
rs.MoveNext
Date2 = rs!DateOf

Me.Current1 = Date1
Me.Current2 = Date1
Me.Last1 = Date2
Me.Last2 = Date2

rs.Close
Set rs = Nothing
Set db = Nothing

Dan
 
Ok, hope you guys had a great Thanksgiving.... well Dan we have moved farther but still no Turkey lol... ok we now get down to this statement:

Me.Current1 = Date1

but unfortunatly we now get the error &quot;You cannot set a value to this object&quot;

I checked to see if the spelling was correct and it is.... so again im lost... any ideas?
 
Ok i made that change and still the same Error occurs.... what does the &quot;Me&quot; mean in that statement if i may ask...
 
Check it out in the help files. The Me keyword refers to the form on which the code is being run.

What is Current1? =============
Jeremy Wallace
Designing, Developing, and Deploying Access Databases Since 1995

Take a look at the Developer's section of the site for some helpful fundamentals.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top