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
Solved

Updating access database using buttons in vb.net

Posted on 2013-11-05
6
452 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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