Solved

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

Posted on 1998-07-29
8
146 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

895 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

18 Experts available now in Live!

Get 1:1 Help Now