Add Record command button

Matizo

Registered User.
Local time
Today, 13:02
Joined
Oct 12, 2006
Messages
83
Hello,

What I need is command button which will add a record to one of my tables. The problem is that first what i have to do is select a student in the list box and then use a button which will add a rcord to student attendace table for that selected student. It should also be possible to add multiple records (if all students are selected).

How to create a macro/ or what is a code for it?


Thanks in advance,

matt
 
This is what you need:

PHP:
    With CurrentDb.OpenRecordset("table_name")
        .AddNew
        ![field1] = value
        ![field2] = Forms![Form]![txtbox]
        ![field3] = Now()
        .Update
        .Close
    End With
 
can i create a macro which will run that code?
I'm beginner in this programming things so i'm not really sure where should i include this code.
Does that code add records just for the selected students?

Thanks,
 
I have used this code:

Private Sub cmdAddAtt_Click()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim var As Variant

Set db = CurrentDb
Set rs = db.OpenRecordset("tblAttendance")

For Each var In Me.lstStudents.ItemsSelected
rs.AddNew
rs.Fields("StudentID") = var
rs.Fields("AttendanceDate") = Me.txtToday
rs.Update
Next
rs.Close

MsgBox "Records Updated"

Set rs = Nothing
Set db = Nothing

End Sub


It works for some of the students but for most of them I get this error:

You cannot add or change a record because a related record is required in table "tblStudents"

What should I change?

P.S: I attached my database

Regards,
 

Attachments

I've looked at your database. The problem is that you dont identify the student. I suggest you make a form based on the student table, and use the dropdown/menu to chose a record. Then you can identify the student, and transfer the right StudentID to the new record.

I think the code I suggested is easier to work with. Try it:

PHP:
With CurrentDb.OpenRecordset("tblAttendance")
        .AddNew
        ![StudentID] = Me.txtStudentID
        ![AttendanceDate] = Me.txtToday (Why not use "Date()"?)
        .Update
        .Close
End With
 
I will try the code you suggested. The dropdown menu is not the best idea because I could not select all students at once. I tried to based my database on the example of database that I have found on this forum. I have attached this database and I would really appriciate if you could take a look on that and tell me how they have done it. I tried to understand that be the code is much more complex.

Thanks a lot,
 

Attachments

I'm afraid that adding multiple record based opon serveral selections is "out of my league", but I'm sure some of the other guys here can help you!
 
Ok, no problem :) thanks for help anyway,

Regards,
Matt
 

Users who are viewing this thread

Back
Top Bottom