Updating table with changes to textbox - vba

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

Related

Passing a NULL value to a column from an Update query

Update to Question:
Background. I am using SQL Server as a backend to my Access Database.
I have created this Stored Procedure (Update Query) that I run by way of a Passthrough query from MS Access. The process works fine with one exception.
If one of the date fields being passed to the update query is null then 1900-01-01 gets inserted into the column instead of leaving (making) it empty. I tried setting the default value to NULL but did not work. The date columns have date as their data type.
MS Access Side of things:
qry_PT_sp_UpdateTable_Club = exec sp_UpdateTable_Club
Button that Runs the process:
Private Sub btn_Save_Click()
'Tests that a club has been selected
mod_ClubSelectTest.ClubSelectTest
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Creates values to be used by sp_UpdateTable_Club
strSQL = "exec sp_UpdateTable_Club '" & txt_Club_ID & "'," & _
" '" & txt_FSM_ID & "', '" & txt_Receiver_ID & "', '" & txt_ClubType_ID & "', '" & txt_ClubNumber & "'," & _
" '" & txt_ClubName & "', '" & txt_EIN & "', '" & chk_1st & "', '" & chk_2nd & "', '" & chk_3rd & "'," & _
" '" & chk_4th & "', '" & cbo_MtgDay_Member & "', '" & txt_MtgTime_Member & "'," & _
" '" & cbo_MTGDay_Trustees & "', '" & txt_MTGTime_Trustees & "', '" & txt_Notes & "'," & _
" '" & txt_Phone_Primary & "', '" & txt_Extension_Primary & "', '" & txt_Phone_Secondary & "'," & _
" '" & txt_Extension_Secondary & "', '" & txt_Address_1_P & "', '" & txt_Address_2_P & "'," & _
" '" & txt_City_P & "', '" & cbo_State_P & "', '" & txt_ZIP_P & "'," & _
" '" & Nz(txt_Address_1_M, Null) & "', '" & txt_Address_2_M & "'," & _
" '" & txt_City_M & "', '" & cbo_State_M & "', '" & txt_ZIP_M & "'," & _
" '" & Opt_AffliationAgreemet & "', '" & opt_990 & "', '" & opt_ByLaws & "', '" & opt_HouseRules & "'," & _
" '" & opt_Officers & "', '" & opt_TrialCommittee & "', '" & opt_FinanceCommitee & "'," & _
" '" & opt_Auditor & "', '" & opt_Invoice & "', '" & opt_Insurance & "', '" & txt_CharterSuspended & "'," & _
" '" & txt_AgentAssigned & "', '" & opt_CharterRequest_Type & "', '" & txt_RequestedDate & "' "
CurrentDb.QueryDefs("qry_PT_sp_UpdateTable_Club").SQL = strSQL
DoCmd.SetWarnings False
DoCmd.OpenQuery "qry_PT_sp_UpdateTable_Club"
DoCmd.SetWarnings True
End Sub
Stored Procedure sp_UpdateTable_Club
#Club_ID int,
#FSM_ID int,
#Recever_ID int,
#ClubType_ID int,
#ClubNumber int,
#ClubName nvarchar(50),
#EIN nvarchar(50),
#MTGWeek_1 bit,
#MTGWeek_2 bit,
#MTGWeek_3 bit,
#MTGWeek_4 bit,
#MTGDay_Members int,
#MTGTime_Members nvarchar(5),
#MTGDay_Trustees int,
#MTGTime_Trustees nvarchar(5),
#Notes nvarchar(max),
#PhonePrimary nvarchar(15),
#ExtensionPrimary nvarchar(6),
#PhoneSecondary nvarchar(15),
#ExtensionSecondary nvarchar(15),
#Address1_P nvarchar(50),
#Address2_P nvarchar(50),
#City_P nvarchar(50),
#State_Province_ID_P nvarchar(50),
#PostalCode_P nvarchar(50),
#Address1_M nvarchar(50),
#Address2_M nvarchar(50),
#City_M nvarchar(50),
#State_Province_ID_M nvarchar(50),
#PostalCode_M nvarchar(50),
#AffiliationAgreement bit,
#990 bit,
#ByLaws bit,
#HouseRules bit,
#Officers bit,
#TrialCommittee bit,
#FinanceCommittee bit,
#Auditor bit,
#Invoice bit,
#Insurance bit,
#CharterSuspended date,
#ReceiverAssigned date,
#CharterRequest_Type_ID int,
#RequestedDate date
AS
Begin
UPDATE Club set FSM_ID = #FSM_ID,
Receiver_ID = #Recever_ID,
ClubType_ID = #ClubType_ID,
ClubNumber = #ClubNumber,
ClubName = #ClubName,
EIN = #EIN,
MTGWeek_1 = #MTGWeek_1,
MTGWeek_2 = #MTGWeek_2,
MTGWeek_3 = #MTGWeek_3,
MTGWeek_4 = #MTGWeek_4,
MTGDay_Members = #MTGDay_Members,
MTGTime_Members = #MTGTime_Members,
MTGDay_Trustees = #MTGDay_Trustees,
MTGTime_Trustees = #MTGTime_Trustees,
Notes = #Notes,
PhonePrimary = #PhonePrimary,
ExtensionPrimary = #ExtensionPrimary,
PhoneSecondary = #PhoneSecondary,
ExtensionSecondary = #ExtensionSecondary,
Address1_P = #Address1_P,
Address2_P = #Address2_P,
City_P = #City_P,
State_Province_ID_P = #State_Province_ID_P,
PostalCode_P = #PostalCode_P,
Address1_M = #Address1_M,
Address2_M = #Address2_M,
City_M = #City_M,
State_Province_ID_M = #City_M,
PostalCode_M = #PostalCode_M,
AffiliationAgreement = #AffiliationAgreement,
[990] = #990,
ByLaws = #ByLaws,
HouseRules = #HouseRules,
Officers = #Officers,
TrialCommittee = #TrialCommittee,
FinanceCommittee = #FinanceCommittee,
Auditor = #Auditor,
Invoice = #Invoice,
Insurance = #Insurance,
CharterSuspended = #CharterSuspended,
ReceiverAssigned = #ReceiverAssigned,
CharterRequest_Type_ID = #CharterRequest_Type_ID,
RequestedDate = NULLIF(#RequestedDate, CONVERT(DATE, '1900-1-1')
Where Club_ID = #Club_ID;
end
SELECT * From Club;

UPDATE Query in MS Access using SQL in VBA with declared strings

been a while since I've posted so please forgive any formatting issues. Looking to update a table field with a value from another record in the same field in the same table.
I've declared two strings, and the strDeal string is coming back with the correct values when debugging. However when I introduce the string into the sql statement I can't the query to update the field. I'm not sure exactly what isn't working correctly, so any help would be appreciated.
The basics are I'm trying to update the Case_Qty field with a value from the same field in the same table based on the returned value from a subquery. Thanks!
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = Forms!frmStructNoDASetup!Structure_Name AND [FG_Ind] = 0 AND [Deal_No] < Forms!frmStructNoDASetup!Deal_No")
strSQL = "UPDATE tblStructuresNoDAworking SET tblStructuresNoDAworking.Case_Qty = (SELECT [Case_Qty] FROM tblStructuresNoDAworking WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL (strSQL)
Try this where you concatenate the values from the form:
Dim strDeal As String
Dim strSQL As String
strDeal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & strDeal & "') " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = '" & Me.Deal_No.Value & "'"
DoCmd.RunSQL strSQL
For numeric Deal:
Dim Deal As Long
Dim strSQL As String
Deal = DMax("[Deal_No]", "[tblStructuresNoDAworking]", "[Structure_Name] = '" & Forms!frmStructNoDASetup!Structure_Name & "' AND [FG_Ind] = 0 AND [Deal_No] < '" & Forms!frmStructNoDASetup!Deal_No & "'")
strSQL = "UPDATE tblStructuresNoDAworking " & _
"SET tblStructuresNoDAworking.Case_Qty = " & _
" (SELECT [Case_Qty] " & _
" FROM tblStructuresNoDAworking " & _
" WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Deal & ") " & _
"WHERE [Structure_Name] = '" & Me.Structure_Name.Value & "' AND [Deal_No] = " & Me.Deal_No.Value & ""
DoCmd.RunSQL strSQL

DCount syntax error

Why does DCOUNT cause a syntax error?
DCount("[Commercial_name] + [Producer] + [Start_date]", "[Cleaning product]", "[Commercial_name] = '" & Me.PP.Value & "' AND [Producer] = '" & Me.PR.Value & "' AND [Start_date] = '" & Me.Rdate.Value & "'")
On the VBA page is all on one line
It's the date:
DCount("*", "[Cleaning product]", "[Commercial_name] = '" & Me!PP.Value & "' And [Producer] = '" & Me!PR.Value & "' And [Start_date] = #" & Format(Me!Rdate.Value, 'yyyy\/mm\/dd') & "#")

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

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.