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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

associate the result of a query into a field in a form

Status
Not open for further replies.

Weendigo

Technical User
Jul 3, 2002
6
BR
I am doing a query using a macro, that gets the parameter using a filter (it is a typed field by a user in a form).

The macro opens a query that pop up on the screen (always one line and two fields) , and i need to associate the text of one of these fields (of query that pop up) into the field of my form.

How can i do it using VBA, after a user has typed [ENTER] (i know it is in the exit event), but no idea about the code i´ll use.

Thank you.
 
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset(&quot;<query>&quot;)
rst.MoveFirst
Me.<control name> = rst!<qry field name>

This is good if you know there will always be something there. If the query might be empty, you'll need to add If Not rst.EOF after Set rst....
 
Thank You TrojanRabbit, it is almost working... but access give me some error (3061 - insufficient parameters) while i am trying to run this code:

Set rst = CurrentDb.OpenRecordset(&quot;SELECT PLACA FROM PLACA_E_ID WHERE PLACA = Forms!INCLUIR_VC3!PLACA;&quot;)
rst.MoveFirst
Me.IDENT = rst!IDENT

<WHERE PLACA = Forms!INCLUIR_VC3!PLACA> is not accepted by the program, it only accepts hardcoded values, like:

<WHERE PLACA = 'KRE3700';&quot;)

How can i solve this trouble if my data is typed in a field ?

Thank You in advance
 
Thats all ok... I think a bit more and found...
This way it works.. Thanks very much everybody here...

Private Sub txtPlaca_Exit(Cancel As Integer)

Dim rst As Recordset

Set rst = CurrentDb.OpenRecordset(&quot;SELECT * FROM FROTA5 WHERE PLACA = '&quot; & txtPlaca & &quot;'&quot;)
rst.MoveFirst
Me.txtIDENT = rst!IDENT

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top