Solved

Updating access database using buttons in vb.net

Posted on 2013-11-05
6
424 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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