views:

36

answers:

1

my problem is as follows I have a properties form which contains a textfield called DateTo(this textfield contains the data type Date) I would like to loop through the DateTo textfield on each of the properties and prints this out in a message box as wasll as a tenantName textfield and a tenantAddress1 field. After this is complete i would like to display in the message box only the properties that meets a particular condition. A month before the Date in the DateTo textbox I would like these values to be printed out in a message box.

any help would be greately appreciated

A: 

Something like this should give you a framework to work from.

Dim db As DAO.Database
Dim rs1 As DAO.Recordset

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("SELECT DateTo, TenantName, TenantAddress1 FROM " & Me.RecordSource)

If rs1.RecordCount > 0 Then
    rs1.MoveFirst
    Do Until rs.EOF
        MsgBox rs.Fields("DateTo") & ", " & rs.Fields("TenantName") & ", " & rs.Fields("TenantAddress1")
        rs1.MoveNext
    Loop
End If
rs1.Close
set rs1 = Nothing
set db = Nothing

You'll need to change the set rs1 = db... line to include whatever criteria you want to add.

Something like:

set rs1 = db.OpenRecordset("SELECT DateTo, TenantName, TenantAddress1 FROM " & Me.RecordSource & " WHERE DateTo >= #1/1/2010#")

Or whatever criteria you are looking for.

KevenDenen
Er, if he has the data open in a form, why are you opening a second copy of it in a recordset? Why not just use the form, or one of the form's recordsets?
David-W-Fenton
Just keeping things separate. I assumed that he wanted the data in the form to be unaffected by any filtering he's doing to get the message boxes. Feel free to provide an answer that uses the form's recordset.
KevenDenen
He wants to loop through the records. If you do that on the form's Recordset, it moves the current record in the form. But there's this handy RecordsetClone (that has existed since at least Access 2) that allows you to navigate a clone of the form's recordset without having any effect on the form's recordset. Your code doesn't do any filtering, so I'm not sure why you suggest that as a reason to use a new recordset.
David-W-Fenton
If you take the time to read his question and my answer again, it says, "After this is complete i would like to display in the message box only the properties that meets a particular condition." Hence, the second code box where I recommend he change the OpenRecordset line to a SQL query that has a WHERE clause. This is the filtering I was referring to.
KevenDenen