Option Compare Database
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO Overtime(Todays_Date, Employee_Name, " & _
"Start_Date, End_Date,Comments) " & _
" VALUES(" & Me.txtCurrentday & ",'" & Me.txtName & "','" & _
Me.txtBegin & "','" & Me.txtEnd & "','" & Me.txtComment & "')"
Me.Refreshenter
cmdClear_Click
End Sub
Private Sub cmdClear_Click()
Me.txtCurrentday = ""
Me.txtName = ""
Me.txtBegin = ""
Me.txtEnd = ""
Me.txtComment = ""
Me.txtCurrentday.SetFocus
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Hello, I have created a Form and a Table in Microsoft Access 2010. The Form is called pbicovertime it has five unbound text boxes which all have unique names and three buttons. I would like the information that has been entered in the Form to be added to the Table called Overtime when the Add button is pressed. The code above does add the data from the Form to the table, however I get a Run-timer error '3061": Too few parameters. Expected 1 error message after closing and reopening the database. So initially everything seemed to be working fine. All the information entered in the Form was being added to the correct column in my Overtime Table. The issue took place after closing and reopening the database. I am not really sure how to proceed from this point.
FYI this is my first time working with Forms in Access !
I'm willing to bet it's this line that it's crashing on.
Specifically the
Me.txtCurrentday
, because it will be evaluated as straight text, and depending on how your PC is setup, it may be confusing SQL. e.g., it might look like this:Dates you should encompass in #'s:
and it will go smoother. Also building up the SQL that way leaves you vulnerable to injections (either as an attack or error). Imagine if the comment was "This is Susie's Job", in which case that extra apostrophe would mess up the insert.
Open your table as a recordset and add a row. That will avoid complications based on required/missing quotes or date delimiters in the values you're adding.
If the original missing parameter error was because of a misspelled field name, this code will throw an error on one of the lines between
AddNew
andUpdate
, so you should be able to quickly identify which name is misspelled.Note: Always include
Option Explicit
in the Declarations sections of your code modules. And then run Debug->Compile from the VB Editor's main menu. Correct anything the compiler complains about before you spend time troubleshooting the code.I don't know what
Me.Refreshenter
is. It looks like a misspelling ofMe.Refresh
. If so, that is somethingOption Explicit
will warn you about. However, if you wantedRefresh
, I suggest you substituteMe.Requery
. The reason is thatRefresh
will pull in changes to any of the existing rows in the form's recordset, but not newly added rows.Requery
gets new rows in addition to changes to existing rows.