Runtime error 3075 can't figure it out - ms-access

I'm trying to do an insert on a button click event and I keep getting a runtime error of missing operator in query expression my query looks like the following. Any ideas?
Private Sub CmdAdd_Click()
Dim strSql As String
strSql = "INSERT INTO Current_Costs(PO_Number,Lineitemid,Capital_detail,CapitalID,GL_Number,Cost_Type,Cost_Center,Cost_cat,Item_Cost,PO_Date)" & _
" VALUES (" & Me.txtPONum & "','" & _
Me.cmbCapDetail & "','" & _
Me.cmbCapDetail.Column(1) & "','" & _
Me.txtCapID & "','" & _
Me.txtGLNum & "','" & _
Me.cmbCostType & "','" & _
Me.txtCostCen & "','" & _
Me.cmbCostCat & "','" & _
Me.txtCost & "','" & _
Me.TxtPODate & "')"
DoCmd.RunSQL strSql
i have a similar query that has the same issue and i cant see the problem
CurrentDb.Execute ("UPDATE Current_Costs " & _
"SET PO_Number='" & Me.txtPONum & "'" & _
",Lineitemid='" & Me.cmbCapDetail & "'" & _
",Capital_detail='" & Me.cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & Me.txtCapID & "'" & _
",GL_Number='" & Me.txtGLNum & "'" & _
",Cost_Type='" & Me.cmbCostType & "'" & _
",Cost_Center='" & Me.txtCostCen & "'" & _
",Cost_cat='" & Me.cmbCostCat & "'" & _
",Item_Cost='" & Me.txtCost & "'" & _
",PO_Date='" & Me.TxtPODate & "'" & _
"WHERE LineItemPOID=" & Me.txtID.Tag)
edit solved

This
" VALUES (" & Me.txtPONum & "','" & _
Is short one quote, it should be
" VALUES ('" & Me.txtPONum & "','" & _
Write your sql in a string, it makes it easier to see the problems:
strSql = "UPDATE Current_Costs " & _
"SET PO_Number='" & txtPONum & "'" & _
",Lineitemid='" & cmbCapDetail & "'" & _
",Capital_detail='" & cmbCapDetail.Column(1) & "'" & _
",CapitalID='" & txtCapID & "'" & _
",GL_Number='" & txtGLNum & "'" & _
",Cost_Type='" & cmbCostType & "'" & _
",Cost_Center='" & txtCostCen & "'" & _
",Cost_cat='" & cmbCostCat & "'" & _
",Item_Cost='" & txtCost & "'" & _
",PO_Date='" & TxtPODate & "'" & _
" WHERE LineItemPOID=" & txtID
Dim db As database
Set db = CurrentDB
db.Execute strsql dbFailOnError
You were missing a space before WHERE and you had an unmatched parenthesis.
Consider using parameters: End of statement error

Related

I got syntax error in update statement.Run-time error: 3144 I use the following code

Private Sub cmdAdd_Click()
'when we Click on button Add there are two options
'1. for insert
'2. for update
If Me.txtdate.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO BphCore1(DateOfTesting, TestingAt, VoltageLevel, PolarityCheck, AppliedVoltageInPrimary, AppliedVoltageInSecondary, MeasurmentUnitIR, MinimumValue, PrimaryToEarth, PrimaryToSecondary,SecondaryToEarth, MeasurmentUnitWR, WRSecondaryValues, 20OfInjectedValue, 20OfMeasurmentValue, 40OfInjectedValue, 40OfMeasurmentValue, 60OfInjectedValue, 60OfMeasurmentValue, 80OfInjectedValue, 80OfMeasurmentValue, 100OfInjectedValue, 100OfMeasurmentValue, KneePointVoltage, MagnetizingCurrent) " & _
" VALUES('" & Me.txtdate & "','" & Me.cbotest & "','" & Me.cbovl & "','" & Me.cbopc & "','" & Me.cboavp & "','" & Me.cboavs & "','" & Me.cbomuir & "','" & _
Me.txtMV & "','" & Me.txtpe & "','" & Me.txtps & "','" & Me.txtse & "','" & Me.cbomuwr & "','" & Me.txtwrsv & "','" & Me.txti20 & "','" & Me.txtm20 & "','" & Me.txti40 & "','" & Me.txtm40 & "','" & Me.txti60 & "','" & Me.txtm60 & "','" & Me.txti80 & "','" & Me.txtm80 & "','" & Me.txti100 & "','" & Me.txtm100 & "','" & Me.txtkpv & "','" & Me.txtmc & "')"
Else
'otherwise (Tag of txtdate store the DateOfTesting of BphCore1 to be modified)
CurrentDb.Execute " UPDATE BphCore1 " & _
" SET DateOfTesting=" & Me.txtdate & _
", TestingAt='" & Me.cbotest & "'" & _
", VoltageLevel='" & Me.cbovl & "'" & _
", PolarityCheck='" & Me.cbopc & "'" & _
", AppliedVoltageInPrimary='" & Me.cboavp & "'" & _
", AppliedVoltageInSecondary='" & Me.cboavs & "'" & _
", MeasurmentUnitIR='" & Me.cbomuir & "'" & _
", MinimumValue='" & Me.txtMV & "'" & _
", PrimaryToEarth='" & Me.txtpe & "'" & _
", PrimaryToSecondary='" & Me.txtps & "'" & _
", SecondaryToEarth='" & Me.txtse & "'" & _
", MeasurmentUnitWR='" & Me.cbomuwr & "'" & _
", WRSecondaryValues='" & Me.txtwrsv & "'" & _
", 20OfInjectedValue='" & Me.txti20 & "'" & _
", 20OfMeasurmentValue='" & Me.txtm20 & "'" & _
", 40OfInjectedValue='" & Me.txti40 & "'" & _
", 40OfMeasurmentValue='" & Me.txtm40 & "'" & _
", 60OfInjectedValue='" & Me.txti60 & "'" & _
", 60OfMeasurmentValue='" & Me.txtm60 & "'" & _
", 80OfInjectedValue='" & Me.txti80 & "'" & _
", 80OfMeasurmentValue='" & Me.txtm80 & "'" & _
", 100OfInjectedValue='" & Me.txti100 & "'" & _
", 100OfMeasurmentValue='" & Me.txtm100 & "'" & ", KneePointVoltage='" & Me.txtkpv & "'" & ", MagnetizingCurrent='" & Me.txtmc & "'" & _
" WHERE DateOfTesting=" & Me.txtdate.Tag
End If
'clear form
cmdclean_Click
'refresh data in the list on form
BphCore1subfrm.Form.Requery
End Sub
Parameters for date/time fields use # delimiter, parameters for text fields use apostrophe delimiter, parameters for number fields do not require delimiter.
" VALUES(#" & Me.txtdate & "#,'"
" SET DateOfTesting=#" & Me.txtdate & "#" & _
" WHERE DateOfTesting=#" & Me.txtdate.Tag & "#"

Microsoft access "data type mismatch in criteria expression" error

I have written code to add and edit data in a table but I am getting "data type mismatch in criteria expression".
Private Sub Submit_Click()
If Me.txtid.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO Client(Client_ID,Title,First_Name,Last_Name,Employer,DOB,Updated_Date,Email,Phone_Number,Mobile_Number,Address_Street,Date_of_1st_Session,Presenting_Issue,Next_Of_Kin,EAP_Number,Status,History)" & _
" VALUES('" & Me.txtid & "','" & Me.txt_title & "','" & Me.txtname & "','" & Me.txtlastname & "','" & Me.txtorg & "','" & Me.CreatedDate & "','" & Me.UpdatedDate & "','" & Me.txtEmail & "', '" & Me.txtphone & "','" & Me.txtmb & "', '" & Me.txtadd & "','" & Me.txtdate & "', '" & Me.txtissue & "','" & Me.txtnext & "','" & Me.txtEAP & "','" & Me.Combo45 & "','" & Me.txtHistory & "')"
Else
CurrentDb.Execute "UPDATE Client" & " SET Client_ID=" & Me.txtid & ", Title='" & Me.txt_title & "'" & ", First_Name='" & Me.txtname & "'" & ", Last_Name='" & Me.txtlastname & "'" & ", Employer='" & Me.txtorg & "'" & ", DOB='" & Me.CreatedDate & "'" & ", Updated_Date='" & Me.UpdatedDate & "'" & ", Email='" & Me.txtEmail & "'" & ", Phone_Number='" & Me.txtphone & "'" & ", Mobile_Number='" & Me.txtmb & "'" & ", Address_Street='" & Me.txtadd & "'" & ", Date_of_1st_Session='" & Me.txtdate & "'" & ", Presenting_Issue='" & Me.txtissue & "'" & ", Next_Of_Kin='" & Me.txtnext & "'" & ", EAP_Number='" & Me.txtEAP & "'" & ", Status='" & Me.Combo45 & "'" & ", History='" & Me.txtHistory & "'" & " WHERE Client_ID=" & Me.txtid.Tag
End If
Command39_Click
Me.txtid.SetFocus
Client_subform.Form.Requery
End Sub
You seem to be treating all the values as strings ('" & Me.txtid & "')when some of the data items may be numeric or dates.
Try ( " & Me.txtid & " ) if it's numeric or (#" & Me.CreatedDate & "#)

Run-time error '3061': Too Few Parameters. Expected 2

So I'm trying to teach myself VBA again and I'm having a cople of troubles. I'm trying to add new users to a table but keep getting the above error when I click my "Update" button. The text field will be in the form of 2 letters and 5 numbers. XX11111 for example.
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtLoginName.Tag & "" = "" Then
'add data to table
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
Else
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmModifyUsersSub.Form.Requery
End Sub
you are missing a ' in this line:
" set LoginName=" & Me.txtLoginName & "'" & _
change it to
" set LoginName='" & Me.txtLoginName & "'" & _
and :
" WHERE LoginName=" & Me.txtLoginName.Tag
to
" WHERE (LoginName='" & Me.txtLoginName.Tag & "')" ' and I don't know if this your intended where condition.
The error pretty much gives you the answer. You need 2 parameters for the function it is failing on. One thing to try is to change
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName=" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName=" & Me.txtLoginName.Tag
To
CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
" VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')",dbFailOnError
and
CurrentDb.Execute "UPDATE tblUsers " & _
"set LoginName='" & Me.txtLoginName & "'" & _
", UserName='" & Me.txtUsername & "'" & _
", Rank='" & Me.cboRank & "'" & _
" WHERE LoginName='" & Me.txtLoginName.Tag & "'", dbFailOnError

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

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