Results 1 to 12 of 12

Thread: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Hello, I have a DataGrid1 hooked up to adodc1, and an access file connected with adodc1 through settings (not code). The problem is when I delete a row from the middle of DataGrid1 using a DblClick on that row .. the row goes away just fine -which is what I want-, but when I hit the CmdLog button to log a new entry from the end of the DataGrid .. all rows after where the DblClick was disappear. My database is Access 2003, and I am working on VB6.


    Two Questions:

    1- How to delete a row from the middle of the DataGrid1, and get the rows before & after it stitched together?
    2- Is there a way to make the data saved all at once upon an event i.e., button click or closing the program?

    Here is my code

    Code:
     Private Sub DataGrid1_DblClick()
    
    On Error GoTo Rora
    
    DataGrid1.Refresh
    Adodc1.Recordset.Delete
    Adodc1.Refresh
    Adodc1.Recordset.Update
    'Adodc1.Recordset.Refresh
    
    Rora:
    Exit Sub
    
    End Sub
    
    Private Sub CmdLog_Click()
    
    Adodc1.Refresh
    Adodc1.Recordset.AddNew
    DataGrid1.Columns(0) = Variable
    DataGrid1.Columns(1) = Variable1
    DataGrid1.Columns(2) = Variable2
    DataGrid1.Columns(3) = Variable3
    DataGrid1.Columns(4) = Variable4
    DataGrid1.Columns(5) = Variable5
    DataGrid1.Columns(6) = Variable6
    Adodc1.Recordset.MoveLast
    
    End Sub

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,908

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Moved to VB6 as this isn't really a DB question. You're likely to get better answers here.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    It should be as simple as just deleting the record.

    What service pack do you have installed? You should be running SP6 if you have no SP installed perhaps that could be your problem. I personally do not use the adodc in part because early on there was a bug that caused a problem when it was bound to a grid. The bug was fixed in an early service pack but I never went back to using it as I like the control I get from using straight up code instead.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    I have no service pack installed, but I just downloaded service pack 6 from Microsoft's website; it seems like the extracted files don't run in any folder. What folder should I extract them into?

    If a grid can be saved without the use of adodc that would be great! Can it?
    Last edited by SaudVb; Aug 13th, 2020 at 05:18 PM.

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,439

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Link:

    https://www.microsoft.com/en-us/down...s.aspx?id=5721

    Installation Instructions:

    Before starting the download, create a download directory on your computer. If your internet connection is less than 300K, it is recommended that you run the multi-part download by following the "More Information" link at the upper right, then clicking "Download Now."

    Click "Download" to begin downloading the single download. When prompted by the download software, choose the option "Save this program to disk" and click OK. Then select the directory you created on your computer.

    Run the file from the download directory. When prompted, select the same directory you created on your computer. You will be expanding the contents of the EXE into this directory.

    Run SetupSP6.exe from the download directory. When you accept the terms of the electronic End User License Agreement (EULA) the setup software will replace the appropriate files in your Visual Basic 6.0 installation.

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Trying to fight with the DataGrid in code isn't the best option. It already supports row-deletion:

    Name:  sshot1.png
Views: 921
Size:  2.1 KB
    Data

    Name:  sshot2.png
Views: 996
Size:  2.3 KB
    Row Selector clicked

    Name:  sshot3.png
Views: 982
Size:  1.9 KB
    DELete key pressed, deleting the row

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Mr. jdc2000,

    Thank you. This is what I got. They're two pictures.

    Name:  vb6 sp6.PNG
Views: 979
Size:  4.9 KB

    Name:  vb6 sp6 2.PNG
Views: 989
Size:  5.0 KB

    Name:  vb6 sp6 3.PNG
Views: 990
Size:  5.3 KB
    Last edited by SaudVb; Aug 13th, 2020 at 06:50 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Mr. dilettante,

    Very good, thank you. But The issue isn't about the ability to delete; the issue is when you delete a row then insert a new row, all rows get deleted except for the top one.
    Last edited by SaudVb; Aug 13th, 2020 at 06:28 PM.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    To use a code based RS rather than the adodc you have to create a rs and bind the grid to it.
    There is a handy dandy little data form wizard in VB that will create a basic data form for you. You can choose to use datagrid there and you can choose control, code or class. When you choose code it will create a few routines that open the selected table and bind it to the grid, also will add code for adding, deleting and other stuff. It is far from perfect but it can give you an idea of how to do this through code.

    To use the data form wizard just go to project/add form and select the data form wizard from the list of templates that show up.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2020
    Posts
    29

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    DataMiser,

    You're right, I learned this from "brucevde" https://www.vbforums.com/showthread....30#post5489430

    But it would be great to also learn how to save this coded grid so the user doesn't lose his data after restarting the app. Whether to save within the app or as an export/import file .. doesn't matter.
    Last edited by SaudVb; Aug 13th, 2020 at 07:03 PM.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Quote Originally Posted by SaudVb View Post
    DataMiser,

    You're right, I learned this from "brucevde" https://www.vbforums.com/showthread....30#post5489430

    But it would be great to also learn how to save this coded grid so the user resumes working after restarting the app.
    Are you talking about the in memory recordset from that thread? Not sure what you are trying to do or ask at this point.
    The dataform wizard approach uses a db. It is simple enough to write something to an ini file so that you can pick up in the same spot the next time the user runs the app if you want. As for an in memory rs well first you have to save the rs to disk then you would have to reload it when the run the program again. Definitely can be done but makes a lot more sense to use a database.

  12. #12
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to Delete a DataGrid1 Row Without Deleting All the Rows After? VB6

    Here I have entered Row 1, Row 2, Row 3, and Row 4. Then I deleted Row 2. Finally I added New Row:

    Name:  sshot.png
Views: 1001
Size:  2.0 KB

    In this case I have also formatted the "Price" column in the DataGrid, as well as hiding the Id column:

    Code:
    Private Sub Form_Load()
        Dim Fmt As StdFormat.StdDataFormat
    
        Set Fmt = New StdFormat.StdDataFormat
        With Fmt
            .Type = fmtCustom
            .Format = "$#0.00"
            .NullValue = vbNullString
        End With
        With DataGrid1
            .WrapCellPointer = True
            .TabAction = dbgGridNavigation
            .Columns("Id").Visible = False
            Set .Columns("Price").DataFormat = Fmt
        End With
    End Sub

    So I don't see the problem. Most likely you have broken something in code (probably fiddling with DataGrid1.Recordset in code, always a bad idea).

    Don't use the old ADODC at all. As I said in another thread, the DataEnvironment replaced it in 1998 and is far more powerful and flexible anyway. But even if you insist on using the clunky old ADODC it should not be broken enough to cause the problem you are seeing.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width