I'm trying to UPDATE my table in Access VBA - ms-access

If Me.dcBox & "" = "" Then
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = """ & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = """ & _
" WHERE [Order] = " & Me.orderBox.Tag & ""
Else
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = #" & Me.drBox & "#" & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = #" & Me.dcBox & "#" & _
" WHERE [Order] = " & Me.orderBox.Tag & ""
End If
I'm trying to UPDATE my table in Access VBA, but I found it causes Runtime Error: 3075 'Syntax error in date in query expression '#' when I try to UPDATE [Date Completed] field from what was blank previously.
And I've set Me.probBox as some text value which was previously blank, but [Problem] field doesn't be updated. What is surprising is there is no error coming out for this one.

Try this
CurrentDb.Execute "UPDATE worklogData " & _
" SET [Submitter] = '" & Me.subBox & "'" & _
", [Section] = '" & Me.secBox & "'" & _
", [Received By] = '" & Me.rbBox & "'" & _
", [Date Received] = """"" & _
", [Serial Number] = '" & Me.snBox & "'" & _
", [Problem] = '" & Me.probBox & "'" & _
", [Computer Name] = '" & Me.cnBox & "'" & _
", [MAC] = '" & Me.macBox & "'" & _
", [Solution] = '" & Me.solBox & "'" & _
", [Completed By] = '" & Me.cbBox & "'" & _
", [Date Completed] = """"" & _
" WHERE [Order] = " & Me.orderBox.Tag & ""

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 & "#"

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 & "#)

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

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

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

End of statement error

I am trying to assemble an insert statement with multiple line breaks. The insertion includes both numerical as well as textual data. I keep getting an error, and I cannot find the where I am syntactically wrong. Can anyone see me error?
CurrentDb.Execute "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
"GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
"LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
"ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
"QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
"VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", "" & _
" & Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", "" & _
" & Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", "" & _
" & Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", "" & _
" & Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", "" & _
" & Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '"" & _
" & Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', "" & _
" & Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", "" & _
" & Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", "" & _
" & Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
I am not sure what the extra quotes and ampersands are for, but they are causing the problem. They occur at the start and end of each Value line. It is best to put your string in a separate variable, and then execute that, it is easier to spot problems. You might also like to consider parameters, they will make your life easier in this case.
ssql = "INSERT INTO tblCustParts (CustPartNum,CustomerID,Alloy,Temper,Finish,FormType,Gauge,GaugeAimPlus,GaugeAimMinus," & _
"GaugeGuarPlus,GaugeGuarMinus,Width,WidthAimPlus,WidthAimMinus,WidthGuarPlus,WidthGuarMinus,Length,LengthAimPlus,LengthAimMinus," & _
"LengthGuarPlus,LengthGuarMinus,Diameter,DiameterAimPlus,DiameterAimMinus,DiameterGuarPlus,DiameterGuarMinus,CircleShear,IDMin," & _
"ODMin,ODMax,LabelAs,ProduceAs,ShopPaperNotes,EyeOrientation,CoreType,PackingNotes,AQ,PaperInterleave,HeatTreatedSkids,HasRecipeFlag," & _
"QRRExists,TensionLevel,MSKCoreSpec,ChemCertsReq,PhysCertsReq,PhysAndChemCertsReq,AAStandard,ASTM_B209_10)" & _
"VALUES('" & Part & "', " & Me.Customer & ", '" & Alloy & "', '" & Temper & "', '" & Finish & "', '" & FormType & "', " & Me.Gauge & ", " & _
Me.Gauge & ", " & Me.GaugeAimPlus & ", " & Me.GaugeAimMinus & ", " & Me.GaugeGuarPlus & ", " & Me.GaugeGuarMinus & ", " & _
Me.WidthEntry & ", " & Me.WidthAimPlus & ", " & Me.WidthAimMinus & ", " & Me.WidthGuarPlus & ", " & Me.WidthGuarMinus & ", " & _
Me.LengthEntry & ", " & Me.LengthAimPlus & ", " & Me.LengthAimMinus & ", " & Me.LengthGuarPlus & ", " & Me.LengthGuarMinus & ", " & _
Me.Diameter & ", " & Me.DiameterAimPlus & ", " & Me.DiameterAimMinus & ", " & Me.DiameterGuarPlus & ", " & _
Me.DiameterGuarMinus & ", " & Me.CS & ", " & Me.IDMin & ", " & Me.ODMin & ", " & Me.ODMax & ", '" & Me.LabelAs & "', '" & _
Me.ProduceAs & "', '" & Me.ShopPaperNotes & "', '" & Me.EyeOrientation & "', '" & Me.CoreType & "', '" & Me.PackingNotes & "', " & _
Me.AQ & ", " & Me.PaperInterleave & ", " & Me.HeatTreatedSkids & ", " & Me.HasRecipeFlag & ", " & Me.QRRExists & ", " & _
Me.TensionLevel & ", " & Me.MSKCoreSpec & ", " & Me.ChemCertsReq & ", " & Me.PhysCertsReq & ", " & Me.PhysAndChemCertsReq & ", " & _
Me.AAStandard & ", " & Me.ASTM_B209_10 & ")"
Dim db As database
Set db = CurrentDB
db.Execute ssql dbFailOnError
Last record inserted ID
Set rs = db.OpenRecordset("select ##identity as id")
LastID = rs("id")
Parameter example:
ssql = "INSERT INTO Table1 (Atext,Anumber) Values (#AText,#Anumber)"
Dim qdf As QueryDef
Set qdf = CurrentDb.CreateQueryDef("", ssql)
qdf.Parameters("#atext") = "abc"
qdf.Parameters("#Anumber") = 1
qdf.Execute dbFailOnError
Last record inserted ID
qdf.SQL = "select ##identity as id"
Set rs = qdf.OpenRecordset
LastID = rs("id")