Not sure what is causing the error.
CurrentDb.Execute "UPDATE dbo_RootCause SET Part = '" & Me.cboPart & "', Qty = '" & Me.cboQTY & "', Code = '" & Me.cboCode & "', Reason = '" & Me.txtReason & "', RootCause = '" & Me.txtRootCause & "', CorrectiveAction = '" & Me.txtCorrectiveAction & "', CAdate = '" & Me.txtCAdate & "', CA_Comp_By = '" & Me.txtCA_Comp_By & "', ReturnInv = '" & Me.cboReturnInv & "', ReturnCust = '" & Me.cboReturnCust & "', CustFollowUp = '" & Me.cboCustFollowUp & "' WHERE ListItems = " & Me.txtListItems & "", dbSeeChanges
You need format your date value as a valid string expression, and numbers are not strings. See examples:
CurrentDb.Execute "UPDATE dbo_RootCause SET Part = '" & Me.cboPart & "', Qty = " & Str(Me.cboQTY) & ", Code = '" & Me.cboCode & "', Reason = '" & Me.txtReason & "', RootCause = '" & Me.txtRootCause & "', CorrectiveAction = '" & Me.txtCorrectiveAction & "', CAdate = #" & Format(Me.txtCAdate, "yyyy\/mm\/dd") & "#, CA_Comp_By = '" & Me.txtCA_Comp_By & "', ReturnInv = '" & Me.cboReturnInv & "', ReturnCust = '" & Me.cboReturnCust & "', CustFollowUp = '" & Me.cboCustFollowUp & "' WHERE ListItems = " & Me.txtListItems & "", dbSeeChanges
Related
still new to this, I wanted to add a combobox to my sql statement and keep getting an error. Can someone explain how to fix and how to add more combobox statements. I have about 20 columns, just testing one at a time.
Private Sub btn_Add_Click()
CurrentDb.Execute _
"INSERT INTO IPA_Raw_Data([Date], Auditor, Line_Clearance) " & _
"VALUES (#" & Format(Me!txt_Date.Value, "yyyy\/mm\/dd") & "#,'" & _
Me!txt_Name.Value & "','" & Me!cb_LC.Value & "')"
btn_Clear_Click
I was able to get it to work! I'm sorry for the delayed response and thank you guys for input. It was just syntax of the way I entered it.
Private Sub btn_Add_Click()
CurrentDb.Execute _
"INSERT INTO IPA_Raw_Data(Date_IPA, Auditor, Area, Operator, Safely, LineClearance, PPE, VerifyDoc, GVI, CompBefore, NonConf, ProcSteps, Trained, Points, Comments) " & _
"VALUES ('" & Me.txt_Date.Value & "', '" & Me!txt_Name.Value & "', '" & Me!txt_Area.Value & "', '" & Me!txt_Operator.Value & "', '" & Me!cb_Safe.Value & "', '" & Me!cb_LC.Value & "', '" & Me!cb_PPE.Value & "', '" & Me!cb_VDP.Value & "', '" & Me!cb_GVI.Value & "', '" & Me!cb_CBF.Value & "', '" & Me!cb_NC.Value & "', '" & Me!cb_ProStep.Value & "', '" & Me!cb_TrainProc.Value & "', '" & Me!txt_Points.Value & "', '" & Me!txt_Comment.Value & "')"
btn_Clear_Click
End Sub
How I can prevent duplicate values to not insert into the table. I have created a code to INSERT, UPDATE and DELETE and I want to display a MsgBox that there is a duplicate value and to cancel it. Thanks. Below you have the code:
Private Sub Command12_Click()
If Me.emID.Tag & "" = "" Then
If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
Me.emID.BorderColor = vbRed
Me.emFirst.BorderColor = vbRed
Me.emLast.BorderColor = vbRed
MsgBox "Please fill required fields", vbInformation, "Information"
Exit Sub
End If
CurrentDb.Execute "INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment)" & _
"VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "')"
MsgBox "Record Added", vbInformation, "information"
Else
CurrentDb.Execute "UPDATE tblEmployees " & _
"SET emiD =" & Me.emID & _
", first ='" & Me.emFirst & "'" & _
", last = '" & Me.emLast & "'" & _
", gender ='" & Me.emGender & "'" & _
", phone = '" & Me.emPhone & "'" & _
", mobphone ='" & Me.emMob & "'" & _
", city ='" & Me.emCity & "'" & _
", state ='" & Me.emState & "'" & _
", zip ='" & Me.emZip & "'" & _
", adress ='" & Me.emAdress & "'" & _
", email ='" & Me.emEmail & "'" & _
", comment ='" & Me.emComment & "'" & _
"WHERE emID =" & Me.emID.Tag
MsgBox "Updated!", vbInformation, "Information"
End If
Me.tblEmployees_subform.Form.Requery
End Sub
It sounds like you'd like to update an employee if one exists for the given ID otherwise you'd like to add a new employee. You can prevent adding duplicate employees by first trying to update an employee record with the given ID and if no records were updated only then do you add a new employee record.
Private Sub Command12_Click()
If (IsNull(Me.emID) Or (Me.emID = "") Or IsNull(Me.emFirst) Or (Me.emFirst = "") Or IsNull(Me.emLast) Or (Me.emLast = "")) Then
Me.emID.BorderColor = vbRed
Me.emFirst.BorderColor = vbRed
Me.emLast.BorderColor = vbRed
MsgBox "Please fill required fields", vbInformation, "Information"
Exit Sub
End If
' You must set CurrentDb to a variable otherwise the RecordsAffected
' property used later will be incorrect.
Dim db As DAO.Database
Set db = CurrentDb
' First try to update an existing employee.
db.Execute _
"UPDATE tblEmployees " & _
"SET first ='" & Me.emFirst & "', " & _
"last = '" & Me.emLast & "', " & _
"gender ='" & Me.emGender & "', " & _
"phone = '" & Me.emPhone & "', " & _
"mobphone ='" & Me.emMob & "', " & _
"city ='" & Me.emCity & "', " & _
"state ='" & Me.emState & "', " & _
"zip ='" & Me.emZip & "', " & _
"adress ='" & Me.emAdress & "', " & _
"email ='" & Me.emEmail & "', " & _
"comment ='" & Me.emComment & "'" & _
"WHERE emID =" & Me.emID.Tag & ";"
' If no records were affected by update then add a new employee.
If db.RecordsAffected = 0 Then
db.Execute _
"INSERT INTO tblEmployees(emID, first, last, gender, phone, mobphone, city, state, zip, adress, email, comment) " & _
"VALUES ('" & Me.emID & "', '" & Me.emFirst & "', '" & Me.emLast & "', '" & Me.emGender & "', '" & Me.emPhone & "', '" & Me.emMob & "', '" & Me.emCity & "', '" & Me.emState & "', '" & Me.emZip & "', '" & Me.emAdress & "', '" & Me.emEmail & "', '" & Me.emComment & "');"
MsgBox "Record Added", vbInformation, "Information"
Else
MsgBox "Updated!", vbInformation, "Information"
End If
Me.tblEmployees_subform.Form.Requery
End Sub
Note: In the update query I removed the update to the emID field since that is what the query is based on (in the WHERE clause). If the emID field is changing you won't be able to use the new emID value to find an employee record with the old emID value.
If you never want any duplicates I would also suggest that you add constraints to your database table to prevent duplicates, as suggested by Daniel Cook. I would also suggest looking into using parameterized queries instead of building SQL strings in VBA.
You can change your SQL to use IF EXISTS condition and insert only if the records does not already exists.
Your SQL may look like:
IF NOT EXISTS
(
SELECT ......
)
BEGIN
INSERT INTO tblEmployees ......<insert since employee does not exists>
END
I don't seem to find out what is wrong with my query. The values at the end are the name of the variables
Command.CommandText = (" INSERT INTO [TransfersAndAdditionalCourses]
(TransferCourse1,TransferCourse2, TransferCourse3,
TransferCourse4, TransferCourse5, AdditionalCourse1,
AdditionalCourse2, AdditionalCourse3, AdditionalCourse4, AdditionalCourse5)
VALUES ('" & firstTransferCourse & ",' " & secondTransferCourse
& "," & thirdTransferCourse & " ', '" & fourthTransferCourse & " ','"
& fifthTransferCourse & " ', '" & firstAdditionalCourse & ", "
& secondAdditionalCourse & "',' " & thirdAdditionalCourse & "',' "
& fourthAdditionalCourse & "','" & fifthAdditionalCourse & "' ")
Thanks
You seem to me missing some single quotation marks.
Please try this:
Command.CommandText = ("INSERT INTO [TransfersAndAdditionalCourses]
(TransferCourse1,TransferCourse2, TransferCourse3, TransferCourse4,
TransferCourse5, AdditionalCourse1, AdditionalCourse2, AdditionalCourse3,
AdditionalCourse4, AdditionalCourse5)
VALUES ('" & firstTransferCourse & "',' " & secondTransferCourse & "', '"
& thirdTransferCourse & " ', '" & fourthTransferCourse & " ','"
& fifthTransferCourse & " ', '" & firstAdditionalCourse & "', '"
& secondAdditionalCourse & "',' " & thirdAdditionalCourse & "',' "
& fourthAdditionalCourse & "','" & fifthAdditionalCourse & "' ")
Format the insert query and you will see the mistakes, you are missing the quotes in many places, also the leading ( and tailing ) is not needed. It should look like something as
Command.CommandText =
"
INSERT INTO [TransfersAndAdditionalCourses]
(
TransferCourse1,
TransferCourse2,
TransferCourse3,
TransferCourse4,
TransferCourse5,
AdditionalCourse1,
AdditionalCourse2,
AdditionalCourse3,
AdditionalCourse4,
AdditionalCourse5
)
VALUES
(
'" & firstTransferCourse & "',
'" & secondTransferCourse & "',
'" & thirdTransferCourse & "',
'" & fourthTransferCourse & "',
'" & fifthTransferCourse & "',
'" & firstAdditionalCourse & "',
'" & secondAdditionalCourse & "',
'" & thirdAdditionalCourse & "',
'" & fourthAdditionalCourse & "',
'" & fifthAdditionalCourse & "'
)"
I am using this code to insert 4 textboxes of data into a table.
CurrentDb.Execute = "INSERT INTO tbl_machineheader(Line No, Description, Service Interval, Type) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"
However it doesn't seem to work and I can't figure out why.
That "=" character is wrong:
CurrentDb.Execute **=** "INSERT INTO tbl_machineheader(Line No, Description, Service Interval, Type) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"
Also you should use square brackets for your column names. The correct syntax is as follows:
CurrentDb.Execute "INSERT INTO tbl_machineheader([Line No], [Description], [Service Interval], [Type]) values ( '" & Me.Combo3 & "', '" & Me.Text1 & "', '" & Me.Text6 & "', '" & Me.Text12 & "')"
The below is in my control source and works:
=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",DLookUp("[CustomerSKU]","ItemCustomerSKU","ItemNumber = '" & Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" & Forms!GARF!customer & "' AND Company = '" & Forms!GARF!company & "' AND MarketingSeason = '" & DLookUp("[Code]","IDtoCode","ID = " & Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")
I would like to assign it to another control source through VBA code like such:
Forms![GARF]![GARFsubForm]![SKU].ControlSource = (the above code)
Forms![GARF]![GARFsubForm]![SKU] is a column in a datasheet.
UPDATE:
With Remou's help I was able to come up with the below working solution:
The code is on the Form_Load event:
Forms![GARF]![GARFsubForm]![SKU].ControlSource =
"=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>'',"
& "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '""
& Forms!GARF!GARFsubForm![finished_good_#] & ""' AND Customer = '" _
& Forms!GARF!customer & "' AND Company = '" _
& Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
& " AND MarketingSeason = '"
& DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id) & "'"")" & ",'')"
There are a few problems. This is what is returned by your string (broken into lines for clarity):
=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",
DLookUp([CustomerSKU],ItemCustomerSKU,"ItemNumber = '"
& Forms!GARF!GARFsubForm![finished_good_#]
& " ' AND Customer = '" & Forms!GARF!customer
& "' AND Company = '" & Forms!GARF!company
& "' AND MarketingSeason = '" & DLookUp([Code],IDtoCode,"ID = "
& Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")
As you can see, these two are not quoted and they need to be:
DLookUp([CustomerSKU],ItemCustomerSKU,
There is an extra space here:
& " ' AND Customer = '" & Forms!GARF!customer
Is finished_good_# text similarly season_id?
It is often possible to use single quote ' instead of "", especially if you escape any occurrence of ' in the text with two ' eg ='O''Reilly'
EDIT
You are doing this in code, so it should be possible to make life easier, for example:
A = DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id)
B = "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '" _
& Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" _
& Forms!GARF!customer & "' AND Company = '" _
& Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
& " AND MarketingSeason = '" & A & "'"")"
s = "=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>''," & c & ",'')"
Debug.Print s