VB.Net Query Syntax Error in MS Access - vb.net

Dim cmd As OleDb.OleDbCommand = New OleDbCommand(" UPDATE Items SET PartNo = " & PartNoTxt.Text & ", EqptDesc = '" & DescTxt.Text & "', Qty = '" & QtyTxt.Text & "', Pasok = '" & InTxt.Text & "', Labas = '" & OutTxt.Text & "', Tapos = '" & EndTxt.Text & "', SerialNumber = '" & SerialTxt.Text & "', CalibrationType = '" & CalType.Text & "', CalibrationDate = '" & CalOn.Value.Date & "', SupplierDue = '" & SuppDue.Value.Date & "', TodaysDate = '" & TodDate.Value.Date & "', Validity = '" & Validity.Text & "', Status = '" & StatsTxt.Text & "', DiOh = '" & DOTxt.Text & "', User = '" & UserTxt.Text & "', EMType = '" & EMType.Text & "' WHERE CTID = " & TxtItemCode.Text, connection)
I am using MS Access.
Here is the code which I am having problem with, it says "Syntax error in UPDATE statement.".

User is a reserved word in Jet/ACE, so you must surround it with square brackets in your statement:
... & "', [User] = '" & UserTxt.Text & ...

Related

Access Error Run-Time 2447 Insert into table

The Error shows its there is an Invalid of the . (Dot) Or ! operator
If IsNull(Me.m_1) Or IsNull(Me.u_1) Or Me.m_1 = "" Or Me.u_1 = "" Then
MsgBox "Please add all needed info"
Else
sqls = "INSERT INTO User_access (Application_ID, Modul_ID, username_ID, Clock_ID, Assinged_date, Assinged_by, Request_number) values('" & Me.A_1 & "', '" & Me.m_1 & "', '" & Me.u_1 & "', '" & Me.Clock_ID & "', '" & Date & "', '" & Me.Assinged_by & "', '" & Me.Request_number & "');"
DoCmd.RunSQL sqls
End If
I cannot see any error within the code So please Help me
Use Date():
sqls = "INSERT INTO User_access (Application_ID, Modul_ID, username_ID, Clock_ID, Assinged_date, Assinged_by, Request_number) values('" & Me.A_1 & "', '" & Me.m_1 & "', '" & Me.u_1 & "', '" & Me.Clock_ID & "', Date(), '" & Me.Assinged_by & "', '" & Me.Request_number & "');"
And no quoting of numeric values.

Updating table with changes to textbox

I have a table of vehicle info (5 fields per vehicle) with the capabilities of multiple vehicles per person. I have all 5 fields and all vehicles going into a multidimensional array and populating a textbox based on the vehicle number (ie vic1, vic2...). I need to update the table if any of the vehicle information is change. I have a different text box for each vehicle. I'm not sure how to take the changes (additions/deletions/updates) and load it back into the table.
Private Sub cmdDone_Click()
strMake1 = VicArray(0, 0)
strModel1 = VicArray(0, 1)
strYear1 = VicArray(0, 2)
strColor1 = VicArray(0, 1)
strPlate1 = VicArray(0, 1)
strMake2 = VicArray(1, 0)
strModel2 = VicArray(1, 1)
strYear2 = VicArray(1, 2)
strColor2 = VicArray(1, 3)
strPlate2 = VicArray(1, 4)
.
.
.
strMake6 = VicArray(5, 0)
strModel6 = VicArray(5, 1)
strYear6 = VicArray(5, 2)
strColor6 = VicArray(5, 3)
strPlate6 = VicArray(5, 4)
strUpdate = "Update VehicleInfo SET " & _
"Vic1License = '" & strPlate1 & "',Vic1Make = '" & strMake1 & "', Vic1Model = '" & strModel1 & "', Vic1Year = '" & strYear1 & "', Vic1Color = '" & strColor1 & " Handicapped= " & strhandicap & "'," & _
"Vic2License = '" & strPlate2 & "',Vic2Make = '" & strMake2 & "', Vic2Model = '" & strModel2 & "', Vic2Year = '" & strYear2 & "', Vic2Color = '" & strColor2 & "'," & _
"Vic3License = '" & strPlate3 & "',Vic3Make = '" & strMake3 & "', Vic3Model = '" & strModel3 & "', Vic3Year = '" & strYear3 & "', Vic3Color = '" & strColor3 & "'," & _
"Vic4License = '" & strPlate4 & "',Vic4Make = '" & strMake4 & "', Vic4Model = '" & strModel4 & "', Vic4Year = '" & strYear4 & "', Vic4Color = '" & strColor4 & "'," & _
"Vic5License = '" & strPlate5 & "',Vic5Make = '" & strMake5 & "', Vic5Model = '" & strModel5 & "', Vic5Year = '" & strYear5 & "', Vic5Color = '" & strColor5 & "'," & _
"Vic6License = '" & strPlate6 & "',Vic6Make = '" & strMake6 & "', Vic6Model = '" & strModel6 & "', Vic6Year = '" & strYear6 & "', Vic6Color = '" & strColor6 & "'," & _
" WHERE ( LastName= '" & TxtLast & "', AND FirstName= '" & txtFirst & "')"
DoCmd.RunSQL strUpdate
I think you are trying to do manually what Access can do for you automatically.
I would suggest setting the recordsource of your form to the table VehicleInfo. Then, set the control source of each control to the fields of that table.
Try this article, it should give you a good intro to using Access more effectively.
https://support.office.com/en-us/article/introduction-to-forms-e8d47343-c937-44e8-a80f-b6a83a1fa3ae
I'm thinking you should have your VehicleInfo form filtered for the name chosen from the first form. The first form could have something like this:
public sub btn_Click()
DoCmd.OpenForm "VehicleInfoForm", , , "Name = '" & selectedName & "'"
end sub
And then the vehicle info form would display the information for that user. Whenever they make changes to the text boxes, it would be saved when they close the form. Assuming it is all bound properly.
I actually split it by a delimiting variable and loaded it into a different array.
SplitArray1 = Split(txtVic1.Value, ",")
strMake1 = SplitArray1(0)
strModel1 = SplitArray1(1)
strYear1 = SplitArray1(2)
strColor1 = SplitArray1(3)
strPlate1 = SplitArray1(4)
.
.
.

SQL Query in VBA code and Access

I have a problem of syntax ( rror 3134) with my SQL query....
Here is the query :
StrSQL1 = "INSERT INTO Repos (Num_utilisateur, Nb_conges_semaine1, Nb_conges_semaine2, Nb_conges_semaine3, Nb_conges_semaine4, Total_conges_mois1, Nb_conges_semaine5, Nb_conges_semaine6, Nb_conges_semaine7, Nb_conges_semaine8, Total_conges_mois2, Nb_conges_semaine9, Nb_conges_semaine10, Nb_conges_semaine11, Nb_conges_semaine12, Total_conges_mois3, Nb_JoursFeries_semaine1, Nb_JoursFeries_semaine2, Nb_JoursFeries_semaine3, Nb_JoursFeries_semaine4, Total_Nb_JoursFeries_mois1, Nb_JoursFeries_semaine5, Nb_JoursFeries_semaine6, Nb_JoursFeries_semaine7, Nb_JoursFeries_semaine8, Total_Nb_JoursFeries_mois2, Nb_JoursFeries_semaine9, Nb_JoursFeries_semaine10, Nb_JoursFeries_semaine11, Nb_JoursFeries_semaine12, Nb_AutresAbsences_semaine1, Nb_AutresAbsences_semaine2, Nb_AutresAbsences_semaine3, Nb_AutresAbsences_semaine4, Total_Nb_AutresAbsences_mois1, Nb_AutresAbsences_semaine5, Nb_AutresAbsences_semaine6, Nb_AutresAbsences_semaine7, Nb_AutresAbsences_semaine8," & _ "# Total_Nb_AutresAbsences_mois2, Nb_AutresAbsences_semaine9, Nb_AutresAbsences_semaine10, Nb_AutresAbsences_semaine11, Nb_AutresAbsences_semaine12, Total_Nb_AutresAbsences_mois3, Total_general) VALUES ('" & NumAgent & "', '" & Conges_1 & "', '" & Conges_2 & "', '" & Conges_3 & "', '" & Conges_4 & "', '" & CongesTotalMois1 & "', '" & Conges_5 & "', '" & Conges_6 & "', '" & Conges_7 & "', '" & Conges_8 & "', '" & CongesTotalMois2 & "', '" & Conges_9 & "', '" & Conges_10 & "', '" & Conges_11 & "', '" & Conges_12 & "', '" & CongesTotalMois3 & "', '" & JourFeriee_1 & "', '" & JourFeriee_2 & "', '" & JourFeriee_3 & "', '" & JourFeriee_4 & "', '" & JourFerieeTotalMois1 & "', '" & JourFeriee_5 & "', '" & JourFeriee_6 & "', '" & JourFeriee_7 & "', '" & JourFeriee_8 & "', '" & JourFerieeTotalMois2 & "', '" & JourFeriee_9 & "', '" & JourFeriee_10 & "', '" & JourFeriee_11 & "', '" & JourFeriee_12 & "', '" & JourFerieeTotalMois3 & "'," & _ "# '" & JourAutre1 & "', '" & JourAutre2 & "', '" & JourAutre3 & "', '" & JourAutre4 & "', '" & JourAutreTotalMois1 & "', '" & JourAutre5 & "', '" & JourAutre6 & "', '" & JourAutre7 & "', '" & JourAutre8 & "', '" & JourAutreTotalMois2 & "', '" & JourAutre9 & "', '" & JourAutre10 & "', '" & JourAutre11 & "', '" & JourAutre12 & "', '" & JourAutreTotalMois3 & "', '" & TotalGeneral & "');"
Sorry It's a long query...
Thank you in advance for your help !
if i do not see wrong i count 46 fields but 47 values. The number of fields and values must be the same.
Your have in your INSERT clause:
# Total_Nb_AutresAbsences_mois2
In your VALUES clause:
# JourAutre1
They're invalid field names.
Try removing the # symbol.
Edit:
Both #Karthick and #Michelle are correct as you're missing that field in your results.
Also hope I'm correct about the # symbol being invalid.

Syntax Error in Update Query (VB.Net)

Please help me. My project uses an access database. This is my code:
Try
Dim SqlQuery As String = "UPDATE Penawaran SET UP = '" & box_up.Text & "', Dari = '" & box_dari.Text & "', Hal = '" & box_hal.Text & "', No_Reference = '" & box_no_reference.Text & "', Email = '" & box_email.Text & "', CC = '" & box_cc.Text & "', No = '" & box_no.Text & "', Bahan_Proses = '" & box_bahan_proses.Text & "', Jumlah = '" & box_jumlah.Text & "', Harga_Satuan = '" & box_harga_satuan.Text & "', Total = '" & box_total.Text & "', Sub_Total = '" & box_sub_total.Text & "', Total_PPN = '" & box_ppn_10_persen.Text & "', Total_Semua = '" & box_total_semua.Text & "', Terbilang = '" & box_terbilang.Text & "', Note = '" & box_note.Text & "', Nama = '" & box_nama.Text & "', Bagian = '" & box_bagian.Text & "' WHERE Kepada = '" & box_kepada.Text & "'"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlQuery
.Connection = Database
.ExecuteNonQuery()
End With
MsgBox("Data yang anda pilih sudah berhasil di ubah & simpan ke dalam sistem.")
Call Sett_pnwrn()
'Me.button_delete.Enabled = False
' Me.button_edit.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
Why it doesn't work?
This is the error message :
Syntax error in update statement.
What should I do?

Getting text from text boxes on tab control

I have a form with a tabcontrol in it with 4 tabpages each has it's own texboxes and comboboxes, then I have a button outside of the tabcontrol wich builds a SQL string from all the values entered in these boxes even if some are left blank, the issue is that when i press the button i get a null reference exception and even when I use Try..Catch or on error resume next the compiler refuses to create the text string.
here is the code I have:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
If Me.BankGuidTextBox.Text = vbNullString Then
Me.BankGuidTextBox.Text = Guid.Empty.ToString
End If
If Me.BankNumTextBox.Text = vbNullString Then
Me.BankNumTextBox.Text = 0
End If
If Me.NameTextBox.Text = vbNullString Or Me.CodeTextBox.Text = vbNullString Then
GoTo outofsub
End If
Try
Dim strSQL3 As String
strSQL3 = "USE MSILHR" & vbCrLf & _
"UPDATE employee SET Code = '" & Me.CodeTextBox.Text & "', Name = '" & Me.NameTextBox.Text & "', Latinname = '" & Me.LatinNameTextBox.Text & "', Barcode = '" & Me.BarcodeTextBox.Text & "', FirstName = '" & Me.FirstNameTextBox.Text & "', LastName = '" & Me.LastNameTextBox.Text & "', idFather = '" & Me.IdFatherTextBox.Text & "', idMother = '" & Me.IdMotherTextBox.Text & "', idBirthDate = '" & Me.IdBirthDateDateTimePicker.Text & "', idBirthplace = '" & Me.IdBirthPlaceTextBox.Text & "', idSex = '" & Me.IdSexComboBox.SelectedValue & "', idMaritalstat = '" & Me.IdMaritalstatComboBox.SelectedValue & "', idSmoker = '" & Me.IdSmokerComboBox.SelectedValue & "', idMilitary = '" & Me.IdMilitaryComboBox.SelectedValue & "', idRegistry = '" & Me.IdRegistryTextBox.Text & "', idNum = '" & Me.IdNumTextBox.Text & "', idAddress = '" & Me.IdAddressTextBox.Text & "', idNationality = '" & Me.IdNationalityTextBox.Text & "', idReligion = '" & Me.IdReligionComboBox.SelectedItem & "', idTel = '" & Me.IdTelTextBox.Text & "', idMobile = '" & Me.IdMobileTextBox.Text & "', idNotes = '" & Me.IdNotesTextBox.Text & "', jStartDate = '" & Me.JStartDateDateTimePicker.Text & "', jQuitDate = '" & Me.JQuitDateDateTimePicker.Text & "', jQuitReason = '" & Me.JQuitReasonTextBox.Text & "', jSocialSec = '" & Me.JSocialSecTextBox.Text & "', jSocialdin = '" & Me.JSocialdinDateTimePicker.Text & "', jSocialdout = '" & Me.JSocialdoutDateTimePicker.Text & "', jHoliday1 = '" & Me.JHoliday1ComboBox.SelectedValue & "', jHoliday2 = '" & Me.JHoliday2ComboBox.SelectedValue & "', jEmpStat = '" & Me.JEmpStatComboBox.SelectedValue & "', jEmail = '" & Me.JEmailTextBox.Text & "', jPrevvacs = '" & Me.JPrevvacsTextBox.Text & "', jAdminvacs = '" & Me.JAdminvacsTextBox.Text & "', jHealthvacs = '" & Me.JHealthvacsTextBox.Text & "', jUnpaidvacs = '" & Me.JUnpaidvacsTextBox.Text & "', DepartmentGuid = '" & Me.DepartmentGuidComboBox.SelectedValue.ToString & "', JobTitleGuid = '" & Me.JobTitleGuidComboBox.SelectedValue.ToString & "',SalarycalctypeGuid = '" & Me.SalarycalctypeGuidComboBox.SelectedValue.ToString & "', TeamGuid = '" & Me.TeamGuidComboBox.SelectedValue.ToString & "', WorkDays = '" & Me.WorkDaysTextBox.Text & "', DaHours = '" & Me.DaHoursTextBox.Text & "', OverTimeHourPrice = '" & Me.OverTimeHourPriceTextBox.Text & "', CutSalary = '" & Me.CutSalaryTextBox.Text & "',BasicSalary = '" & Me.BasicSalaryTextBox.Text & "', SpecialSalary = '" & Me.SpecialSalaryTextBox.Text & "',CurrencyGuid = '" & Me.CurrencyGuidComboBox.SelectedValue.ToString & "', BankGuid = '" & Me.BankGuidTextBox.Text & "', BankNum = '" & Me.BankNumTextBox.Text & "', PeriodGUID = '" & Me.EmpperiodCombo.SelectedValue.ToString & "'" & vbCrLf & _
"WHERE GUID = '" & Me.GUIDTextBox.Text & "'"
'"update employee set Picturepath = (select name from types where type = '660')+'\" & Me.NameTextBox.Text & "' where code = '" & Me.CodeTextBox.Text & "' AND Name = '" & Me.NameTextBox.Text & "'"
Dim dbConnection As New SqlConnection(connectionString)
' A SqlCommand object is used to execute the SQL commands.
Dim cmd As New SqlCommand(strSQL3, dbConnection)
' Open the connection, execute the command, and close the connection.
' It is more efficient to ExecuteNonQuery when data is not being
' returned.
dbConnection.Open()
cmd.ExecuteNonQuery()
dbConnection.Close()
MessageBox.Show("Record is updated.", _
"Data Addition Status", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'Me.EmployeeTableAdapter.Fill(Me.MSILHRDataset.employee)
ResetUI()
outofsub:
MsgBox("you need to add more information!", MsgBoxStyle.Information, "Error")
End Sub
Ok new update :
I entered some data left some text boxes empty but made sure all combo boxes has values selected in them and then pressed updatebutton I got the same problem, but I redid that changed the current tab selected to any other one (from the other four) and then switched back to the previous tab, I press the button and IT WORKS!!?? it's as if the form doesn't Commit the values entered by user until focus changes or something it's crazy.
You are only catching a SqlException.
You need to also catch a general exception.
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch exc As Exception
MessageBox.Show(exc.ToString, "General Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Not sure why you are getting a null exception.
But it is not caught because it is not a SqlException.
On that String break it down do see where it is breaking.
On the surface it looks like it should work.
I suspect one of the controls is returning null rather than string.empty.