DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: update database based on datagrid selections

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    5

    update database based on datagrid selections

    I have an ADO connection to a database.
    I define a query and open a recordset using that connection.
    I set the datasource for a datagrid to the recordset (in the program, so it is unbound).
    The datagrid is populated and the user can scroll through the records: all of that works great.

    Here is where I am stuck:
    I do not want the user to make any edits to the database directly in the datagrid, but I do want the user to be able to flag currently selected record(s) in the database. So, I have a command button that I want to use to update one field in the database called "flag" (set it to a value of 1), for the currently selected record(s) in the datagrid.

    What do I put in the click event of the command button?

    Rick

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Does each record in the recordset have a unique ID? If so, you can execute an UPDATE query like this:

    UPDATE TableName SET Flag = 1
    WHERE IDColumn IN (ID1, ID2, ID3, etc.)

    ID1, ID2, ID3, etc. are the unique IDs of the selected items in the grid.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jan 2010
    Posts
    5
    Yes, I could capture the name of the unique ID for the selected record and create an update statement for that record. Now that I look at this again, perhaps I am misunderstanding the idea of datagrid selections: I see there is one (and only one) record with an arrow, but you can hold the ctrl key and highlight several of the records. Does highlighting the records create any type of selectionset? i.e. is there any way to loop through all of the highlighted records and capture their unique ID values?

    Also, After I build the UPDATE... WHERE... statement, I'm a little fuzzy on exactly how to execute the update command. (sorry, ADO and unbound recordsets are new to me)

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    The DataGrid's SelBookmarks property returns a collection of the highlighted rows. You can loop through them like this:
    Code:
    Dim ID As Integer
    Dim Bookmark As Variant
    For Each Bookmark In DataGrid1.SelBookmarks
        ID = DataGrid1.Columns("IDColumn").CellValue(Bookmark)
        ' Add ID to array, collection, etc.
    Next
    To execute an UPDATE query, create an ADO.Command object and set its CommandText property to your query. Then execute the Command object's Execute method.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Similar Threads

  1. problem in update database using visual basic
    By NJman in forum VB Classic
    Replies: 4
    Last Post: 07-23-2007, 07:54 AM
  2. HELP! update local database from internet database
    By smiley_sheinniz in forum VB Classic
    Replies: 0
    Last Post: 01-17-2007, 10:28 AM
  3. Pictures in a datagrid from a acces database
    By l_schuiling in forum .NET
    Replies: 0
    Last Post: 12-16-2006, 07:59 AM
  4. Offline database update * Advice *
    By Drew_gable in forum Database
    Replies: 2
    Last Post: 05-26-2006, 04:29 PM
  5. Update Database too slow!!!
    By Kika in forum VB Classic
    Replies: 1
    Last Post: 05-04-2000, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center