Solved

Updating access database using buttons in vb.net

Posted on 2013-11-05
6
535 Views
Last Modified: 2013-11-06
I am trying to update a database record when a user amends it. I have a textbox called 'txtColsTextBox' which stores the value and a button called 'btnSaveExit'. On the button click, I need to update the db with new value. How would I do this based on my code. I am thinking , i need to use me.validate function but not sure how to code. Thanks

        Dim connetionString As String
        Dim oledbCnn As OleDbConnection
        Dim oledbCmd As OleDbCommand
        Dim sql As String

        connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\csm\storage.accdb"
        sql = "SELECT Cols FROM Racks Where [Rack code] = '" & buttonName & "'"
            oledbCnn = New OleDbConnection(connetionString)
        Try
            oledbCnn.Open()
            oledbCmd = New OleDbCommand(sql, oledbCnn)
            Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            While oledbReader.Read
                'MsgBox(oledbReader.Item(0))
                txtColsTextBox.Text = oledbReader.Item(0)
            End While
            oledbReader.Close()
            oledbCmd.Dispose()
            oledbCnn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 21
ID: 39625427
I would recommend using an Update query.

Execute something like this:

Update Racks SET Cols = " & txtColsTextBox.Text  & " Where [Rack code] = '" & buttonName & "'"

Open in new window


** Aircode - Not tested
0
 

Author Comment

by:peter-cooper
ID: 39627526
@TheHiTechCoach

Would I place this in my button click event? Also, do I not need to reference an adapter of sorts to perform the update? Thanks
0
 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 500 total points
ID: 39627747
You would use this in the click event in place of your code using oledbReader.

try something like this:

Dim myConnection As New OleDb.OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\csm\storage.accdb")

Dim SQLCmd As New OleDbCommand
Dim SQLStr As String = "Update Racks SET Cols = " & txtColsTextBox.Text  & " Where [Rack code] = '" & buttonName & "'"

myConnection.Open()
SQLCmd.Connection = myConnection
SQLCmd.CommandText = SQLStr
SQLCmd.ExecuteNonQuery()

myConnection.Close()

Open in new window


also see:
http://www.codeproject.com/Articles/26801/Insert-Update-Delete-Search-Values-in-MS-Access-20

Insert, Update, Delete & Search Values in MS Access 2003 with VB.NET 2005

Introduction

This article and sample covers that "How to Insert, Update, Delete and Search Values to/from MS Access database 2003 with VB.NET 2005", Although VB.NET 2005 gives lot more methodologies to do this same job but I find this more easier, simplest and understandable for a beginner.
0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

Author Comment

by:peter-cooper
ID: 39627872
Thanks for the informative link and your help in solving my problem.
0
 

Author Closing Comment

by:peter-cooper
ID: 39627881
Thank you
0
 
LVL 21
ID: 39627979
You're welcome. Glad we could assist.
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question