Click here to Skip to main content
15,898,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Somehow it lets me add and update when I run but it doesn't add/update in MS Access. So when I close the window and re-run again, the previous data that I input is not there anymore. Any idea why it happens?


VB
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\invData.mdb;"
        cnn = New OleDbConnection(connectionString)
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            'open connection if it is not yet open
            cnn.Open()
        End If
 
        cmd.Connection = cnn
        'check whether add new or update
        If Me.TextBox1.Tag & "" = "" Then
            'add new 
            'add data to table
            cmd.CommandText = "INSERT INTO NTIdb(ID, Status, DateDeliver, Client, POrder, StoreCode, StoreType, ProdUsage, ProdCategory, Brand, Description, Quantity, MainUnitSerial, HDD1, HDD2, OpticalDrive, Monitor, Keyboard, NetAdaptor, ProdKey, WTYEnt, AccExec, SalesOrder, SalesInv, DReceipt, ReceivedBy, WayBill, DueDate) " & _
                " VALUES(" & TextBox1.Text & ",'" & ComboBox1.SelectedItem() & "','" & _
                           DateTimePicker1.Value() & "','" & TextBox2.Text & "','" & _
                           TextBox3.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "','" & _
                           TextBox6.Text & "', '" & TextBox7.Text & "', '" & TextBox8.Text & "','" & _
                           TextBox9.Text & "', '" & NumericUpDown1.Value() & "', '" & TextBox10.Text & "','" & _
                           TextBox11.Text & "', '" & TextBox12.Text & "', '" & TextBox13.Text & "', '" & _
                           TextBox14.Text & "', '" & TextBox15.Text & "', '" & TextBox16.Text & "', '" & _
                           TextBox17.Text & "', '" & TextBox18.Text & "', '" & TextBox19.Text & "', '" & _
                           TextBox20.Text & "', '" & TextBox21.Text & "', '" & TextBox22.Text & "', '" & _
                           TextBox23.Text & "', '" & TextBox24.Text & "', '" & DateTimePicker2.Value() & "')"
 
            adptr.InsertCommand = New OleDbCommand(cmd.CommandText, cnn)
            cmd.ExecuteNonQuery()
        Else
            'update data in table
            cmd.CommandText = "UPDATE NTIdb " & _
                        " SET ID=" & TextBox1.Text & _
                        ", Status='" & ComboBox1.SelectedItem() & "'" & _
                        ", DateDeliver='" & DateTimePicker1.Value() & "'" & _
                        ", Client='" & TextBox2.Text & "'" & _
                        ", POrder='" & TextBox3.Text & "'" & _
                        ", StoreCode='" & TextBox4.Text & "'" & _
                        ", StoreType='" & TextBox5.Text & "'" & _
                        ", ProdUsage='" & TextBox6.Text & "'" & _
                        ", ProdCategory='" & TextBox7.Text & "'" & _
                        ", Brand='" & TextBox8.Text & "'" & _
                        ", Descrption='" & TextBox9.Text & "'" & _
                        ", Quantity='" & NumericUpDown1.Value() & "'" & _
                        ", MainUnitSerial='" & TextBox10.Text & "'" & _
                        ", HDD1='" & TextBox11.Text & "'" & _
                        ", HDD2='" & TextBox12.Text & "'" & _
                        ", OpticalDrive='" & TextBox13.Text & "'" & _
                        ", Monitor='" & TextBox14.Text & "'" & _
                        ", Keyboard='" & TextBox15.Text & "'" & _
                        ", NetAdaptor='" & TextBox16.Text & "'" & _
                        ", ProdKey='" & TextBox17.Text & "'" & _
                        ", WTYEnt='" & TextBox18.Text & "'" & _
                        ", AccExec='" & TextBox19.Text & "'" & _
                        ", SalesOrder='" & TextBox20.Text & "'" & _
                        ", SalesInv='" & TextBox21.Text & "'" & _
                        ", DReceipt='" & TextBox22.Text & "'" & _
                        ", ReceibedBy='" & TextBox23.Text & "'" & _
                        ", Waybill='" & TextBox24.Text & "'" & _
                        ", DueDate='" & DateTimePicker2.Value() & "'" & _
                        ", WHERE ID=" & TextBox1.Tag()
            cmd.ExecuteNonQuery()
        End If
        'refresh data in list
        RefreshData()
        'clear form
        Me.Button5.PerformClick()
 
        'close connection
        cnn.Close()
 
        '//To view Client
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\invData.mdb;"
 
        Dim ds1 As New DataSet
        Dim sql1 As String = "Select Client from NTIdb"
        cnn = New OleDbConnection(connectionString)
 
        Try
            cnn.Open()
            adptr = New OleDbDataAdapter(sql1, cnn)
            adptr.Fill(ds1)
 
            For a = 0 To ds1.Tables(0).Rows.Count - 1
                ComboBox2.Items.Add(ds1.Tables(0).Rows(a).Item(0))
            Next
 
            adptr.Dispose()
            cnn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
 
        'get data into list
        Me.RefreshData()
 
        If TextBox2.Text = sql1 Then
            TextBox2.Visible = False
            ComboBox2.Visible = True
        End If
Posted

1 solution

Whenever you are doing any code for database connection you should alwasys use try and catch block so that you can trap the error.
As per your code is find just one thing to mention is please debug code and take the query of insert and update and try to exexute in MSAccess

Also sometime due to that .mdb file is open it not able to update data so close .mdb while doing this operation.
 
Share this answer
 
Comments
PBubble 7-Mar-13 10:17am    
I tried what you suggested using these:

connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\invData.mdb;"
cnn = New OleDbConnection(connectionString)
Dim sql As String = "INSERT INTO NTIdb(ID, Status, DateDeliver, Client, POrder, StoreCode, StoreType, ProdUsage, ProdCategory, Brand, Description, Quantity, MainUnitSerial, HDD1, HDD2, OpticalDrive, Monitor, Keyboard, NetAdaptor, ProdKey, WTYEnt, AccExec, SalesOrder, SalesInv, DReceipt, ReceivedBy, WayBill, DueDate) " & _
" VALUES(" & TextBox1.Text & ",'" & ComboBox1.SelectedItem() & "','" & _
DateTimePicker1.Value() & "','" & TextBox2.Text & "','" & _
TextBox3.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "','" & _
TextBox6.Text & "', '" & TextBox7.Text & "', '" & TextBox8.Text & "','" & _
TextBox9.Text & "', '" & NumericUpDown1.Value() & "', '" & TextBox10.Text & "','" & _
TextBox11.Text & "', '" & TextBox12.Text & "', '" & TextBox13.Text & "', '" & _
TextBox14.Text & "', '" & TextBox15.Text & "', '" & TextBox16.Text & "', '" & _
TextBox17.Text & "', '" & TextBox18.Text & "', '" & TextBox19.Text & "', '" & _
TextBox20.Text & "', '" & TextBox21.Text & "', '" & TextBox22.Text & "', '" & _
TextBox23.Text & "', '" & TextBox24.Text & "', '" & DateTimePicker2.Value() & "')"
Try
cnn.Open()
adptr.InsertCommand = New OleDbCommand(sql, cnn)
adptr.InsertCommand.ExecuteNonQuery()
MsgBox("Data has been added!")
adptr.Dispose()
cnn.Close()
Catch ex As Exception

MsgBox(ex.ToString)
End Try

And it still does the same..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900