I'm runing a query (which is returning the correct data) but how can I loop the recordset and add each line in the form with VBA? Because it's currently adding the last line repeatedly.
Private Sub buscarReservasFio()
Dim strSQL As String
strSQL = "SELECT * FROM tblReservasFio WHERE fk_solicitacao = " & "'" & arrParameters(0) & "'"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
If rs.BOF Or rs.EOF Then
'to-do insert
MsgBox ("Insert")
Else
Do Until rs.EOF
DoCmd.GoToRecord , , acNewRec
Me.ds_funcao = rs.Fields("ds_funcao").Value
Me.fk_titulo = rs.Fields("fk_titulo").Value
Me.nr_cor = rs.Fields("nr_cor").Value
Me.nr_peso = rs.Fields("nr_peso").Value
Me.dt_liberacao = rs.Fields("dt_liberacao").Value
Me.dt_baixa = rs.Fields("dt_baixa").Value
rs.MoveNext
Loop
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
Apparently, form is bound to table but your code is populating UNBOUND controls. That's the only way I can replicate your output - which should be expected. The reason multiple rows of same data show is because controls (BOUND or UNBOUND) repeat for each record and since there is only one set of controls, the same data shows in UNBOUND controls for all rows - last record from recordset loop. Step debug and watch data change on form. The GoToRecord action is meaningless because no data is input to BOUND controls. The result is same without that line.
In order to display multiple records from table, bind controls to fields. Then, options to modify nr_peso field with new calculated value when form first opens:
run an UPDATE action SQL
open a recordset of form's RecordsetClone and loop through that recordset and change value of nr_peso field - changes in RecordsetClone will immediately reflect in the form
code physically moves to each record on form and modifies field value
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments