Beginner Trying to Add to Recordset

Aspire

Registered User.
Local time
Today, 15:16
Joined
Apr 25, 2012
Messages
12
Hello there! Thank you for taking a look at my question.

I have a table, refEmployees, with a key of EmployeeID and a date field of ADM001Test.
On the form where a button calls the function to add the date, the EmployeeID is set as a hidden field [Forms]![frmTrainingModule]![cboUserName] which gives the correct EmployeeID.

I need to add today's date to [ADM001Test] for the correct EmployeeID row.
My recordset .AddNew VBA knowledge is lacking because I can't seem to grasp the concept. Here is my flailing attempt at the code - obviously it does not work.
Thank you very much for your help!

Scott

Dim rs As DAO.Recordset
Dim strSQL As Date
Dim IntFieldIndex As Integer
strSQL = [ADM001Test]
IntFieldIndex = [Forms]![frmTrainingModule]![cboUserName]
Set rs = CurrentDb.OpenRecordset(strSQL)
With rs
.AddNew
.Fields("ADM001Test") = Date
.Update
End With
rs.Close
Set rs = Nothing
 
The strSQL needs to be a valid SQL string such as "Select * from ADM001Test"
Dim SQL as String (not Date)
Conversely, the openrecordset argument could be the name of a table as shown below.
Quick example:

Code:
Dim dbVideoCollection As DAO.Database
   Dim rstVideos As DAO.Recordset
   Set dbVideoCollection = CurrentDb
   Set rstVideos = dbVideoCollection.OpenRecordset("Videos") ' name of a table
   rstVideos.AddNew
   rstVideos("Title").value = "Cape Fear"
   rstVideos("Director").value = "Martin Scorsese"
   rstVideos("CopyrightYear").value = 1991
   rstVideos("Length").value = "2 Hours 8 Mins"
   rstVideos("Rating").value = "R" ' Pirate video rating otherwise PG
   rstVideos.Update
Be sure to look at this sites Demo Databases. You can spend hours. Search it for an ".addnew" and you might just find dozens of examples to evaluate.
Also, be sure to follow up with success because it will help many others who are looking for the same advice.
 
Last edited:
Thank you for that example. I saw something like it online, but didn't make sense the way your posting does. I will try using this framework.
Thanks again!
 

Users who are viewing this thread

Back
Top Bottom