Passing a NULL value to a column from an Update query - sql

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;

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

Date exception in German windows during insert MS access vb.net

I have written an insert query which inserts datetime along with other columns. It works fine for all locations except when
my German client logs in and runs the application it gives him below error. I have formatted the datevalue to yyyy-mm-dd to make
culture independent.
MS access database is stored in a server in US.
German client is running application from Germany.
strDateSubmit = dtpDateSub.Value.ToString("yyyy-MM-dd")
strSaveOSTR = "INSERT INTO " & strOSTR & " ([OSTR #],[OSTR Type],[# of Samples],[RA#],[Customer],[SKF #],[Test Description]," & _
"[TestLength],[TestUnit],[TestLengthDays],[Requestor],[Date Submitted],[Seals Avail],[Fixtures Available],[Peripherals Avail],[PO Avail]," & _
"[Machine Type],[Hours to Process],[Location],[Current Status],[ErrorsPresent],[ContaminType]" & SampleREcvd1 & ", [Emp_ID],[Industry])" & _
" Values ( '" & strOSTRNum & "', '" & cmbOSTRTypes.Text & "', " & intSamples & ", '" & strRA & "', '" & strCustomer & "', '" & strSKFNum & _
"', '" & strTestDescr & "', " & intTestLength & ", '" & strTestUnits & "', '" & txtTestLDays.Text & "', '" & strRequestor & "', #" & strDateSubmit & "#, '" & strSealAvail & _
"', '" & strFixtAvail & "', '" & strPheriAvail & "', '" & strPOAvail & "', '" & strMachineClass & "', " & intHrstoProc & ",'" & g_objProp.Location & _
"', '" & strStatus & "', '" & ErrorsPresent & "', '" & ContaminationType & "'" & SampleREcvd2 & ", '" & emp_id & "', '" & Industry & "')"
Error: Syntaxfehler in Datum in Abfrageausdruck '#01.02.2016'
Do yourself a favor: Scrap the dynamic SQL and use a parameterized query, e.g.,
sampleSQL = "INSERT INTO TableName ([Date Submitted]) VALUES (?)"
Dim cmd = New OleDbCommand(sampleSQL, conn)
cmd.Parameters.AddWithValue("?", dtpDateSub.Value)
cmd.ExecuteNonQuery()
That way you can use the actual DateTime value returned by the DateTimePicker. You don't have to be concerned with locale settings or string formats or any of those aggravations.
You may need a more strict format of the date expression string:
strDateSubmit = dtpDateSub.Value.ToString("yyyy'/'MM'/'dd")
This will always return a date like "2016/02/01" (no dots or dashes), and it will concatenate correctly here:
"', #" & strDateSubmit & "#, '"
as you intend.

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.