Access Too Few Parameters Error - ms-access

I have this code I have been working on to add data to a table. And there is a portion of code that is being highlighted with the error message RUN TIME ERROR 3061: TOO FEW PARAMETERS. EXPECTED 1
And I cannot figure out what the issue is and what I am missing. The following is the portion of code being highlighted.
CurrentDb.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"

Try
dim ssql as string
ssql = "INSERT INTO KWTable(KW, Source, Code) " & _
" VALUES(" & Me.text_key & ",'" & Me.txt_code & "','" & _
Me.combo_source & "')"
debug.print ssql
CurrentDb.Execute ssql

Related

SQL Inserting in to an external table

I am looking to insert data into a different table that users are using.
I think I am on the right track to piecing this together but looks like ill need a hand
My Code:
CurrentDb.Execute "INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "]" _
& "(CDP,Open_Date, Open_Time) VALUES " _
& "'" & Environ("UserName") & "', Date(), Time());"
Error
Syntax error in INSERT INTO statement
What I am trying to achieve
When a user opens the database I want to send the username, date, time to a database stored in a different location
UPDATE
I have broken the code down and got the below working, just need to work out how to get date, time in there:
CurrentDb.Execute " INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "] " _
& "(CDP) VALUES " _
& "('" & Environ("UserName") & "');"
date() and time() are functions and should not be within the quotation string. Dates and Times need to be enclosed by # signs.
CurrentDb.Execute "INSERT INTO [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblengagement & "]" _
& "(CDP,Open_Date, Open_Time) VALUES " _
& "'" & Environ("UserName") & "',#" & Date() & "#,#" & Time() & "#);"

Identify a data entry without changing the ID

I am trying to create an update form. It is supposed to take values from the text boxes on the form and update that registry using the ID's autonumber as an identifier. It says I cannot edit the ID even though I don't think I am.
Private Sub edit_Click()
'Will edit the currently selected record
CurrentDb.Execute "UPDATE DataInput " & _
" SET ID=" & Me.txtID & _
", [Date]='" & Me.Date & "'" & _
", [Time Up]='" & Me.txttimeup & "'" & _
", [Notes]='" & Me.CboNotes & "'" & _
", [Time Down]='" & Me.txtTimeDown & "'" & _
" WHERE ID=" & Me.txtID.Tag
Me.txtID.Tag = ""
'refresh data on form
DataInput_subform.Form.Requery
'Disable Update Button
Me.edit.Enabled = False
'Enable Edit Button
Me.cmdEdit.Enabled = True
'Clear texts
cmdClear_Click
Try formatting your code in a more readable way and add a debug like
Dim sSql As String
sSql = "UPDATE DataInput SET [Date]=#" & Format(Me.Date,"mm/dd/yyyy") & "#, "
sSql = sSql & "[Time Up] = '" & Me.txttimeup & "', [Notes] ='" & Me.CboNotes & "', "
sSql = sSql & "[Time Down] = '" & Me.txtTimeDown & "' "
sSql = sSql & "WHERE ID = " & Me.txtID & " ;"
Debug.Print sSql
CurrentDb.Execute sSql

Set statement error

I am receiving the 3075 error (invalid operator) from the script below. The VBA error is marking the set statement in yellow. I don't understand why. What is lacking?
strSql = "SELECT FA_AVG.RadNr, (Left([Text],122)) AS LetaEfter1, Mid([Text],130,18) AS LetaEfter2, Right(Left([Text],184),36) AS LetaEfter3, FA_AVG.PerNr, FA_AVG.Fil, FA_AVG.GR1 " & _
"FROM FA_AVG " & _
"WHERE (((FA_AVG.RadNr)=20) AND ((Left([Text],122))='" & strTemp1 & "') AND ((Right(Left([Text],184),36))='" & strTemp3 & "') AND (Trim(FA_AVG.PerNr)='" & strPer & "'))"
Set rstAddData = CurrentDb.OpenRecordset(strSql)
Run this and study the output, and it will probably be quite clear to you why it won't run:
strSql = "SELECT FA_AVG.RadNr, (Left([Text],122)) AS LetaEfter1, Mid([Text],130,18) AS LetaEfter2, Right(Left([Text],184),36) AS LetaEfter3, FA_AVG.PerNr, FA_AVG.Fil, FA_AVG.GR1 " & _
"FROM FA_AVG " & _
"WHERE (((FA_AVG.RadNr)=20) AND ((Left([Text],122))='" & strTemp1 & "') AND ((Right(Left([Text],184),36))='" & strTemp3 & "') AND (Trim(FA_AVG.PerNr)='" & strPer & "'))"
Debug.Print strSQL

MS Access using UPDATE statement keeps entering new data

I'm using an UPDATE Statement but whenever I click the Edit button then Update, it's entering a new line but with the same data.
My code:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtNumber.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblcompany (companyname, companyaddress, contactnumber, contactperson, emailaddress, website, plantlocation, projectinfo, consultant) " & _
" VALUES('" & Me.txtCompanyName & "','" & _
Me.txtCompanyAddress & "','" & Me.txtContactNumber & "','" & _
Me.txtContactPerson & "','" & Me.txtEmailAddress & "','" & _
Me.txtWebsite & "','" & Me.txtPlantLocation & "','" & _
Me.txtProjectInfo & "','" & Me.txtConsultant & "')"
Else
'otherwise (tag of txtNumber store the number of company to be modified)
CurrentDb.Execute "UPDATE tblcompany " & _
" SET companyname='" & Me.txtCompanyName & "''" & _
", companyaddress='" & Me.txtCompanyAddress & "''" & _
", contactnumber='" & Me.txtContactNumber & "'" & _
", contactperson='" & Me.txtContactPerson & "''" & _
", emailaddress='" & Me.txtEmailAddress & "'" & _
", website='" & Me.txtWebsite & "'" & _
", plantlocation='" & Me.txtPlantLocation & "''" & _
", projectinfo='" & Me.txtProjectInfo & "''" & _
", consultant='" & Me.txtConsultant & "''" & _
" WHERE number=" & Me.txtNumber.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmCompanySub.Form.Requery
End Sub
Isn't the Tag property empty by default? if you're saving a new record you will have to set a tag property equal to the number. So that when you come to update a record the Where number = & me.txt.number.tag is true. Otherwise all record tags of "" will equal "".
Also, tab in on your currentDb.execute line (after then).

How to make text entry into table based on clicking an button in forms in Access 2010

I have created a form with submit buttons on it.
I have entered the data in the text box and then clicked on submit button.But the data is not getting saved in the table.Also,it is not showing any error message. It is not working at all.
Private Sub CmdAddNew_Click()
'add data to table
CurrentDb.Execute "INSERT INTO tblemployee(firstname,lastname,Address,city)" & _
" VALUES('" & Me.txtfirstname & "','" & Me.txtlastname & "','" & Me.txtaddress & "','" & Me.txtcity & "')"
try this:
Private Sub CmdAddNew_Click()
Dim dbs As DAO.Database, Sqltext As String, iCount As Integer
Set dbs = CurrentDb
Sqltext = "INSERT INTO tblemployee(firstname,lastname,Address,city) " & _
"VALUES('" & Me.txtfirstname & "','" & Me.txtlastname & _
"','" & Me.txtaddress & "','" & Me.txtcity & "');"
Debug.Print "SQL statement generated with variables:" & vbCrLf & Sqltext
dbs.Execute Sqltext, dbFailOnError
iCount = dbs.RecordsAffected
Debug.Print "..." & iCount & " row(s) inserted"
End Sub
The debug.print messages will print to the immediate window (Ctrl+g) to view from VBA editor, you can delete them if you want to once you have confirmed it's working.