Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Visual basic problem: Object variable or With block variable not set.
 

Visual basic problem: Object variable or With block variable not set.

Hi, if anyone could help, id be mightly greatful, Im a beginner at this stuff so not the most experienced but Im starting to get my head round things in VB.

Anyhow Im getting the error that states:

Object variable or With block variable not set.


when trying to delete a single record from a table: here is the current code i have trying to achieve this operation:


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

sql = "Delete from Fault where TechID='" & TechIDM & "'"

Dim commandDelete As New OdbcCommand(sql)

Using connection As New OdbcConnection(constring)

commandDelete.Connection = connection

connection.Open()

commandDelete.ExecuteNonQuery()

connection.Close()

End Using

'Messagebox to give secure check to user about deleting data
If MsgBox("Are you sure you wish to proceed?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

commandDelete.Connection = connection

connection.Open()

commandDelete.ExecuteNonQuery()

connection.Close()

End If


error comes up on highlighted text. if anyone can shed some light on this id be so greatful, thank you to anyone who can help


Cheers
angliaboy  Thursday, November 26, 2009 10:02 PM
You have declared the variable Connection in the Using clause. That declaration terminates when the Using terminates, so connection is no longer valid when the error occurs. You need to expand the range of the Using to cover all usage of the Connection variable, or don't use the Using form for declaring it - simply declare itas a procedure-level variable.
Acamar  Thursday, November 26, 2009 10:29 PM
sql = "Delete from Fault where TechID='" & TechIDM & "'"

Using connection As New OdbcConnection(constring)

Dim commandDelete As New OdbcCommand(sql, connection)
connection.Open()
'Messagebox to give secure check to user about deleting data
If MsgBox("Are you sure you wish to proceed?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

commandDelete.ExecuteNonQuery()
connection.Close()
End If

End Using
connection.Close()



I would do better but try it please

Don't judge me, just Upgrade me. Thanks!
Malange  Thursday, November 26, 2009 10:35 PM
Ok thanks for the quick replies guys, there is no problem with your code as such which is a plus - it comes up with no errors, but nothing happens, I have the table on an automatic refresh but the record is not deleting, its just staying there?

Any clues
angliaboy  Thursday, November 26, 2009 10:47 PM

You can use google to search for other answers

Custom Search

More Threads

• Please help with my Date Calculator. VB 2005.
• Modifying data and displaying it in a datagrid
• Setting up and utilizing a TCP Client/Server application
• Convert Registry Hex Value to Date & Time
• Help me with Sockets "Please" Urgent!!!
• When will Classic ASP be desupported?
• Help with WebBrowser
• autocomplete comboBox
• Slow Timer
• Creating a CommandButton during Execution