Populating fiels in a form with query data

G

Guest

I have a query that returns 5 rows of data. One column in the query is named
VALUE.

I have a form with 5 fields named : Value1, Value2, Value3, Value4, Value5.

I can get Value1 in the form equal to the first VALUE in the query with:
Value1 = DLookUp("[VALUE]","[QueryName]")

How can I get:
Value2 = the second VALUE listed in the query
Value3 = the third VALUE listed in the query
Value4 = the forth VALUE listed in the query
Value5 = the fifth VALUE listed in the query

Any help would be appreciated!
Thanks
Mark
 
G

Guest

Set to Control Source of the object on the form to
[QueryName]![Value2]
[QueryName]![Value3]
[QueryName]![Value4]
[QueryName]![Value5]

Hope this helps
 
G

Guest

If your query has other field names simply refer to them from the form
object's control source
[QueryName]![2ndValue]
[QueryName]![3rdValue]
[QueryName]![4thValue]
[QueryName]![5thValue]
 
J

John Spencer

I would use a continuous form or a subform to do this. If you cannot do
this, then perhaps

UNTESTED CODE - probably run this in the form's current event.
Assumptions:
The form is a Single form, not a continuous form.
This is an MDB and the DAO library is referenced

Private Sub Form_Current()

Dim rstAny as DAO.RecordSet
Dim dbAny as DAO.Database
Set dbAny = CurrentDb
Set rstAny = dbAny.OpenRecordset("QueryName")

With rstAny
If .recordcount > 0 then
.movelast
.MoveFirst
else
exit sub
end if

If .recordcount < 5 Then
exit sub
Else
Me.Value1 = .Fields("Value")
.movenext
Me.Value2 = .Fields("Value")
.movenext
Me.Value3 = .Fields("Value")
.movenext
Me.Value4 = .Fields("Value")
.movenext
Me.Value5 = .Fields("Value")
.movenext
End if

End With

End Sub
 
G

Guest

This works perfectly!!!!!
Many Thanks

Mark



John Spencer said:
I would use a continuous form or a subform to do this. If you cannot do
this, then perhaps

UNTESTED CODE - probably run this in the form's current event.
Assumptions:
The form is a Single form, not a continuous form.
This is an MDB and the DAO library is referenced

Private Sub Form_Current()

Dim rstAny as DAO.RecordSet
Dim dbAny as DAO.Database
Set dbAny = CurrentDb
Set rstAny = dbAny.OpenRecordset("QueryName")

With rstAny
If .recordcount > 0 then
.movelast
.MoveFirst
else
exit sub
end if

If .recordcount < 5 Then
exit sub
Else
Me.Value1 = .Fields("Value")
.movenext
Me.Value2 = .Fields("Value")
.movenext
Me.Value3 = .Fields("Value")
.movenext
Me.Value4 = .Fields("Value")
.movenext
Me.Value5 = .Fields("Value")
.movenext
End if

End With

End Sub

magicdds said:
I have a query that returns 5 rows of data. One column in the query is
named
VALUE.

I have a form with 5 fields named : Value1, Value2, Value3, Value4,
Value5.

I can get Value1 in the form equal to the first VALUE in the query with:
Value1 = DLookUp("[VALUE]","[QueryName]")

How can I get:
Value2 = the second VALUE listed in the query
Value3 = the third VALUE listed in the query
Value4 = the forth VALUE listed in the query
Value5 = the fifth VALUE listed in the query

Any help would be appreciated!
Thanks
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top