# Update In SQL Table using VB



## Focus2 (Nov 30, 2011)

Im trying to update in table using visual basic. Basically what Im trying to do is select a record from its date, in update the rows. Can someone help. I provided my coded below.



Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLConnection As New SqlConnection
Dim SQLSelect As New SqlCommand
Dim SQLinsert As New SqlCommand
Dim SQLUPDATE As New SqlCommand

Try
SQLConnection.ConnectionString = "Server=10.53.252.221;UID=phasqladmin;PWD=i82dothis;Database=PhabsQuality;Integrated Security=False"
SQLUPDATE.Connection = SQLConnection
SQLUPDATE.CommandText = ("Update [Tracking_Board] Set [Issue Description]= '" & TextBox1.Text & "', [Stations] = '" & TextBox2.Text & "' , [Containment] = '" & ComboBox1.Text & "', [Root Cause Analysis] = '" & ComboBox2.Text & "', [Corrective Action Implemented] = '" & ComboBox3.Text & "', [Error Proof / Detection] = '" & ComboBox4.Text & "', [Corrective Action Verified] = '" & ComboBox5.Text & "', [PFMEA / CP Updated] = '" & ComboBox6.Text & "', [Standardized Work Operator] = '" & ComboBox7.Text & "', [Horizontal Deployment] = '" & ComboBox8.Text & "', [Date Closed] = '" & ComboBox9.Text & "', [Overall Status] = '" & ComboBox10.Text & "' where [Date Open] = " & DateTimePicker1.Value & " and [Next Date of Owner Report out to Staff] = " & DateTimePicker2.Value & " ") 
MessageBox.Show(SQLUPDATE.CommandText)
SQLConnection.Open()
SQLUPDATE.ExecuteNonQuery()
Catch ex As Exception
End Try
SQLConnection.Close()
SQLUPDATE.Dispose()
MessageBox.Show("Update Has Been Sent")


----------



## Focus2 (Nov 30, 2011)

Need Help..

I have a proram that insert but also update, in the file exist.


----------



## Focus2 (Nov 30, 2011)

Need Help..
I have a proram that insert but also update, in the file exist. 

But I have in problem, when I insert a record in the table, it keep prompting me the update message box I have in the code. How can I set the program to look at one thing at a time, meaning. If a file exist in a person wont to update, it will prompt the ,message box. But if the file doesnt exist, It will insert a new record.. My code is below.


Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLConnection As New SqlConnection
Dim SQLSelect As New SqlCommand
Dim SQLinsert As New SqlCommand
Dim SQLUPDATE As New SqlCommand
If NumericUpDown1.Value = "7" Then
MessageBox.Show("You have completed the Fast Response Tracking Board")
NumericUpDown1.Value = "1"
Exit Sub
Else
End If
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim iResponce As Integer
iResponce = MsgBox("Do You Wont To Make Changes To The Previous Day???", vbYesNo, "UPDATE PREVIOUS DAY...") ' This provides the YES, an No Box. That will give the user the option to choose from. If they will like to insert into the sql table or Update the previous data in the table...
If iResponce = vbYes Then ' They Clicked YES!
con.ConnectionString = "Server=10.53.252.22;UID=psql;PWD=i654656;Database=PhabsQuality;Integrated Security=False
con.Open()
cmd.Connection = con
cmd.CommandText = ("Update [Tracking_Board] Set [Issue Description]= '" & TextBox1.Text & "', [Stations] = '" & TextBox2.Text & "' , [Containment] = '" & ComboBox1.Text & "', [Root Cause Analysis] = '" & ComboBox2.Text & "', [Corrective Action Implemented] = '" & ComboBox3.Text & "', [Error Proof / Detection] = '" & ComboBox4.Text & "', [Corrective Action Verified] = '" & ComboBox5.Text & "', [PFMEA / CP Updated] = '" & ComboBox6.Text & "', [Standardized Work Operator] = '" & ComboBox7.Text & "', [Horizontal Deployment] = '" & ComboBox8.Text & "', [Date Closed] = '" & ComboBox9.Text & "', [Overall Status] = '" & ComboBox10.Text & "'where [Issue Number] ='" & NumericUpDown1.Value & " 'and [Date Open] = '" & DateTimePicker1.Value & "' ")
cmd.ExecuteNonQuery()
con.Close()
End If
Exit Sub
'Try
SQLConnection.ConnectionString = "Server=10.53.252.221;UID=phasqladmin;PWD=i82dothis;Database=PhabsQuality;Integrated Security=False"
SQLinsert.Connection = SQLConnection
SQLinsert.CommandText = "insert into Tracking_Board ([Overall Status], [Issue Number], [Issue Description], [Date Open], [Stations], [Owner], [Next Date of Owner Report out to Staff],[Containment], [Root Cause Analysis], [Corrective Action Implemented], [Error Proof / Detection], [Corrective Action Verified], [PFMEA / CP Updated], [Standardized Work Operator], [Horizontal Deployment], [Date Closed]) values ('" & ComboBox10.Text & "'," & NumericUpDown1.Value & ",'" & TextBox1.Text & "','" & DateTimePicker1.Value & "','" & TextBox2.Text & "','" & TextBox4.Text & "','" & DateTimePicker2.Value & "','" & ComboBox1.Text & "', '" & ComboBox2.Text & "', '" & ComboBox3.Text & "','" & ComboBox4.Text & "', '" & ComboBox5.Text & "', '" & ComboBox6.Text & "', '" & ComboBox7.Text & "', '" & ComboBox8.Text & "', '" & ComboBox9.Text & "')"
'DateTimePicker1.Value.Year
SQLinsert.Connection = SQLConnection
SQLConnection.Open()
SQLinsert.ExecuteNonQuery()
SQLConnection.Close()
'Catch ex As Exception
'MessageBox.Show(ex.ToString)
'End Try
MessageBox.Show("Update Has Been Sent")
End sub
End class


----------



## AceInfinity (Jan 21, 2012)

It's hard to understand your english, but since you already declare that your using the System.IO namespace you would do something like:

```
If File.Exist("Filepath string here") Then
	'Do something here as the file does exist
Else
	'Do something else if file does not exist
End If
```


----------

