Run-time error '3061': Too Few Parameters. Expected 2 - ms-access

So I'm trying to teach myself VBA again and I'm having a cople of troubles. I'm trying to add new users to a table but keep getting the above error when I click my "Update" button. The text field will be in the form of 2 letters and 5 numbers. XX11111 for example.
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtLoginName.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
Else
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmModifyUsersSub.Form.Requery
End Sub

you are missing a ' in this line:
" set LoginName=" & Me.txtLoginName & "'" & _
change it to
" set LoginName='" & Me.txtLoginName & "'" & _
and :
" WHERE LoginName=" & Me.txtLoginName.Tag
to
" WHERE (LoginName='" & Me.txtLoginName.Tag & "')" ' and I don't know if this your intended where condition.

The error pretty much gives you the answer. You need 2 parameters for the function it is failing on. One thing to try is to change
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
To
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')",dbFailOnError
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName='" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName='" & Me.txtLoginName.Tag & "'", dbFailOnError

Related

I got syntax error in update statement.Run-time error: 3144 I use the following code

Private Sub cmdAdd_Click()
'when we Click on button Add there are two options
'1. for insert
'2. for update
If Me.txtdate.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO BphCore1(DateOfTesting, TestingAt, VoltageLevel, PolarityCheck, AppliedVoltageInPrimary, AppliedVoltageInSecondary, MeasurmentUnitIR, MinimumValue, PrimaryToEarth, PrimaryToSecondary,SecondaryToEarth, MeasurmentUnitWR, WRSecondaryValues, 20OfInjectedValue, 20OfMeasurmentValue, 40OfInjectedValue, 40OfMeasurmentValue, 60OfInjectedValue, 60OfMeasurmentValue, 80OfInjectedValue, 80OfMeasurmentValue, 100OfInjectedValue, 100OfMeasurmentValue, KneePointVoltage, MagnetizingCurrent) " & _
" VALUES('" & Me.txtdate & "','" & Me.cbotest & "','" & Me.cbovl & "','" & Me.cbopc & "','" & Me.cboavp & "','" & Me.cboavs & "','" & Me.cbomuir & "','" & _
Me.txtMV & "','" & Me.txtpe & "','" & Me.txtps & "','" & Me.txtse & "','" & Me.cbomuwr & "','" & Me.txtwrsv & "','" & Me.txti20 & "','" & Me.txtm20 & "','" & Me.txti40 & "','" & Me.txtm40 & "','" & Me.txti60 & "','" & Me.txtm60 & "','" & Me.txti80 & "','" & Me.txtm80 & "','" & Me.txti100 & "','" & Me.txtm100 & "','" & Me.txtkpv & "','" & Me.txtmc & "')"
Else
'otherwise (Tag of txtdate store the DateOfTesting of BphCore1 to be modified)
CurrentDb.Execute " UPDATE BphCore1 " & _
" SET DateOfTesting=" & Me.txtdate & _
", TestingAt='" & Me.cbotest & "'" & _
", VoltageLevel='" & Me.cbovl & "'" & _
", PolarityCheck='" & Me.cbopc & "'" & _
", AppliedVoltageInPrimary='" & Me.cboavp & "'" & _
", AppliedVoltageInSecondary='" & Me.cboavs & "'" & _
", MeasurmentUnitIR='" & Me.cbomuir & "'" & _
", MinimumValue='" & Me.txtMV & "'" & _
", PrimaryToEarth='" & Me.txtpe & "'" & _
", PrimaryToSecondary='" & Me.txtps & "'" & _
", SecondaryToEarth='" & Me.txtse & "'" & _
", MeasurmentUnitWR='" & Me.cbomuwr & "'" & _
", WRSecondaryValues='" & Me.txtwrsv & "'" & _
", 20OfInjectedValue='" & Me.txti20 & "'" & _
", 20OfMeasurmentValue='" & Me.txtm20 & "'" & _
", 40OfInjectedValue='" & Me.txti40 & "'" & _
", 40OfMeasurmentValue='" & Me.txtm40 & "'" & _
", 60OfInjectedValue='" & Me.txti60 & "'" & _
", 60OfMeasurmentValue='" & Me.txtm60 & "'" & _
", 80OfInjectedValue='" & Me.txti80 & "'" & _
", 80OfMeasurmentValue='" & Me.txtm80 & "'" & _
", 100OfInjectedValue='" & Me.txti100 & "'" & _
", 100OfMeasurmentValue='" & Me.txtm100 & "'" & ", KneePointVoltage='" & Me.txtkpv & "'" & ", MagnetizingCurrent='" & Me.txtmc & "'" & _
" WHERE DateOfTesting=" & Me.txtdate.Tag
End If
'clear form
cmdclean_Click
'refresh data in the list on form
BphCore1subfrm.Form.Requery
End Sub
Parameters for date/time fields use # delimiter, parameters for text fields use apostrophe delimiter, parameters for number fields do not require delimiter.
" VALUES(#" & Me.txtdate & "#,'"
" SET DateOfTesting=#" & Me.txtdate & "#" & _
" WHERE DateOfTesting=#" & Me.txtdate.Tag & "#"

Insert Into Statement VBA [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hi everyone I am coding in VBA using Access 2013 I wrote my code two different ways and keep getting a syntax error. Here's my code.
Private Sub cmdAdd_Click()
CurrentDb.Execute "INSERT INTO GroupVolunteers(Group, Leader, Name,
phone, email, EmergencyContact, EmergencyContact) " & _
" VALUES(" & Me.txtGroup & "','" & Me.cboLeader & "','" & Me.txtName & "','"
& Me.txtEmail & "','" & Me.txtPhone & "','" & Me.txtEmergencyContact & "','"
& Me.EmergencyNumber & "','" & Me.txtRegNumber & "')"
'clear form
cmdClear_Click
'refresh data in list on form
frmStudentSub.Form.Requery
End Sub
OR
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtRegNumber & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO GroupVolunteers(Group, Leader, Name,
phone, email, EmergencyContact, EmergencyContact) " & _
" VALUES(" & Me.txtGroup & "','" & Me.cboLeader & "','" &
Me.txtName & "','" & _
Me.txtEmail & "','" & Me.txtPhone & "','" &
Me.txtEmergencyContact & "','" & Me.EmergencyNumber & "','" &_
Me.txtRegNumber & "')"
Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE GroupVolunteers " & _
" SET Group=" & Me.txtGroup & _
", leader='" & Me.cboLeader & "'" & _
", name='" & Me.txtName & "'" & _
", email='" & Me.txtEmail & "'" & _
", phone='" & Me.txtPhone & "'" & _
", EmergencyContact='" & Me.txtEmergencyContact & "'" & _
", EmergencyNumber='" & Me.txtEmergencyNumber & "'" & _
", NumberVolunteers ='" & Me.txtNumberVolunteers & "'" & _
" WHERE RegNumber = " & Me.txtRegNumber.Tag
" VALUES(" & Me.txtGroup & "', ...
Think of how that's going to end up in your statement:
VALUES(<Me.txtGroup>', ...
In other words, you're either missing the opening quote for a character-type column or you have too many for a numeric-type column. It should be one of:
" VALUES('" & Me.txtGroup & "', ... // for character-type column
" VALUES(" & Me.txtGroup & ", ... // for numeric-type column
That should fix your insert in both code blocks, you may also want to examine the update in the second code block as well. It has no quotes on the group column which is okay if it's numeric-type but probably not if it's character-type.

Microsoft access "data type mismatch in criteria expression" error

I have written code to add and edit data in a table but I am getting "data type mismatch in criteria expression".
Private Sub Submit_Click()
If Me.txtid.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO Client(Client_ID,Title,First_Name,Last_Name,Employer,DOB,Updated_Date,Email,Phone_Number,Mobile_Number,Address_Street,Date_of_1st_Session,Presenting_Issue,Next_Of_Kin,EAP_Number,Status,History)" & _
" VALUES('" & Me.txtid & "','" & Me.txt_title & "','" & Me.txtname & "','" & Me.txtlastname & "','" & Me.txtorg & "','" & Me.CreatedDate & "','" & Me.UpdatedDate & "','" & Me.txtEmail & "', '" & Me.txtphone & "','" & Me.txtmb & "', '" & Me.txtadd & "','" & Me.txtdate & "', '" & Me.txtissue & "','" & Me.txtnext & "','" & Me.txtEAP & "','" & Me.Combo45 & "','" & Me.txtHistory & "')"
Else
CurrentDb.Execute "UPDATE Client" & " SET Client_ID=" & Me.txtid & ", Title='" & Me.txt_title & "'" & ", First_Name='" & Me.txtname & "'" & ", Last_Name='" & Me.txtlastname & "'" & ", Employer='" & Me.txtorg & "'" & ", DOB='" & Me.CreatedDate & "'" & ", Updated_Date='" & Me.UpdatedDate & "'" & ", Email='" & Me.txtEmail & "'" & ", Phone_Number='" & Me.txtphone & "'" & ", Mobile_Number='" & Me.txtmb & "'" & ", Address_Street='" & Me.txtadd & "'" & ", Date_of_1st_Session='" & Me.txtdate & "'" & ", Presenting_Issue='" & Me.txtissue & "'" & ", Next_Of_Kin='" & Me.txtnext & "'" & ", EAP_Number='" & Me.txtEAP & "'" & ", Status='" & Me.Combo45 & "'" & ", History='" & Me.txtHistory & "'" & " WHERE Client_ID=" & Me.txtid.Tag
End If
Command39_Click
Me.txtid.SetFocus
Client_subform.Form.Requery
End Sub
You seem to be treating all the values as strings ('" & Me.txtid & "')when some of the data items may be numeric or dates.
Try ( " & Me.txtid & " ) if it's numeric or (#" & Me.CreatedDate & "#)

How to insert correct value from DropDownList into Database Table

I have a form where an admin can add a company to a database table called 'Company'
In this form there is a Drop Down list to select whether a company is a Scholarship Company, there are two options 'Yes' and 'No'. I have created a table to pull these options from called 'YesNo'.
When I select Yes in the drop down list and hit submit it updates the table so the cell for ScholarshipCompany is '1' and not 'Yes'. This works fine in the edit form I have which just updates the company though I am having trouble with the add company form which inserts a new entry into the table.
Please see code below;
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("Name") = Nothing Then
Response.Redirect("Login.aspx")
End If
Session("Name") = Session("Name")
Session("Username") = Session("Username")
Try
If Not IsPostBack Then
Dim locationList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT [{Location}].ID, [{Location}].Location FROM [{Location}] ORDER BY [{Location}].ID")
ddlLocation.DataSource = locationList
ddlLocation.DataTextField = "Location"
ddlLocation.DataValueField = "ID"
ddlLocation.DataBind()
locationList.close()
Dim statusList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT * FROM EmployerStatus ORDER BY ID ASC")
ddlStatus.DataSource = statusList
ddlStatus.DataTextField = "Status"
ddlStatus.DataValueField = "ID"
ddlStatus.DataBind()
statusList.close()
Dim yesnoList As OleDbDataReader = Database.DoSQLReturnDataReader("SELECT * FROM YesNo ORDER BY ID DESC")
ddlYesno.DataSource = yesnoList
ddlYesno.DataTextField = "Option"
ddlYesno.DataValueField = "Option"
ddlYesno.DataBind()
yesnoList.close()
End If
Catch ex As Exception
Response.Redirect("index.aspx?Yr=" & Replace(Request.QueryString("Yr"), "'", "''"))
End Try
End Sub
Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
Dim strSql As String = "INSERT INTO Company(" & _
"CompanyName, " & _
"URL, " & _
"Location, " & _
"EmployerStatus, " & _
"Address, " & _
"ScholarshipCompany, " & _
"ScholarshipNotes, " & _
"Town, " & _
"County, " & _
"Blurb, " & _
"InterviewTips, " & _
"HRContactForename, " & _
"HRContactSurname, " & _
"HREmail, " & _
"Telephone, " & _
"[Password], " & _
"Fax, " & _
"HRContactForename2, " & _
"HRContactSurname2, " & _
"HREmail2, " & _
"Telephone2, " & _
"HRContactForename3, " & _
"HRContactSurname3, " & _
"HREmail3, " & _
"Telephone3, " & _
"Postcode) " & _
"VALUES " & _
"( " & _
"'" & TxtBoxCompanyName.Text.Replace("'", "''") & "', " & _
"'" & TxtBoxURL.Text.Replace("'", "''") & "', " & _
"'" & Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _
"'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _
"'" & TxtAreaAddress.InnerText.Replace("'", "''") & "', " & _
"'" & Replace((ddlYesno.SelectedIndex), "'", "''") & "', " & _
"'" & TxtBoxSchol.Text.Replace("'", "''") & "', " & _
"'" & TxtBoxTown.Text.Replace("'", "''") & "', " & _
"'" & TxtBoxCounty.Text.Replace("'", "''") & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & "" & "', " & _
"'" & TxtBoxPostcode.Text.Replace("'", "''") & "'" & _
")"
Dim updateCompanyDetails As OleDbDataReader = Database.DoSQLReturnDataReader(strSql)
updateCompanyDetails.Close()
System.Threading.Thread.Sleep("1000")
Instead of
Replace((ddlLocation.SelectedIndex + 1), "'", "''") & "', " & _
"'" & Replace((ddlStatus.SelectedIndex + 1), "'", "''") & "', " & _
try
Replace((ddlLocation.SelectedValue + 1), "'", "''") & "', " & _
"'" & Replace((ddlStatus.SelectedValue + 1), "'", "''") & "', " & _
or SelectedText

Runtime error 3075 can't figure it out

I'm trying to do an insert on a button click event and I keep getting a runtime error of missing operator in query expression my query looks like the following. Any ideas?
Private Sub CmdAdd_Click()
Dim strSql As String
strSql = "INSERT INTO Current_Costs(PO_Number,Lineitemid,Capital_detail,CapitalID,GL_Number,Cost_Type,Cost_Center,Cost_cat,Item_Cost,PO_Date)" & _
" VALUES (" & Me.txtPONum & "','" & _
Me.cmbCapDetail & "','" & _
Me.cmbCapDetail.Column(1) & "','" & _
Me.txtCapID & "','" & _
Me.txtGLNum & "','" & _
Me.cmbCostType & "','" & _
Me.txtCostCen & "','" & _
Me.cmbCostCat & "','" & _
Me.txtCost & "','" & _
Me.TxtPODate & "')"
DoCmd.RunSQL strSql
i have a similar query that has the same issue and i cant see the problem
CurrentDb.Execute ("UPDATE Current_Costs " & _
"SET PO_Number='" & Me.txtPONum & "'" & _
",Lineitemid='" & Me.cmbCapDetail & "'" & _
",Capital_detail='" & Me.cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & Me.txtCapID & "'" & _
",GL_Number='" & Me.txtGLNum & "'" & _
",Cost_Type='" & Me.cmbCostType & "'" & _
",Cost_Center='" & Me.txtCostCen & "'" & _
",Cost_cat='" & Me.cmbCostCat & "'" & _
",Item_Cost='" & Me.txtCost & "'" & _
",PO_Date='" & Me.TxtPODate & "'" & _
"WHERE LineItemPOID=" & Me.txtID.Tag)
edit solved
This
" VALUES (" & Me.txtPONum & "','" & _
Is short one quote, it should be
" VALUES ('" & Me.txtPONum & "','" & _
Write your sql in a string, it makes it easier to see the problems:
strSql = "UPDATE Current_Costs " & _
"SET PO_Number='" & txtPONum & "'" & _
",Lineitemid='" & cmbCapDetail & "'" & _
",Capital_detail='" & cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & txtCapID & "'" & _
",GL_Number='" & txtGLNum & "'" & _
",Cost_Type='" & cmbCostType & "'" & _
",Cost_Center='" & txtCostCen & "'" & _
",Cost_cat='" & cmbCostCat & "'" & _
",Item_Cost='" & txtCost & "'" & _
",PO_Date='" & TxtPODate & "'" & _
" WHERE LineItemPOID=" & txtID
Dim db As database
Set db = CurrentDB
db.Execute strsql dbFailOnError
You were missing a space before WHERE and you had an unmatched parenthesis.
Consider using parameters: End of statement error