Getting text from text boxes on tab control - sql

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.

Related

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)
.
.
.

keep getting a "System.Data.OleDb.OleDbException: 'Syntax error in UPDATE statement" error

Good day, I keep getting a "System.Data.OleDb.OleDbException: 'Syntax error in UPDATE statement" error.
Can someone please assist with why and where?
Any help will be greatly appriciated
Below is the code
Try
conec.Open()
Dim cmd As New OleDbCommand("UPDATE tblCreate Set Username = '" & txtUserName.Text & "', EMail = '" & txtEmail.Text & "', FirstName = '" & TxtName.Text & "', LastName = '" & txtSurname.Text & "', Access = '" & cmbAccess.SelectedItem & "', CreatedBY = '" & Label9.Text & "', DateCreated = '" & Label10.Text & "', ChangedBY = '" & Label6.Text & "', DateChanged = '" & Date.Now.ToString("yyyy-MMMM-dd hh:mm tt") & "' WHERE UserName = " & txtUserName.Text & ";")
cmd.CommandType = CommandType.Text
cmd.Connection = conec
cmd.ExecuteNonQuery()
MessageBox.Show("Data Updated" & vbCrLf & "Done")
conec.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Looks like you're missing apostrophes in your where clause, try:
Dim cmd As New OleDbCommand("UPDATE tblCreate Set Username = '" & txtUserName.Text & "', EMail = '" & txtEmail.Text & "', FirstName = '" & TxtName.Text & "', LastName = '" & txtSurname.Text & "', Access = '" & cmbAccess.SelectedItem & "', CreatedBY = '" & Label9.Text & "', DateCreated = '" & Label10.Text & "', ChangedBY = '" & Label6.Text & "', DateChanged = '" & Date.Now.ToString("yyyy-MMMM-dd hh:mm tt") & "' WHERE UserName = '" & txtUserName.Text & "';")
I recommend using SQL Parameters however, they're a lot easier to maintain.

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?

datatype mismatch in criteria expression vb2010 edit button

I'm getting the error
datatype mismatch in criteria expression vb.net edit button
when I click the edit button on my vb2010 project. Adding and search button works fine but not on edit button.
Here is my code:
Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\pcInventory.accdb"
myConnection = New OleDbConnection
OpenStr = "Has Open Office"
If TbPCnum.Text = "" Then
MsgBox("Please select data.")
Else
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "update [pctable] set [Person Incharge] = '" & TbPI.Text & "', [DeptSection] = '" & TbDS.Text & "', [Workgroup] = '" & TbWG.Text & "', [Network ID] = '" & TbNI.Text & "', [OS] = '" & TbOS.Text & "', [Processor] = '" & TbProcessor.Text & "', [MOBO] = '" & TbMB.Text & "', [Memory] = '" & TbRAM.Text & "', [HDD] = '" & TbHDD.Text & "', [Product Key] = '" & TbKey.Text & "', [MS Office] = '" & CbMS.SelectedValue & "', [Open Office] = '" & tbOO.Text & "', [Anti Virus] = '" & TbAV.Text & "', [Monitor] = '" & TbMonitor.Text & "', [Email Address] = '" & TbEadd.Text & "', [Date Purchased] = '" & TbDP.Text & "', [Machine Type] = '" & comboMT.SelectedValue & "', [Remarks] = '" & TbRM.Text & "', [Password] = '" & TbPW.Text & "' Where [PCNumber] = '" & TbPCnum.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
TbPCnum.Clear()
TbPI.Clear()
TbDS.Clear()
TbWG.Clear()
TbNI.Clear()
TbOS.Clear()
TbProcessor.Clear()
TbMB.Clear()
TbRAM.Clear()
TbHDD.Clear()
TbKey.Clear()
tbOO.Clear()
TbAV.Clear()
TbMonitor.Clear()
TbEadd.Clear()
TbDP.Clear()
TbRM.Clear()
TbPW.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
I suspect that it may be your query string. You sending a string/text instead of an int/number for example.
check through it and make sure that the database column type matches the data you sending through with the query.
Remove the single quotation marks if you sending int/numeric values.

VB.Net Query Syntax Error in MS Access

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 & ...