Solved

Deleting an item from a Grid (vb 4.0, 32bits)

Posted on 1998-07-29
8
149 Views
Last Modified: 2010-05-03
I created a grid control on my form.  The grid filled from a database(Access).  I created a routine where I can add an item to my grid without any problems, but I can't seem to figure out how to delete that same item.  I declared the variable Table as Recordset and put in the following line of code:
Dim Table as Recordset
Set Table = dbTFH.OpenRecordset("Trucks", dbOpenTable)
Table.Index = "ID"  
Table.Seek "=", grdtrucks.Text  

When I use the 'Table.Delete' method I continually get an error message saying, "Run-Time error '3021', No current Record" and it points to my 'Table.Delete'.
Do you have any ideas on the matter?
0
Comment
Question by:jollyk
8 Comments
 

Author Comment

by:jollyk
ID: 1467132
Edited text of question
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1467133
Try:
  Table.Bookmark = Table.LastModified
  Table.Delete

I submit this as a comment.  If it solves your problem, let me know and I'll resubmit it as an answer.
0
 
LVL 1

Expert Comment

by:blwatkins
ID: 1467134
The problem is your seek statment did not find a record, therefore your record pointer is located on the EOF marker.

You need to check to see if your seek statement found a record before you try and delete it.

do so by checking the nomatch property of your recordset, like so:

IF Not TABLE.nomatch then
  Delete your record here
 else
  it didn't find a record.
endif

or you can just check to see if the table is on the EOF by:

if Table.eof then, it didn't find a record.

Thanks Brad.



0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:jollyk
ID: 1467135
My seek statement did not find a record.  I tried Table.Movefirst before the seek statement but that didn't work.  So now the problem is related to the seek statement.
I tried
Table.Bookmark = Table.LastModified
Table.Delete
but that didn't work.  It still gives me the same "No current Record" error message.  Just to verify I also had the following code before the delete:
grdtrucks.Col = 0
Table.MoveFirst
Table.Index = "ID"
Table.Seek "=", grdtrucks.Text
Any suggestions?
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1467136
first off.....  I am assuming that ID is a long integer, and Text as string (that I know) so you need this, minimum

table.seek "=", clng(grdTrucks.Text)
if table.nomatch then
  msgbox "You did not select a record"
else
  table.delete
  table.movefirst
end if

Secondly... which grid are you using?  The grid could do it for you.  If you are using DBGrid that comes with VB, when you move to a record, it moves the pointer for you.


I would recommend making sure grdTrucks.Text is an ID
0
 

Author Comment

by:jollyk
ID: 1467137
I am NOT using DBGrid.  I am using Grid.  Should I switch and is the coding more or less the same?
"ID" is part of a table in the database.  Each Truck in the database is given a unique ID number.  ID is never declared as long.  There is no reason for that.  I am trying to perform a search on a record in a table and then delete the record I searched for.  I have tried all suggestions but nothing has helped.  Is it possible that my problem is elsewhere in the project?  Here is my code exactly as I have it(and it doesn't work):
    Dim Table As Recordset
       
    Set Table = dbTFH.OpenRecordset("Trucks", dbOpenTable)
   
    grdtrucks.Col = 0
    Table.MoveFirst
    Table.Index = "ID"
   
    Table.Seek "=", (grdtrucks.Text)
   If Table.NoMatch Then
        MsgBox "You did not select a record"
    Else
        Table.Delete
        Table.MoveFirst
    End If
0
 
LVL 6

Accepted Solution

by:
anthonyc earned 50 total points
ID: 1467138
you should switch, DEFINITELY. the coding is 100% easier.  You can bind the grid to your data control, and then you will not have to code delete, insert or update.



0
 

Author Comment

by:jollyk
ID: 1467139
I figured out the problem.  It was quite simple actually.  My search was focused on col = 0.  The problem was that the "ID" was in col = 6.  Thanks anyways for all your responses.  You guys were great.  I've never used DBGrid.  Is there a place I could go to find out how to use it properly?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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