I'm trying something with VBA in Access but I cannot see what I'm doing wrong.
I have in a form a text control (numeric) where I can write some number. What I want is when I clic on a button in the form, cretat as many records in another table (not the source of the form) as the number in the text control.
I'm trying this, but it doesn't do anything:
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Table")
If Not rst.EOF And rst.BOF Then
Do While rst.RecordCount < Me.Txt_Control
rst.Edit
rst.Fields("Field_01") = "Example 1"
rst.Fields("Field_02") = "Example 2"
rst.Fields("Field_03") = "Example 3"
rst.Update
rst.MoveNext
Loop
rst.Close
End If
Can someone give me a hand?
Regards.
You're editing one existing record, not adding new ones. Also, your code may run into trouble since you're opening a recordset with possible records present.
You can use the following:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("SELECT * FROM Table WHERE 1=0") 'Open the table, but don't select anything
Do While rst.RecordCount < Me.Txt_Control
rst.AddNew 'Add a new record
rst.Fields("Field_01") = "Example 1"
rst.Fields("Field_02") = "Example 2"
rst.Fields("Field_03") = "Example 3"
rst.Update
Loop
rst.Close