I am working on part of a application that needs to import data from a excel sheet into a mysql database table. The code works fine until it gets to a record in the excel sheet where one of the string values gets assigned "ABCDE All'John D Doe 999 West Lame Blvd Cullman, AL 35055". I am not certain but I believe that it has to do completely with the "'" that appears there. Which that can not change and other records from the excelsheet could contain the " ' " as well... When it gets to this record it throws this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'John D Doe','ABCDE','All'John','D','Doe','256-555-5555',' ','256-555-5555' at line 1
the code that i have around this problems is as follows:
Private Function PerFormUpdate(ByVal customer As String, ByVal bill_to As String, ByVal Contact As String, ByVal Company As String, ByVal firstName As String, ByVal mi As String, ByVal lastname As String, ByVal phone As String, ByVal altPhone As String, ByVal fax As String)
Dim _db As New schoolEntities
Dim command As MySqlCommand = _dbconn.CreateCommand()
command.CommandText = "SELECT * FROM quickbooks_imports WHERE Customer= "" & _customer& "" & Bill_to= "" & _bill_to& "" & Contact= "" & _Company& ""& First_Name= "" & _firstName& "" & M_I= "" & _mi& "" & Last_Name= "" & _lastname& "" & Phone= "" & _phone& "" & Alt_Phone= "" & _altPhone& "" & Fax= "" & _Fax& """
_dbconn.Open()
Dim _mysqlReader As MySqlDataReader = command.ExecuteReader()
_dbconn.Close()
If Not _mysqlReader.HasRows Then
Dim _UpdateItem As New quickbooks_imports
Dim updateCommand As MySqlCommand = _dbconn.CreateCommand()
_UpdateItem.Customer = customer
_UpdateItem.Bill_to = bill_to
_UpdateItem.Contact = Contact
_UpdateItem.Company = Company
_UpdateItem.First_Name = firstName
_UpdateItem.M_I = mi
_UpdateItem.Last_Name = lastname
_UpdateItem.Phone = phone
_UpdateItem.Alt_Phone = altPhone
_UpdateItem.Fax = fax
updateCommand.CommandText = "INSERT INTO quickbooks_imports(Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') "
_dbconn.Open()
updateCommand.ExecuteNonQuery()
_db.SaveChanges()
The Error shows up on the ExecuteNonQuery to perform the update..
Any help would be greatly appreciated...
As per your response I switched to the params and this is the new code:
updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"
updateCommand.Parameters.AddWithValue("Customer", _UpdateItem.Customer)
updateCommand.Parameters.AddWithValue("Bill_to", _UpdateItem.Bill_to)
updateCommand.Parameters.AddWithValue("Contact", _UpdateItem.Contact)
updateCommand.Parameters.AddWithValue("Company", _UpdateItem.Company)
updateCommand.Parameters.AddWithValue("First_Name", _UpdateItem.First_Name)
updateCommand.Parameters.AddWithValue("M_I", _UpdateItem.M_I)
updateCommand.Parameters.AddWithValue("Last_Name", _UpdateItem.Last_Name)
updateCommand.Parameters.AddWithValue("Phone", _UpdateItem.Phone)
updateCommand.Parameters.AddWithValue("Alt_Phone", _UpdateItem.Alt_Phone)
updateCommand.Parameters.AddWithValue("Fax", _UpdateItem.Fax)
how ever its throwing a fatal exception now...
I just tried using name parameters as you mentioned in your reply and the code is as follows:
Private Function PerFormUpdate(ByVal customer As String, ByVal bill_to As String, ByVal Contact As String, ByVal Company As String, ByVal firstName As String, ByVal mi As String, ByVal lastname As String, ByVal phone As String, ByVal altPhone As String, ByVal fax As String)
Dim _db As New schoolEntities
Dim command As MySqlCommand = _dbconn.CreateCommand()
command.CommandText = "SELECT * FROM quickbooks_imports WHERE Customer= "" & _customer& "" & Bill_to= "" & _bill_to& "" & Contact= "" & _Company& ""& First_Name= "" & _firstName& "" & M_I= "" & _mi& "" & Last_Name= "" & _lastname& "" & Phone= "" & _phone& "" & Alt_Phone= "" & _altPhone& "" & Fax= "" & _Fax& """
_dbconn.Open()
Dim _mysqlReader As MySqlDataReader = command.ExecuteReader()
_dbconn.Close()
If Not _mysqlReader.HasRows Then
Dim _UpdateItem As New quickbooks_imports
Dim updateCommand As MySqlCommand = _dbconn.CreateCommand()
_UpdateItem.Customer = customer
_UpdateItem.Bill_to = bill_to
_UpdateItem.Contact = Contact
_UpdateItem.Company = Company
_UpdateItem.First_Name = firstName
_UpdateItem.M_I = mi
_UpdateItem.Last_Name = lastname
_UpdateItem.Phone = phone
_UpdateItem.Alt_Phone = altPhone
_UpdateItem.Fax = fax
updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
updateCommand.Parameters.AddWithValue("#Customer", _UpdateItem.Customer)
updateCommand.Parameters.AddWithValue("#Bill_to", _UpdateItem.Bill_to)
updateCommand.Parameters.AddWithValue("#Contact", _UpdateItem.Contact)
updateCommand.Parameters.AddWithValue("#Company", _UpdateItem.Company)
updateCommand.Parameters.AddWithValue("#First_Name", _UpdateItem.First_Name)
updateCommand.Parameters.AddWithValue("#M_I", _UpdateItem.M_I)
updateCommand.Parameters.AddWithValue("#Last_Name", _UpdateItem.Last_Name)
updateCommand.Parameters.AddWithValue("#Phone", _UpdateItem.Phone)
updateCommand.Parameters.AddWithValue("#Alt_Phone", _UpdateItem.Alt_Phone)
updateCommand.Parameters.AddWithValue("#Fax", _UpdateItem.Fax)
'updateCommand.CommandText = "INSERT INTO EXCEL (id,Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.id & "','" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') ON DUPLICATE KEY UPDATE Customer= '" & _UpdateItem.Customer & "' Bill_to= '" & _UpdateItem.Bill_to & "' Contact= '" & _UpdateItem.Contact & "' Company= '" & _UpdateItem.Company & "' First_Name= '" & _UpdateItem.First_Name & "' M_I= '" & _UpdateItem.M_I & "' Last_Name= '" & _UpdateItem.Last_Name & "' Phone= '" & _UpdateItem.Phone & "' Alt_Phone= '" & _UpdateItem.Alt_Phone & "' Fax= '" & _UpdateItem.Fax & "'"
'updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') "
_dbconn.Open()
updateCommand.ExecuteNonQuery()
_db.SaveChanges()
and I am still getting the fatal exception on the updateCommand.ExecuteNonQuery()
Fatal error encountered during command execution.
InnerException Message: "Parameter '?' must be defined."
You need to use parameters which will properly escape your strings for database execution.
Refer to this link. http://www.devart.com/dotconnect/mysql/docs/Parameters.html
Edit: Try using named parameters instead:
updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ("#Customer", "#Bill_to", "#Contact", "#Company", "#First_Name", "#M_I", "#Last_Name", "#Phone", "#Alt_Phone", "#Fax")"
updateCommand.Parameters.AddWithValue("#Customer", _UpdateItem.Customer)
updateCommand.Parameters.AddWithValue("#Bill_to", _UpdateItem.Bill_to)
updateCommand.Parameters.AddWithValue("#Contact", _UpdateItem.Contact)
updateCommand.Parameters.AddWithValue("#Company", _UpdateItem.Company)
updateCommand.Parameters.AddWithValue("#First_Name", _UpdateItem.First_Name)
updateCommand.Parameters.AddWithValue("#M_I", _UpdateItem.M_I)
updateCommand.Parameters.AddWithValue("#Last_Name", _UpdateItem.Last_Name)
updateCommand.Parameters.AddWithValue("#Phone", _UpdateItem.Phone)
updateCommand.Parameters.AddWithValue("#Alt_Phone", _UpdateItem.Alt_Phone)
updateCommand.Parameters.AddWithValue("#Fax", _UpdateItem.Fax)
Related
written query execute successfully without any error but data is not getting saved.
Table_StockList is a Table not sure where i am making mistake . kindly assist.
Dim sid As String
Dim pnumber As String
Dim pname As String
Dim pmake As String
Dim pmodel As String
Dim phsn As String
Dim pdop As Date
Dim ppp As Currency
Dim pstatus As String
Dim psp As Currency
Dim paqty As Integer
Dim totalqty As Integer
Dim pinvoice As Attachment
sid = Me.Stock_ID.Value
pnumber = Me.Part_No.Value
pname = Me.Product_Name.Value
pmake = Me.Make.Value
pmodel = Me.Model.Value
phsn = Me.HSN.Value
pdop = Me.curpurchasedate.Value
ppp = Me.Purchase_Price_dr_.Value
pstatus = Me.Status.Value
psp = Me.Selling_price_cr_.Value
paqty = Me.availqty.Value
totalqty = Me.totalqty.Value
pinvoice = Me.Invoice
'Me.Recordset.MoveLast
DoCmd.RunCommand acCmdRecordsGoToLast
DoCmd.RunSQL "INSERT INTO Table_StockList ([Stock ID],[Part No],[Product Name],Make,Model,HSN,[Date of Purchase],Status,[Selling price(cr)],[Available QTY],[Total QTY])" & _
"VALUES('" & sid & "','" & pnumber & "','" & pname & "','" & pmake & "','" & pmodel & "','" & phsn & "','" & pdop & "','" & ppp & "','" & pstatus & "','" & psp & "','" & paqty & "','" & totalqty & "','" & pinvoice & "' )"
I have a button that lets me select a file to choose, that location of the file gets saved to a textbox, I use the location from the textbox to upload a file to a data base and I get an error.
"An INSERT INTO query cannot contain a multi-valued field."
Following is my code.
Private Sub btn_Browse_Click()
Dim f As Object
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant
Set f = Application.FileDialog(3)
f.allowMultiSelect = True
If f.Show Then
For Each varItem In f.selectedItems
strFile = Dir(varItem)
strFolder = Left(varItem, Len(varItem) - Len(strFile))
MsgBox "Folder: " & strFolder & vbCrLf & _
"File: " & strFile
Me.txt_PicSource = strFolder + strFile
Next
End If
Set f = Nothing
...
CurrentDb.Execute _
"INSERT INTO Field_Return_Table(Date_FRL, Time_FRL, Client_FRL, Product_FRL, LOT_FRL, Territory_FRL, Tracking_FRL, Condition_FRL, Location_FRL, Completed_By_FRL, Comments_FRL, Attachment_FRL) " & _
"VALUES ('" & strDate & "', '" & strDate & "', '" & Me.combo_Client.Value & "', '" & Me.txt_Product.Value & "', '" & Me.txt_Lot.Value & "', '" & Me.txt_Territory.Value & "', '" & Me.txt_Track.Value & "', '" & strCondition & "', '" & strLocation & "', '" & strUser & "', '" & Me.txt_Comment.Value & "', '" & Me.txt_PicSource.Value & "')"
...
Any tips of how to upload this file via the INSERT INTO?
So I've got the code below (not all of it is grey for some reason)
Private Sub CmdEdit_Click()
Me.Tableinform.Requery
Dim rs As DAO.Recordset
Set rs = Me.Tableinform.Form.RecordsetClone
'get data to textbox control
With rs
If .RecordCount > 0 Then
With Me.Tableinform.Form.Recordset
Me.ShowIDbox = .Fields("[ID]").Value
Me.CompbyDD = .Fields("[Received By]")
Me.Date1 = .Fields("[Date Received]")
Me.Date2 = .Fields("[Date Processed]")
Me.ReqType = .Fields("[Request Type]")
Me.InsName = .Fields("[Insured Name]")
Me.RiskNo = .Fields("[Risk Number]")
Me.EndtRef = .Fields("[Endorsement Reference]")
Me.EOCNo = .Fields("[EOC Number]")
Me.Tech = .Fields("[Technician]")
Me.BillIns = .Fields("[Billing Instructions]")
Me.Addrecord.Caption = "Update"
Me.CmdEdit.Enabled = False
Me.cmdDuplicate.Enabled = True
Me.CmdDelete.Enabled = True
End With
End If
End With
End Sub
After one Edit and Update, the edit function throws out Error '3021' No Current Record?
Please show me where I am going wrong with this.
Add/Update button as follows:
If Me.ShowIDbox.Value = "" Then
CurrentDb.Execute "INSERT INTO fmdatatable( [Received By], [Date Received], [Date Processed], [Request Type], [Insured Name], [Risk Number], [Endorsement Reference], [EOC Number], [Technician], [Billing Instructions]) " & _
" VALUES ('" & Me.CompbyDD & "','" & Me.Date1 & "','" & Me.Date2 & "','" & Me.ReqType & "','" & Me.InsName & "','" & Me.RiskNo & "','" & Me.EndtRef & "','" & Me.EOCNo & "','" & Me.Tech & "','" & Me.BillIns & "')"
Else
CurrentDb.Execute "UPDATE fmdatatable SET [Received By]='" & Me.CompbyDD & "'" & _
", [Date Received]='" & Me.Date1 & "'" & _
", [Date Processed]='" & Me.Date2 & "'" & _
", [Request Type]='" & Me.ReqType & "'" & _
", [Insured Name]='" & Me.InsName & "'" & _
", [Risk Number]='" & Me.RiskNo & "'" & _
", [Endorsement Reference]='" & Me.EndtRef & "'" & _
", [EOC Number]='" & Me.EOCNo & "'" & _
", [Technician]='" & Me.Tech & "'" & _
", [Billing Instructions]='" & BillIns & "'" & _
" WHERE [ID]=" & Me.ShowIDbox.Value
End If
Me.CompbyDD = "-Please Select-"
Me.Date1 = ""
Me.Date2 = ""
Me.ReqType = "-Please Select-"
Me.InsName = ""
Me.RiskNo = ""
Me.EndtRef = ""
Me.EOCNo = ""
Me.Tech = ""
Me.BillIns = "-Please Select-"
Me.ShowIDbox = ""
Me.Addrecord.Caption = "Add Record"
Me.CmdEdit.Enabled = True
Me.cmdDuplicate.Enabled = False
Me.CmdDelete.Enabled = False
Tableinform.Form.Requery
This edit works once, then all subsequent later edits are throwing errors out.
I have created a table where I'm saving attachments(in external folder) and file paths in the table. When I'm trying to update existing attachment with another, it does not work. Please see below "Add file" code and "Update button" code.
Private Sub cmAdd_Click()
On Error Resume Next
Dim strLokacioni As String
Dim strSQL As String
strLokacioni = "C:\Users\HSE\Desktop\datas\" & getFileName(Me.path.Value)
strSQL = "INSERT INTO tbl_tracker(path, filename, IncNo, PATS, SAP, First, Last, IncDate, Description, Location, OshaType, IncType, RootCause,Inspector, Surfaces, WeatherCon, WorkRelated, IncTime)" & _
"VALUES ( '" & strLokacioni & "', '" & Me.path.Value & "', '" & Me.txtInc & "', '" & Me.txtPATS & "', '" & Me.txtSAP & "', '" & Me.txtFirst & "', '" & Me.txtLast & "', '" & Me.txtDate & "', '" & Me.txtDesc & "', '" & Me.cmbLoc & "', '" & Me.cmbOsha & "', '" & Me.cmbType & "', '" & Me.txtCause & "', '" & Me.cmbInsp & "', '" & Me.cmbSur & "', '" & Me.cmbWcon & "', '" & Me.cmbRelated & "', '" & Me.txtTime & "')"
CurrentDb.Execute (strSQL)
MsgBox "Record Added", vbInformation, "Information"
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
fso.CopyFile Me.path.Value, strLokacioni
Set fso = Nothing
Me.tbl_tracker_subform.Form.Requery
End Sub
' Below is update button code
Private Sub Command88_Click()
'On Error Resume Next
Dim strLokacioni As String: strLokacioni = "C:\Users\HSE\Desktop\datas\" & getFileName(Me.path.Value)
Me.path = strLokacioni
CurrentDb.Execute "UPDATE tbl_tracker " & _
" SET IncNo = " & Me.txtInc & _
", path = '" & strLokacioni & "'" & _
", filename = '" & Me.filename & "'" & _
", PATS = '" & Me.txtPATS & "'" & _
", SAP = '" & Me.txtSAP & "'" & _
", First = '" & Me.txtFirst & "'" & _
", Last = '" & Me.txtLast & "'" & _
", IncDate = '" & Me.txtDate & "'" & _
", Location = '" & Me.cmbLoc & "'" & _
", Description = '" & Me.txtDesc & "'" & _
", OshaType = '" & Me.cmbOsha & "'" & _
", Inctype = '" & Me.cmbType & "'" & _
", RootCause = '" & Me.txtCause & "'" & _
", Inspector = '" & Me.cmbInsp & "'" & _
", Surfaces = '" & Me.cmbSur & "'" & _
", WeatherCon = '" & Me.cmbWcon & "'" & _
", WorkRelated = '" & Me.cmbRelated & "'" & _
", IncTime = '" & Me.txtTime & "'" & _
" WHERE IncNo = " & Me.txtInc.Tag
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
fso.CopyFile Me.path.Value, strLokacioni
Set fso = Nothing
MsgBox "Record Updated", vbInformation, "Information"
Me.tbl_tracker_subform.Form.Requery
End sub
And the the value Me.path.Value at the start of Command88_click is coming from :
Public Function getFileName(ByVal strPath As String) As String
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
getFileName = getFileName(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
Private Sub cbdBrowse_Click()
On Error Resume Next
Dim f As Object
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.AllowMultiSelect = False
f.Show
Me.path.Value = f.SelectedItems(1)
End Sub
When I use update queries like you have here, I would always use the following syntax.
dim db as database
set db = currentDb
db.execute "UPDATE...."
would you require the same? Or have I been going about this a long way.
Also, I would have left a comment as this may not be the answer. Unfortunately, I cant due to rep<50
Private Sub cmdAdd_Click()
'add data to table
CurrentDb.Execute = "INSERT INTO jscbb_dir2(ID,Lastname,FirstName, PrimA, Artea,LubNum,OfficeNum,OfficePhone,Email,LabPhone,stats)" & _
" VALUES(" & Me.Textid & ",'" & Me.TextLast & "','" & Me.TextFirst & "','" & Me.Textprima & "','" & Me.Textarea & "','" & Me.Textlabnum & _
"','" & Me.Textofficenum & "','" & Me.Textofficephone & "','" & Me.Textemail & "','" & Me.Textlabphone & "','" & Me.Textstatus & "')"
'refresh data is list on focus
jscbb_dirsub.Form.Requery
End Sub
Why am I getting an error on the last (Me.Textstatus)? I know this is a low-level question, but I need another pair of eyes, I've been looking at this for over an hour. The error is "Compile Error: Argument Not Optional"
Consider parameters, they will be easier to debug.
Dim qdf As QueryDef
ssql = "INSERT INTO jscbb_dir2(ID,Lastname,FirstName,PrimA,Artea," _
& "LubNum,OfficeNum,OfficePhone,Email,LabPhone,stats) " _
& "VALUES([id],[last],[first],[prima],[area],[lab]," _
& "[office],[phone],[email],[stat])"
Set qdf = CurrentDb.CreateQueryDef("", ssql)
qdf.Parameters("id") = Me.TextID
qdf.Parameters("last") = Me.Textlast
qdf.Parameters("first") = Me.Textfirst
qdf.Parameters("prima") = Me.Textprima
qdf.Parameters("area") = Me.Textarea
qdf.Parameters("lab") = Me.Textlabnum
qdf.Parameters("office") = Me.Textofficenumbet
qdf.Parameters("phone") = Me.Textofficephone
qdf.Parameters("email") = Me.Textemail
qdf.Parameters("stat") = Me.Textstatus
qdf.Execute dbFailOnError
Execute is a method, not a property. You don't use = between a method and its arguments, so
CurrentDb.Execute = "..."
should be
CurrentDb.Execute "..."