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
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
I am trying to run an Excel macro that imports data from a csv file into an Access database, MySQL front end. The macro code is not new-we've been using this for a couple of years, the table in the Access DB is not new, and the csv files with which the data is used to be brought into the Access table is also unchanged (meaning same rows, same data types).
I am stumped as the error appears to be something of an anomaly and I have tried several fixes to correct the error. I have attached a copy of the error as well as the code below. The issue with the column referenced below as well error message attached, "itemcode", this column is in the database.
Clearly, there is something that I am missing. Any insights are greatly appreciated. Thank you.
'Read in Current .csv file
'Data Services
NumRows = Application.CountA(Range("A:A"))
For iCount = 1 To NumRows
Dim InstData(11)
InstData(1) = Range("A" & iCount) 'Date
invoiceDate = Format(InstData(1), "yyyy/mm/dd")
InstData(2) = Range("B" & iCount) 'Invoice Number
InstData(3) = Range("C" & iCount) 'names
flname() = Split(InstData(3), ",")
lname = flname(0)
fname = flname(1)
InstData(4) = Range("D" & iCount) 'Address1
InstData(5) = Range("E" & iCount) 'Address2
InstData(6) = Range("F" & iCount) 'City
InstData(7) = Range("G" & iCount) 'State
InstData(8) = Format(Range("H" & iCount), "00000") 'postal code
InstData(9) = Range("I" & iCount) 'phone
InstData(10) = Range("J" & iCount) 'email
InstData(11) = Range("K" & iCount) 'amount
Set conn = New ADODB.Connection
Set rec = New ADODB.Recordset
conn.Open "Dsn=HERIpub"
rec.Open ("Insert into tbl_Invoice (invoiceNumber,invoiceDate,invoiceAmount,invoiceDescription,invoiceFAU,invoiceCostCenter,invoiceProject,itemcode,invoicecFName,invoicecLName, invoicecAddr1, invoicecAddr2, invoicecCity, invoicecState, invoicecZIP, invoicecEmail, invoicecPhone) Values ('" & InstData(2) & "','" & invoiceDate & "','" & InstData(11) & "','Data Services','T6','T6','DATASL','40070DATASL','" & fname & "'" & ",'" & lname & "','" & InstData(4) & "', '" & InstData(5) & "', '" & InstData(6) & "', '" & InstData(7) & "', '" & InstData(8) & "', '" & InstData(10) & "', '" & InstData(9) & "')"), conn
macro error
Look at the variable fname , there you have redundant " & " . I think it should be:
rec.Open ("Insert into tbl_Invoice (invoiceNumber,invoiceDate,invoiceAmount,invoiceDescription,invoiceFAU,invoiceCostCenter,invoiceProject,itemcode,invoicecFName,invoicecLName, invoicecAddr1, invoicecAddr2, invoicecCity, invoicecState, invoicecZIP, invoicecEmail, invoicecPhone) Values ('" & InstData(2) & "','" & invoiceDate & "','" & InstData(11) & "','Data Services','T6','T6','DATASL','40070DATASL','" & fname & "','" & lname & "','" & InstData(4) & "', '" & InstData(5) & "', '" & InstData(6) & "', '" & InstData(7) & "', '" & InstData(8) & "', '" & InstData(10) & "', '" & InstData(9) & "')"), conn
Tell me if I'm right, or mark a vote up!
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.
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 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 & "')"