Marchello2004
Technical User
Hi everyone! Anyone able to add records to the database using VBA?
I started using 'DoCmd.GoToRecord , , acNewRec
to generate the new ID for a new record, but this doesnt seem to add the ID number directly into the database, instead I had to create a textfield in which to write this ID, and then the data appears in the table, WIERD! anyway, instead I'm trying to use this VBA code, but the keeps coming up with a messange saying ODBC - call failed!
Private Sub Submit_form_Click()
On Error GoTo Err_Submit_form_Click
Dim ID As ADODB.Recordset
Set ID = New ADODB.Recordset
ID.Open "Bookings", CurrentProject.Connection, adOpenStatic, adLockOptimistic
If ID.Supports(adAddNew) Then
With ID
.AddNew
.Fields("staff_name").Value = Name
.Update
End With
End If
ID.close
Set ID = Nothing
DoCmb.close
Exit_Submit_form_Click:
Exit Sub
Err_Submit_form_Click:
MsgBox Err.description
Resume Exit_Submit_form_Click
End Sub
It seems to me that there is something missing in the connection to the database, but I can't figure out what it is...
Anyone used this before? Any ideas? I'm not using Access Project, is just plain access...
Thank you!!
I started using 'DoCmd.GoToRecord , , acNewRec
to generate the new ID for a new record, but this doesnt seem to add the ID number directly into the database, instead I had to create a textfield in which to write this ID, and then the data appears in the table, WIERD! anyway, instead I'm trying to use this VBA code, but the keeps coming up with a messange saying ODBC - call failed!
Private Sub Submit_form_Click()
On Error GoTo Err_Submit_form_Click
Dim ID As ADODB.Recordset
Set ID = New ADODB.Recordset
ID.Open "Bookings", CurrentProject.Connection, adOpenStatic, adLockOptimistic
If ID.Supports(adAddNew) Then
With ID
.AddNew
.Fields("staff_name").Value = Name
.Update
End With
End If
ID.close
Set ID = Nothing
DoCmb.close
Exit_Submit_form_Click:
Exit Sub
Err_Submit_form_Click:
MsgBox Err.description
Resume Exit_Submit_form_Click
End Sub
It seems to me that there is something missing in the connection to the database, but I can't figure out what it is...
Anyone used this before? Any ideas? I'm not using Access Project, is just plain access...
Thank you!!