S
S
shokw0w2016-10-24 13:55:55
Database
shokw0w, 2016-10-24 13:55:55

How to add the next row in the list to the table through a loop?

From the list I want to bring in order to the table the id rows. I made a loop, but it adds the first row id to all rows of the table.

Private Sub Save_Click()
Dim criteriaSQL As String
With Me.criteria
        For i = 0 To .ListCount - 1
            If .ListCount > 0 Then
                .Selected(i) = True
                criteriaSQL = " Insert Into marks(userid, criteriaid, finalgrade) values ('" & Me.student & "', '" & Me.criteria & "', '" & Me.mark & "')"
                CurrentProject.Connection.Execute (criteriaSQL)
            End If
        Next
End With
MsgBox ("Added!")
End Sub

8cb4e03043e442eb898d5ac2628deb65.png8c879750103c49c1b9d8ffb35d2a9a4a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
current, 2016-11-03
@current

Try to make such processes invisible to the user, avoid Selected(i).
If you really want to work through VBA, use

Dim mytab As ADODB.Recordset
Set mytab = New ADODB.Recordset
mytab.Open "Select..." '(тут ваш запрос)'
Do While mytab.EOF = False
'тут что хотите то и тврите с полями mytab!myField'
mytab.MoveNext
Loop

this is one of the ways in VBA
Another way is through queries:
create various queries for updates, deletions, additions. It is possible to cram some kind of series (transaction) into one request, but then you will no longer be able to use the constructor, you will have to handle it (but this is another question).
then either immediately hang up the execution of the request on the button, or if it is a series of requests, create a macro that executes this series, and then you already hang the macro on the button.
In your case, there is no need to run through the lines and insert for each line, since the data does not change and you do not do any checks,
make one query like
INSERT INTO ТаблицаДляВставки ( ПоляДляВставки через запятую )
SELECT ПоляДоноры
FROM ТаблицыДоноры
WHERE условия выборки можно тащить из формы 
ORDER BY сортировочка;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question