Hi. Can anyone help me with this code?

I have a form which takes subject name, semester and class. I put this data in a database tables using 2 ADODC’s and the connections are as follows:

ADODC3- LecturesAttended (StudentID, RollNo, Name, SubjectName)
ADODC4- Student (StudentID, RollNo, Name,Class)

I take studentid,rollno,name from student table and put it in the LecturesAttended table and assign the subject to each student.
Now the problem is that my code isn’t assigning the subject to the last student in Student table. If there are 5 students then it adds only 4students in LecturesAttended table.
Is something wrong my for loop?

Adodc3.RecordSource = "select * from LecturesAttended"
Adodc4.RecordSource = "select * from Student where Class='" + cmbClass.Text + "' "
        
        Dim count As Integer
        For count = 0 To Adodc4.Recordset.RecordCount 
        
        Adodc3.Recordset.AddNew
        Adodc3.Recordset.Fields("StudentID") = Adodc4.Recordset.Fields("StudentID")
        Adodc3.Recordset.Fields("Name") = Adodc4.Recordset.Fields("Name")
        Adodc3.Recordset.Fields("RollNo") = Adodc4.Recordset.Fields("Roll_No")
        Adodc3.Recordset.Fields("Semester") = UCase(cmbSemester.Text)
        Adodc3.Recordset.Fields("SubjectName") = UCase(txtSubject.Text)
        Adodc4.Recordset.MoveNext
        'If Adodc4.Recordset.EOF = True Then
        'MsgBox "Details added.", vbInformation, "Add Subject"
         ' Exit Sub
        'End If
        Next count
        MsgBox "Details added.", vbInformation, "Add Subject"
        
        Unload Me

I tried all combinations in for loop.
Like if I change the for loop to this :

For count = 0 To Adodc4.Recordset.RecordCount – 1

Then it adds the last entry but with errors. Like EOF or BOF is true and requested operation needs a current record.

Try the following -

Dim count As Integer
For count = 0 To Adodc4.Recordset.RecordCount -1

If Adodc.Recordset.EOF = True Then
Exit For
Else
'All the other code here
End If

'An easier way is to use a do while loop

Do While Adodc4.Recordset.EOF = False
'All code here
Loop

Thanks for replying. I tried both the ways but its still not working. It doesn't add 1 entry.

Which record is excluded from the table, the first record or the last record?

The first record in the table is excluded.. That is the last entry i made of a student.

Try the following -

Adodc4.Recordset.MoveFirst

Do While Adodc4.Recordset.EOF = False
'All code here
Loop

I might have the Adodc3/4 mixed up. Move the recordset that you are copying from to the first record, then run the loop to the eof action.

Hey all the codes you provided are correct.

The problem is with my database.
I used DataGrids on my form to see if the data is getting added correctly. In the DataGrid it shows the records but in my database it shows one record less.

but in my database it shows one record less.

I'm not sure what you mean by this. If a record is not showing in the database itself, it does not exist.:)

I'll provide a screenshot of the form and database for better understanding.

The students from the Student table are assigned the subject and the entries go in the LecturesAttended table once i click Add.

But check out the DataGrid of LecturesAttended table and the database table below. It doesn't add or show one entry in my database table.

What I could gather from your pic -

You have the same student id number but different names. If studentid is a primary key, this will cause errors. The best is for you to zip your app and post it here, let me have a look at all the code and the database itself.

I had missed the update statement. So problem is solved now.

Thanks!!

Hehehe, funny how a small thing can make you pull out your hair.:)

Well done and happy coding.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.