How to load a multiline recordset into an Access form with VBA

Julia

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

Table and Form

June7

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:

  1. run an UPDATE action SQL

  2. 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

  3. code physically moves to each record on form and modifies field value

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Load Recordset Values into Access Form

VBA Access - Empty Recordset (error 3021) in Form.Current event

VBA in MS Access: how to accelerate a loop operation on a recordset

Access VBA - Error on checking recordset

Setting the Recordset property of an Access form

Error setting VBA Recordset of form from ADODB.recordset

Access VBA: What is the difference between dao.recordset vs recordset?

How to get the rows of a RecordSet in VBA

Access VBA set recordset of lists not working

Why is a loop needed to change recordset in Access VBA?

MS access copying recordset using vba

Export Access VBA Recordset To Single Row In Excel

Datatype mismatch when reading recordset in vba access

Access - VBA/SQL Recordset loop through not updating

access vba array by looping through recordset

Access 2013 - VBA - Recordset Insert getting ID

Access 2016 VBA .FindFirst is not finding record in recordset

Access VBA: Editing a RecordSet in a continuous subform

Access VBA - how can i convert a value in recordset from text to number?

Access VBA: Attachment image won't load in form

How to load multiline records into dataframe

How to find whether Recordset is empty is not in VBA?

How to define Recordset.GetRows parameters in VBA?

Access VBA Find Records using Search Critera in Recordset

Recordset in excel vba returns nothing while data is present in Access DB

access vba code returns empty recordset despite table having records

MS ACCESS VBA recordset search criteria type mismatch

Access VBA: Recordset doesn't work with a specific query?

Running two SQL queries in VBA MS Access. The second recordset depends on the first recordset