Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Records to a table in access 2000

Status
Not open for further replies.

12369874

MIS
Nov 30, 2001
12
GB
Hi, I want to be able to insert 5 records from a form into a table named employees, I keep getting an error message saying I cant reference a property or method unless the control has the focus. Ive included the code below. Im using Access 2000

Private Sub CmdAdd_Click()
Dim dbsJH As Database
Dim rstEmployees As Recordset

Set dbsJH = CurrentDb
Set rstEmployees = dbsJH.OpenRecordset("Employees", dbOpenDynaset)


With rstEmployees

.AddNew
!ID = txtID.Value
!Surname = txtSurname.Text
!Forename = txtForename.Text
!Age = txtAge.Text
!Update = txtUpdate.Text
.Update
.Close
End With
End Sub

Thanks in advance
Paul
 
Hi,

You could always try to set the focus to the control before assigning it into the field like: It isn't great but it will work if you need a quick fix. <g>


Private Sub CmdAdd_Click()
Dim dbsJH As Database
Dim rstEmployees As Recordset

Set dbsJH = CurrentDb
Set rstEmployees = dbsJH.OpenRecordset(&quot;Employees&quot;, dbOpenDynaset)

With rstEmployees

.AddNew
!ID = txtID.Value
txtsurName.SetFocus
!Surname = txtsurName.Text
txtForename.SetFocus
!Forename = txtForename.Text
txtAge.SetFocus
!Age = txtAge.Text
txtUpdate.SetFocus
!Update = txtUpdate.Text
.Update
.Close
End With
End Sub

Have a good one!
BK
 
I think you'll find this works without needing to move the focus around all the time- assuming your text boxes appear on a form that remains open while the code runs.

.AddNew
!ID = Me.txtID
!Surname = Me.txtsurName
!Forename = Me.
!Age = Me.txtAge
!Update = Me.txtUpdate
.Update
.Close
End With

Nigel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top