Solved

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

Posted on 1998-07-29
8
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 44
Spell Check in VB6 13 148
Macro Excel - Multiple If conditions 2 94
Count text color with conditional formating in Excel 4 68
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…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

751 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