Solved

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

Posted on 1998-07-29
8
147 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

10 Experts available now in Live!

Get 1:1 Help Now