Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Updating access database using buttons in vb.net

Posted on 2013-11-05
6
Medium Priority
?
764 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

598 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