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
Related
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
I am a Create Customer form with a button and the following textboxes: txtCustomerID, txtFirstName, txtLastName, txtPhone, txtEmail. When I click the button, the information typed in the textboxes are inserted into [Customer] table and creates a folder based on the value of txtCustomerID, txtFirstName, and txtLastName. For example, if..
txtCustomerID: 100
txtFirstName: Ron
txtLastName: Smith
I want the folder name to display "100 - Ron Smith", but I can't find a solution to do that.
Below is the code that I used and it works perfectly, I just can't get it to the way I want to display the folder name.
Private Sub btnCreate_Click()
DoCmd.RunSQL "INSERT INTO [Customer] (CustomerID, FirstName, LastName, Phone, Email) VALUES ('" & Me.txtCustomerID & "', '" & Me.txtFirstName & "', '" & Me.txtLastName & "', '" & Me.txtPhone & "', '" & Me.txtEmail & "')"
MkDir ("C:\Users\Desktop\" & txtCustomerID.Value & txtFirstName.Value & txtLastName.Value)
End Sub
Any help or suggestion would be great. Thank you
MkDir ("C:\Users\Desktop\" & txtCustomerID.Value & " - " & txtFirstName.Value & " " & txtLastName.Value)
will add the dash and spaces that you want.
Should be as simple as putting in spaces.....
Private Sub btnCreate_Click()
DoCmd.RunSQL "INSERT INTO [Customer] (CustomerID, FirstName, LastName, Phone, Email) VALUES ('" & Me.txtCustomerID & "', '" & Me.txtFirstName & "', '" & Me.txtLastName & "', '" & Me.txtPhone & "', '" & Me.txtEmail & "')"
MkDir ("C:\Users\Desktop\" & txtCustomerID.Value & " - " & txtFirstName.Value & " " & txtLastName.Value)
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 & "')"