Solved

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

Posted on 1998-07-29
8
145 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
Comment Utility
Edited text of question
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jollyk
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now