# VB help! Delete from Access DB using VB form



## Akumos (May 8, 2010)

Hi all

Trying to delete a record from a database using a visual basic form.... I get an error sayingi convertin to type integer isn't valid!


Dim Movie_id_deleteString As String = btnDeleteMovie.Text

        Dim SQLStringDelete As String = "DELETE FROM movie WHERE id = " & CInt(Movie_id_deleteString) & ""


----------



## Kreij (May 8, 2010)

Try ... "DELETE FROM movie WHERE id=" & CInt(Movie_id_deleteString)

No ampersand and quote at the end.


----------



## Akumos (May 8, 2010)

Hi again Kreij!!

Tried it but still get this error:
Conversion from string "Delete" to type 'Integer' is not valid.


----------



## Akumos (May 8, 2010)

This is what I have:


----------



## Rusel (May 8, 2010)

Dim SQLStringDelete As String = "DELETE FROM movie WHERE id='" & Movie_id_deleteString & "'"


----------



## Akumos (May 9, 2010)

Thanks for your input russell!
I need to convert Movie_id_deleteString to an integer first otherwise I get a datatype mismatch error!


----------



## Rusel (May 9, 2010)

Make sure that Movie_id_deleteString is an actual number, otherwise it won't let you convert it to an integer.

```
Dim Movie_id_deleteString_INT As Integer
if Int32.TryParse(Movie_id_deleteString, out Movie_id_deleteString_INT) then
Dim SQLStringDelete As String = "DELETE FROM movie WHERE id='" & Movie_id_deleteString & "'"
else
MessageBox.Show("Movie_id_deleteString cannot be converted into an integer")
```


----------



## Akumos (May 9, 2010)

Hi Rusell

When I put that, it underlines the words out and the second Movie_id_deleteString_INT

It says out is not declared and expected comma!


----------



## regexorcist (May 9, 2010)

Rusel said:


> Dim SQLStringDelete As String = "DELETE FROM movie WHERE id='" & Movie_id_deleteString & "'"


If you using it as an integer, you shouldn't have single single quotes around it.
Dim SQLStringDelete As String = *"DELETE FROM movie WHERE id = " & Movie_id_deleteString & " "*
(no single quotes, spaces around equals sign is cleaner)

I also agree w/ Rusel's last statement about being a valid conversion,
is the text on your button a valid integer number?

Now if it's valid and the datatype has to actually be an integer, then convert prior to initialization of the delete string.

Dim Movie_id_deleteString As String = btnDeleteMovie.Text

*Dim converted_movie_id as Integer
converted_movie_id = CInt(Trim(Movie_id_deleteString))

Dim SQLStringDelete As String = "DELETE FROM movie WHERE id = " & converted_movie_id & ""*

Because of the string integer combination the ampersands may need to be + signs, you have to experiment.

Good luck and let me know...


----------



## Deleted member 3 (May 9, 2010)

So you read as string from a textbox cast it to int just to glue it to a string again?


----------



## regexorcist (May 9, 2010)

2 reasons, mainly troubleshooting
if the conversion to int fails, we've solved the problem.

If the sql is is fed to the db as an event, I'm suprised that the db knows anything about VB datatypes, just single quotes (string) or no single quotes (number).

The first section of my last post should work


----------



## Akumos (May 9, 2010)

I've got this working to add to a database using the help of Kreij.... But I cant get it to delete

I'm using thre number 1 as my ID!!! Here is what I have after using the suggestions of regExorcist


----------



## Rusel (May 9, 2010)

Are you sure that *btnDeleteMovie.Text* is a textbox and not a button?


----------



## regexorcist (May 9, 2010)

Rusel said:


> Are you sure that *btnDeleteMovie.Text* is a textbox and not a button?


Agreed, the text on the button probably says "Delete".

Like you say, he has probably extracted the text from the button instead of the text from the textbox.

As a test, he should create a new textbox (Textbox2) then try...
*Textbox2.Text = btnDeleteMovie.Text*

Then he will see why the conversion doesn't work, fix it and go back and use his original code
w/ textbox text and not button text.


----------

