MySQL query too long in vba - mysql

I have an insert MySQL query that is too long and I get error probably because I am not breaking them properly.
so here's the query
sq = "Insert into trainer (`ID`, `Site work being carried out`, `Manufacturers Serial Number or VIN`,`Platform`,`Type of Equipment`,`Equipment Sub-Grouping`,`Equipment Group`,`Equipment Description`,`NSN / Part Number`,`Manufacturer`,`Model`,`Date of Manufacture`,`Base`,`On Base Location`,`Serial Number`,`Last Odometer or Hour Meter Reading`,`Current Status`,`Equipment Arrival Date`,`Equipment ECD (Estimated Completion Date)`,`Revised Estimated Delivery Date`,`Equipment ACD (Actual Equipment Completion Date)`,`Delivered Date`,`Equipment In Program for Repair or Refurbishment`,`Remarks`,`Date Requested Induction of Equipment`,`Date Equipment Inducted into Site / Program`,`Diagnosing Start Date`,`Diagnosing End Date`,`Date Price Parts Sourcing Start`,`Date Price Parts Sourcing End`,`Date NTE Process Starts`,`Induction Cycle Process Complete Date`,`Date Proposal Sent `, `S O issued Date`,`S O No`,`Date PO Request Sent to Procurement`,`Date Foregin / International Parts Arrived at Customs`,`Date Last Part(s) Recvd at Site (last part recvd to start work)`,`Date Repair Started (After last part was recvd at site)`,`Date Repair Completed`,`Local Parts Ordered`,`Local Parts Received`,`Foreign / Int Parts Ordered`,`Foreign / Int Parts Received`) values ('" & prr.value & "','" & sitecombo.value & "','" & vin.value & "','" & ComboBox2.value & "','" & ComboBox3.value & "','" & ComboBox4.value & "','" & eqgrp.value & "','" & desc.value & "','" & nsn.value & "','" & mfg.value & "','" & mdl.value & "','" & mfgyr.value & "','" & rsafbase.value & " ','" & onbaseloc.value & "','" & rsafsr.value & "','" & odomtr.value & "','" & status.value & "','" & arvldt.value & "','" & ecd.value & "','" & redd.value & "','" & acd.value & "','" & deldt.value & "','" & vin.value & "','" & rprrfb.value & "','" & remarks.value & "','" & indreq.value & "','" & inddt.value & "','" & digdt.value & "','" & digend.value & "','" & srcdt.value & "','" & srcend.value & "','" & ntedt.value & "','" & incend.value & "','" & nteprp.value & "','" & nteapp.value & "','" & nteend.value & "','" & sodt.value & "','" & sono.value & "','" & podt.value & "','" & farvl.value & "','" & lstpartrecdt.value & "','" & rprstrt.value & "','" & rprcom.value & "','" & lclord.value & "', '" & lclrec.value & "','" & ford.value & "','" & frec.value & "');"
can someone help me please...

Related

Runtime Error 3061 VBA using Insert command

I have the following code. I am using a form in MS Access 2010 to enter new lines into my table Nlog. When I try to run the code, it gives me the error Run-Time error '3061': Too few parameters. Expected 1.
I can't find the error!
Private Sub Command128_Click()
'add data to table
CurrentDb.Execute "INSERT INTO NLog(IDKEY, Company, CoName, State, City, AmtpdTotal, DateRec, Notified, DateNotice, AcctID, TaxType, Period, NoticeReason, Resolution, TaxDue, Intdue, PenDue, Dateres, Amtpdint, amtpdpen, amtpdtax, Assigned, subAssgn, Resolved) " & _
" VALUES(" & Me.TxtIDKEY & ",'" & Me.Company & "','" & Me.CoName & "','" & _
Me.State & "','" & Me.City & "','" & Me.TxtAmtpdTotal & "','" & _
Me.DateRec & "','" & Me.Notified & "','" & _
Me.DateNotice & "','" & Me.AcctID & "','" & Me.TaxType & "','" & Me.Period & "','" & _
Me.NoticeReason & "','" & Me.Resolution & "','" & Me.TaxDue & "','" & Me.IntDue & "','" & Me.PenDue & "','" & _
Me.DateRes & "','" & Me.AmtPdInt & "','" & Me.AmtpdPen & "','" & Me.AmtpdTax & "','" & _
Me.Assigned & "','" & Me.txtsubass & "','" & Me.Resolved & "')", dbFailOnError
MsgBox ("Entry Added")
Use a string variable to hold the INSERT statement. That allows you to Debug.Print the string so you can inspect the actual statement your code created. And you can Execute the string.
Dim strInsert As String
strInsert = "INSERT INTO NLog(IDKEY, Company, CoName, State, City, AmtpdTotal, DateRec, Notified, DateNotice, AcctID, TaxType, Period, NoticeReason, Resolution, TaxDue, Intdue, PenDue, Dateres, Amtpdint, amtpdpen, amtpdtax, Assigned, subAssgn, Resolved) " & _
" VALUES(" & Me.TxtIDKEY & ",'" & Me.Company & "','" & Me.CoName & "','" & _
Me.State & "','" & Me.City & "','" & Me.TxtAmtpdTotal & "','" & _
Me.DateRec & "','" & Me.Notified & "','" & _
Me.DateNotice & "','" & Me.AcctID & "','" & Me.TaxType & "','" & Me.Period & "','" & _
Me.NoticeReason & "','" & Me.Resolution & "','" & Me.TaxDue & "','" & Me.IntDue & "','" & Me.PenDue & "','" & _
Me.DateRes & "','" & Me.AmtPdInt & "','" & Me.AmtpdPen & "','" & Me.AmtpdTax & "','" & _
Me.Assigned & "','" & Me.txtsubass & "','" & Me.Resolved & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailOnError
You can then view the completed statement (the output from Debug.Print) in the Immediate window. Ctrl+g will take you to the Immediate window.
If the error is not obvious, you can copy the statement text, create a new query in the query designer, switch it to SQL View, and paste in the copied statement. When you try to run that query from the designer, Access will display a parameter dialog which asks you to supply a value for the parameter. Notice that dialog also includes the "name" of the parameter. Frequently the cause of this problem is a misspelled field name --- since Access can't find a field by that name, it assumes it must be a parameter instead.
Alternatively, you can use VBA code to show you the names of any parameters in your query:
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strInsert)
For Each prm In qdf.Parameters
Debug.Print prm.Name
Next

#22003Out of range value adjusted for column ' column name ' at row 1

I have this code in saving the record in the database
mycom.Connection = cn
mycom.CommandText = "Insert into
tbl_employee(LastName,FirstName,MiddleName,NickName,DHired,Position,Gender,Address,
Cellphone,FName,FOccupation,MName,MOccupation,DSpoken,BDate,Place,CStatus,Citizenship,
Height,Weight,Religion,EName,EContact,SSS,Pagibig,Philhealth,TIN,Spouse,SOccupation,
ChildNo,C1,A1,C2,A2,Motto,FMovie,FQuotation,FActress,FColor,Moment,FActor,PastTime,FDay,
ESchool,EYG,SSchool,SYG,CSchool,CYG,CCourse,CUG,VSchool,VYG,VCourse,MSchool,MYG,MDegree,
SSkills,STitle1,SAddress1,SDate1,STitle2,SAddress2,SDate2,STitle3,SAddress3,SDate3,
STitle4,SAddress4,SDate4,STitle5,SAddress5,SDate5) values ('" & txtLastName.Text & "','"
& txtFirstName.Text & "','" & txtMiddleName.Text & "','" & txtNickname.Text & "','" & dtpDate.Text & "','" & cboPosition.Text & "','" & cboGender.Text & "','" & txtAddress.Text
& "','" & txtCellphone.Text & "','" & txtFName.Text & "','" & txtFOccupation.Text & "','"
& txtMName.Text & "','" & txtMOccupation.Text & "','" & txtDialects.Text & "','" &
dtpBdate.Text & "','" & txtPlace.Text & "','" & cboCStatus.Text & "','" &
txtCitizenship.Text & "','" & txtHeight.Text & "','" & txtWeight.Text & "','" &
txtReligion.Text & "','" & txtEName.Text & "','" & txtEPhone.Text & "','" & txtSSS.Text &
"','" & txtPagibig.Text & "','" & txtPhilhealth.Text & "','" & txtTin.Text & "','" &
txtSpouse.Text & "','" & txtSOccupation.Text & "','" & txtChildNo.Text & "','" &
txtChild1.Text & "','" & txtAge1.Text & "','" & txtChild2.Text & "','" & txtAge2.Text &
"','" & txtMotto.Text & "','" & txtMovie.Text & "','" & txtQuotation.Text & "','" & txtActress.Text & "','" & txtColor.Text & "','" & txtMoment.Text & "','" & txtActor.Text & "','" & txtPasttime.Text & "','" & txtDay.Text & "','" & txtElemSchool.Text & "','" &
txtElemYG.Text & "','" & txtHSSchool.Text & "','" & txtHSYG.Text & "','" &
txtCollSchool.Text & "','" & txtCollYG.Text & "','" & txtCollDegree.Text & "','" &
txtYearLevel.Text & "','" & txtVocSchool.Text & "','" & txtVocYG.Text & "','" &
txtVocCourse.Text & "','" & txtEMasSchool.Text & "','" & txtEMasYG.Text & "','" &
txtEMasDegree.Text & "','" & txtSSkills.Text & "','" & txtSTTitle1.Text & "','" &
txtSTLoc1.Text & "','" & txtSTDate1.Text & "','" & txtSTTitle2.Text & "','" &
txtSTLoc2.Text & "','" & txtSTDate2.Text & "','" & txtSTTitle3.Text & "','" &
txtSTLoc3.Text & "','" & txtSTDate3.Text & "','" & txtSTTitle4.Text & "','" &
txtSTLoc4.Text & "','" & txtSTDate4.Text & "','" & txtSTTitle5.Text & "','" &
txtSTLoc5.Text & "','" & txtSTDate5.Text & "');"
myr = mycom.ExecuteReader
It always displays this error
#22003Out of range value adjusted for column 'A1' at row 1
Can anyone help me with this one
Change your datatype of age and change your query to parameterized query like this
MySqlCommand m = new MySqlCommand(readCommand);
m.Parameters.Add(new MySqlParameter("LastName", txtLastName.Text));
m.Parameters.Add(new MySqlParameter("FirstName", txtFirstName.Text));
MySqlDataReader r = m.ExecuteReader();
Assuming column A1 is supplied by txtAge1.Text.
The error is likely either A1 being the wrong type eg int and inputting abc. Does txtAge1.Text return a string or an int?
or being the type being too small eg byte and inputting 300
Prevent SQL Injection
The basic pattern according to the MySQL documentation
cmd.CommandText = "INSERT INTO myTable VALUES(NULL, #number)"
cmd.Prepare()
cmd.Parameters.AddWithValue("#number", 1)
cmd.ExecuteNonQuery()

Error with SQL Insert Into Access 2010

I may have found the problem but at this point trying to correct it has been the issue. I have checked the source table and the insertion table and both have the correct fields/type values needed for this insert. So, my question is why am I getting an appending error in one row with this statement? I believe the only Yes/No box ( i.e. Ramp ) is causing this headache. How would I go about correcting this? Thank you very much here is the insertion statement.
DoCmd.RunSQL "INSERT INTO tblReportFinal(ProjectNo, RouteType, Route, StreetType, Direction1, Direction2, CommonDesignation, " & _
"FromRouteType, FromRoute, FromStreetType, FromCommonDesignation, ToRouteType, ToRoute,ToStreetType, ToCommonDesignation, Ramp, RampDirection, " & _
"Municipality, StartDate, StopDate, ImpactOnTravel, SuggestionsToMotorists, TrafficAlert, OtherComments, WidthRestriction, " & _
"ProjectStatus, ContractNo, Contractor, PercentComplete, GeneralOfficePhoneNumber, GeneralMobilPhoneNumber, GeneralHomePhoneNumber, ElectricalContractor, " & _
"ElectricalPhoneNumber, ContractorRep1Name, ContractorRep1OfficePhone, ContractorRep1MobilPhone, ContractorRep1HomePhone, ContractorRep2Name, ContractorRep2OfficePhone, ContractorRep2MobilPhone, " & _
"ContractorRep2HomePhone, ConsultantFirstName, ConsultantLastName, ConsultantOfficePhoneNumber, ConsultantMobilPhoneNumber, ConsultantHomePhoneNumber, ResidentFirstName, ResidentLastName, " & _
"ResidentOfficePhoneNumber, ResidentHomePhoneNumber, ResidentMobilePhoneNumber, SupervisorFirstName, SupervisorLastName, SupervisorOfficePhoneNumber, SupervisorMobilPhoneNumber, TrafficControlCompanyName, TrafficControlContactName, TrafficControlPhoneNumber, RouteTypeOrderBy) " & _
"VALUES('" & rsReportLoc("ProjectNo") & "','" & rsReportLoc("RouteType") & "','" & rsReportLoc("Route") & "','" & rsReportLoc("StreetType") & "','" & rsReportLoc("Direction1") & "','" & _
rsReportLoc("Direction2") & "','" & rsReportLoc("CommonDesignation") & "','" & rsReportLoc("FromRouteType") & "','" & rsReportLoc("FromRoute") & "','" & rsReportLoc("FromStreetType") & "', '" & _
rsReportLoc("FromCommonDesignation") & "','" & rsReportLoc("ToRouteType") & "','" & rsReportLoc("ToRoute") & "','" & rsReportLoc("ToStreetType") & "','" & rsReportLoc("ToCommonDesignation") & "', '" & rsReportLoc("Ramp") & "', '" & rsReportLoc("RampDirection") & "', '" & _
rsIn("Municipality") & "', '" & _
rsIn("StartDate") & "','" & rsIn("StopDate") & "','" & rsIn("ImpactOnTravel") & "','" & rsIn("SuggestionsToMotorists") & "','" & rsIn("TrafficAlert") & "','" & _
rsIn("OtherComments") & "','" & rsIn("WidthRestriction") & "','" & rsIn("ProjectStatus") & "','" & rsIn("ContractNo") & "','" & rsIn("Contractor") & "', '" & _
rsIn("PercentComplete") & "','" & rsIn("GeneralOfficePhoneNumber") & "','" & rsIn("GeneralMobilPhoneNumber") & "','" & rsIn("GeneralHomePhoneNumber") & "','" & rsIn("ElectricalContractor") & "', '" & _
rsIn("ElectricalPhoneNumber") & "','" & rsIn("ContractorRep1Name") & "','" & rsIn("ContractorRep1OfficePhone") & "','" & rsIn("ContractorRep1MobilPhone") & "','" & rsIn("ContractorRep1HomePhone") & "', '" & _
rsIn("ContractorRep2Name") & "','" & rsIn("ContractorRep2OfficePhone") & "','" & rsIn("ContractorRep2MobilPhone") & "','" & rsIn("ContractorRep2HomePhone") & "','" & rsIn("ConsultantFirstName") & "', '" & _
rsIn("ConsultantLastName") & "','" & rsIn("ConsultantOfficePhoneNumber") & "','" & rsIn("ConsultantMobilPhoneNumber") & "','" & rsIn("ConsultantHomePhoneNumber") & "','" & rsIn("ResidentFirstName") & "', '" & _
rsIn("ResidentLastName") & "','" & rsIn("ResidentOfficePhoneNumber") & "','" & rsIn("ResidentHomePhoneNumber") & "','" & rsIn("ResidentMobilePhoneNumber") & "','" & rsIn("SupervisorFirstName") & "','" & rsIn("SupervisorLastName") & "', '" & _
rsIn("SupervisorOfficePhoneNumber") & "','" & rsIn("SupervisorMobilPhoneNumber") & "','" & rsIn("TrafficControlCompanyName") & "','" & rsIn("TrafficControlContactName") & "','" & rsIn("TrafficControlPhoneNumber") & "', '" & RouteTypeOrder & "')"
If Ramp is an Yes/No type why are you using a String value?
, '" & rsReportLoc("Ramp") & "',
It should be simply
, " & rsReportLoc("Ramp") & ",

how to get specific id from access table in a msgbox

i want to get the id of specific record
this is my code
If DLookup("Full_Name", "tbl_Personal_Information", "Full_Name = Forms!frm_New_Person!F_N") > 0 Then
MSG = MsgBox("this person is existed and his id number is {" & [P_ID] & "} ", vbOKOnly + vbInformation + vbMsgBoxRtlReading, "client database")
Else
strsql101 = "insert into tbl_Personal_Information (Name_English, Father_English, Family_English, Mother_English, P_ID, NName, Father, Family, Mother, Birthdate, Nationality, Record_Number, Record_Place, Address, Mobile1, Mobile2, Phone1, Phone2, Ets_Name) Values ('" & Me.Name_English & "','" & Me.Father_English & "','" & Me.Family_English & "','" & Me.Mother_English & "','" & Me.PID & "','" & Me.NName & "','" & Me.Father & "','" & Me.Family & "','" & Me.Mother & "','" & Me.Birthdate & "','" & Me.Nationality & "','" & Me.Record_Number & "','" & Me.Record_Place & "','" & Me.Address & "','" & Me.Mobile_1 & "', '" & Me.Mobile_2 & "','" & Me.Phone_1 & "', '" & Me.Phone_2 & "','" & Me.Ets_Name & "')"
DoCmd.SetWarnings False
DoCmd.RunSQL strsql101
MSG = MsgBox("information added", vbOKOnly + vbInformation + vbMsgBoxRtlReading, "client database")
everything is OK but the id in the msgbox give me the last id in my table
Doesn't look like you are looking up the P_ID of the record you want.
Should you be doing something like this ?
MSG = MsgBox("this person is existed and his id number is {" & DLookup("[P_ID]", "tbl_Personal_Information", "Full_Name = Forms!frm_New_Person!F_N") & "} ", vbOKOnly + vbInformation + vbMsgBoxRtlReading, "client database")

how to vb.net date save to mysql date with no 0000-00-00

I've been killing brain cell for over 5 hours with this question.
how to properly save a date from vb.net to mysql date without getting the 0000-00-00all the time.
I have tried multiple suggestions where you format the date value before entering but it gets me the same result ex:
Dim departDate As Date
Dim d As String = Format(departDate, "05/05/2007")
in which i tested and didnt works i still got the 0000-00-00
I've tried:
Dim changeFormatDate As Date
Dim AddFechaListadoCasas2 as String
changeFormatDate = AddCasasListadoDatePicker.Text
AddFechaListadoCasas2 = changeFormatDate.ToString("yyyy-MM-dd")
which still gives me on mysql the 0000-00-00
I also tried this example:
Dim myDate as DateTime = DateTime.ParseExact(x, "yyyy-MM-dd", CultureInfo.CurrentCulture)
and trying to save to mysql still gave me the 0000-00-00
in my program im grabbing the date from a datepicker as text then swtich it to a string and try to save it to the database like that. if necessary i would love to convert it to a date with the format yyyy-MM-dd which is necessary for MySQL but everything i've done above it still shows me the 0000-00-00 which is frustrating.
any help would be very appreciated it.
Leo
here is the MySQL insert statement i have on my program.
AddFechaListadoCasas2 = AddCasasListadoDatePicker.Text
AddFechaCierreCasas2 = AddFechaCierreCasas.Text
SqlVentaCasas = "INSERT INTO VentaCasas (ID, Direccion, Ciudad_Municipio, Zona_Localidad, Barrio, " & _
"Estatus, Precio, Unidad, Departamento, Conjunto, Estrato, Matricula_Inmobiliaria, Cedula_Catastral," & _
"Escritura, Notaria, Ano, Area_Metros_2, Tipo_de_Construccion, Niveles, Habitaciones, Garajes," & _
"Techo, Vista, Picina, Cocina_Integral, Bar, Cuarto_Bano_Servicio, Antejardin, Equipos," & _
"Aire_Acondicionado, Detalles_addicionales, Estilo, Ano_Construccion, Orientacion, Banos, Parqueos," & _
"Piso, Balcon, Jacuzzi_Sauna, Chimenea, Hall_Television, Zona_lavanderia, Patio, Accesorios," & _
"Calefaccion, Administracion, Amenidades, Impuestos, Condiciones, Ocupacion," & _
"Cuota_Administracion, Restricciones, Seguridad, Mostrar, Comision, Fecha_Listado, " & _
"Agente_Cooperante, Fecha_Expiracion, CiudadEscritura, Agente, Licensia, Compania, NumeroPersonal," & _
"NumeroCompania, NumeroFax, Email, AreaLotesM2, PrecioOriginal, Account_Creator, NumeroDias ) VALUES ('" & _
AddIDCasas2 & "','" & _
AddDireccionCasas2 & "','" & AddCiudadCasas2 & "','" & AddZonaCasas2 & "','" & AddBarrioCasas2 & "','" & _
AddEstatusCasas2 & "'," & AddPrecioCasas2 & ",'" & AddUnidadCasas2 & "','" & AddDepartamentoCasas2 & "','" & _
AddConjuntoCasas2 & "'," & AddEstratoCasas2 & ",'" & AddMatriculaCasas2 & "','" & _
AddCedulaCatastralCasas2 & "','" & AddEscrituraCasas2 & "','" & AddNotariaCasas2 & "'," & _
AddAnoCasas2 & "," & AddMetros2Casas2 & ",'" & AddTipoConstruccionCasas2 & "'," & _
AddNivelesCasas2 & "," & AddHabitacionesCasas2 & "," & AddGarajesCasas2 & ",'" & _
AddTechoCasas2 & "','" & AddVistaCasas2 & "','" & AddPicinaCasas2 & "','" & AddCocinaIntegralCasas2 & "','" & _
AddBarCasas2 & "','" & AddCuartoCasas2 & "','" & AddAnteJardinCasas2 & "','" & AddEquiposCasas2 & "','" & _
AddAireAcondicionadoCasas2 & "','" & AddDetallesAddicionalesCasas2 & "','" & AddEstiloCasas2 & "'," & _
AddAnoConstruccionCasas2 & ",'" & AddOrientacionCasas2 & "'," & AddBanoCasas2 & "," & _
AddParqueoCasas2 & ",'" & AddPisoCasas2 & "','" & AddBalconCasas2 & "','" & AddJacuzziCasas2 & "','" & _
AddChimeneaCasas2 & "','" & AddTVCasas2 & "','" & AddZonaLavanderiaCasas2 & "','" & AddPatioCasas2 & "','" & _
AddAccesoriosCasas2 & "','" & AddCalefaccionCasas2 & "','" & AddAdministracionCasas2 & "','" & _
AddAmenidadesCasas2 & "','" & AddImpuestosCasas2 & "','" & AddCondicionesCasas2 & "','" & _
AddOcupacionCasas2 & "','" & AddCuotaAdministracionCasas2 & "','" & AddRestriccionesCasas2 & "','" & _
AddSeguridadCasas2 & "','" & AddMonstrarCasas2 & "','" & AddComisionCasas2 & "','" & _
AddFechaListadoCasas2 & "','" & AddAgenteCooperanteCasas2 & "','" & _
AddFecdhaExpiracionCasas2 & "','" & _
AddCiudadEscrituraCasas2 & "','" & AgenteNameCasas2 & "','" & _
AgenteLicensiaCasas2 & "','" & AgenteCompaniaCasas2 & "','" & _
AgentePersonalCasas2 & "','" & AgenteTelefonoCasas2 & "','" & _
AgenteFaxCasas2 & "','" & AgenteEmailCasas2 & "'," & AddAreaLotesM2Casas2 & "," & AddPrecioOriginalCasas2 & ", '" & AgenteAccountCreatorCasas2 & "', 0 );"
AddFechaListadoCasas2 and AddFecdhaExpiracionCasas2 are Strings that contains the date i want to input on MySQL
Your current query is vulnerable with SQL Injection. Please make use of ADONet Object such as Connection and Command with parameters. To be more specific, since you are using MySQL, use MySQL Data provider. It will automatically cast the data types for you.
Dim query As String = "INSERT INTO tableName (dateColumn) VALUES (#dateParam)"
Using conn As New MySqlConnection("connectionstringHere")
Using comm As New MySqlCommand()
With comm
.Connection = conn
.CommandText = query
.CommandType = Commandtype.Text
' Assuming this is a DatePicker '
.Parameters.AddWithValue("#dateParam", AddCasasListadoDatePicker.Value)
End With
Try
conn.Open()
comm.ExecuteNonQuery()
Catch(ex as MySqlException)
Msgbox(ex.Message.ToString())
End Try
End Using
End Using