Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2006, 03:03 PM
Authorized User
 
Join Date: Oct 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to insert,delete,update datagrids to MS Access

Hey guys how is everyone today?

I have a small issue with datagrids so far I'm able to retrieve data from MS access and populate the grid. The user let say puts in an id number and the grids
gets populated...now I want the user to be able to edit certain attributes from grid itself for example (lastname ,firstname, data of birth etc)

I am able to actually edit in the grid, but I don't know how to rebind it back once I press a submit button or better yet how am i able to reconnect all the new
fresh data that has been either inserted,deleted,update in the grid itself.

There are much more fields in this grid, but I decided to keep it nice and simple by using only 4 fields .

Well look at my noobish code and tell me what you think guys?

////////////////

 Dim connString As String = "Provider = Microsoft.JET.OLEDB.4.0; Data Source = X:\members.mdb; "
        Dim myConnection As OleDbConnection = New OleDbConnection
        Dim insertCmd As OleDbCommand = New OleDbCommand

        myConnection.ConnectionString = connString
        myConnection.Open()

        With insertCmd

            .Connection = myConnection
            .CommandText = " Select CreditApp_ID,FirstName,MiddleName,LastName,DOB From CreditApplication " & _
            "Where CreditApp_ID=@CreditApp_ID"

            .CommandType = CommandType.Text




            .Parameters.Add(New OleDbParameter("@CreditApp_ID", OleDbType.Char, 10))
            .Parameters.Add(New OleDbParameter("@FirstName", OleDbType.Char, 30))
            .Parameters.Add(New OleDbParameter("@MiddleName", OleDbType.Char, 1))
            .Parameters.Add(New OleDbParameter("@LastName", OleDbType.Char, 30))
            .Parameters.Add(New OleDbParameter("@DOB", OleDbType.Date))


                 .Parameters("@CreditApp_ID").Value = CreditID
            .Parameters("@FirstName").Value = CreditID
            .Parameters("@MiddleName").Value = CreditID
            .Parameters("@LastName").Value = CreditID

 End With



        'Now to declare a OLEDBDataAdapter object
        Dim Adapter As New OleDbDataAdapter

        'Now to declare a dataset
        Dim DataSet As New DataSet

        'Now to apply my Command to the DataAdapter
        Adapter.SelectCommand = insertCmd



        Try

            Adapter.Fill(DataSet)


            'To bind the Dataset to the DataGrid :)
            grdResults.DataSource = DataSet

            'Tell the DataGrid which table in the Dataset to use
            grdResults.DataMember = DataSet.Tables(0).TableName


            'Setting the Alternating Colors property to the Grid
            grdResults.AlternatingBackColor = Color.WhiteSmoke

            'Set the GridLineStyle Property :)
            grdResults.GridLineStyle = DataGridLineStyle.None

            'Set the SelectionBackColor and the Selection ForeColor Properties
            grdResults.SelectionBackColor = Color.LightGray
            grdResults.SelectionForeColor = Color.Black



        Catch OleDbExceptionErr As OleDbException
            Debug.WriteLine(OleDbExceptionErr.Message)
        Catch InvalidOperationExceptionErr As InvalidOperationException
            Debug.WriteLine(InvalidOperationExceptionErr.Messa ge)



        End Try


        'Cleaning up the Mess
        insertCmd.Dispose()
        insertCmd = Nothing
        Adapter.Dispose()
        Adapter = Nothing
        DataSet.Dispose()
        DataSet = Nothing

        myConnection.Dispose()
        myConnection = Nothing

///////////////////////////////

Keep in mind guys the code works well so there is no coding trouble to troubleshoot here....more like solution problem lol

Again objective is to have another button rebind the grid after user is done updating,deleting,inserting etc.

Im a noob at this whole binding stuff and would like to learn the best way to do this stuff.

Thankyou in advance as always :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert, update, delete in gridview-!!! URGENT !!! dagad ASP.NET 2.0 Basics 0 September 27th, 2006 06:24 AM
INSERT, UPDATE, DELETE dagad ASP.NET 2.0 Professional 0 September 27th, 2006 01:22 AM
Views-Insert/update/delete lily611 SQL Language 2 November 29th, 2004 03:41 AM
Update, insert and delete Trigger khautinh SQL Server 2000 2 September 17th, 2003 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.