Solved

VB Delete records from mdb tables

Posted on 2012-03-13
5
150 Views
Last Modified: 2012-03-14
I am trying to delete data containted in two tables within my windows application using vb. Here is the code I have so far...

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        If System.IO.File.Exists(DBName) Then
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            conn.BeginTransaction()
            Dim strDelete As String = "DELETE * FROM Hours"
            conn.ExecuteNonQuery()
            strDelete = "DELETE * FROM Tips"
            conn.ExecuteNonQuery()
        End If
        Return True
0
Comment
Question by:rawilken
  • 4
5 Comments
 

Author Comment

by:rawilken
ID: 37718501
I did some homework and came up with this...

    Public Function DeleteData() As Boolean
        Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        If System.IO.File.Exists(DBName) Then
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            Dim myCommand As New OleDb.OleDbCommand("DELETE * FROM Hours", conn)
            myCommand.ExecuteNonQuery()
            Dim myCommand1 As New OleDb.OleDbCommand("DELETE * FROM Tips", conn)
            myCommand1.ExecuteNonQuery()
        End If
        Return True
    End Function
0
 
LVL 6

Accepted Solution

by:
Peter Kiprop earned 500 total points
ID: 37718516
Hi rawilken,

Please try the below code

Public Function DeleteData() As Boolean
        Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        If System.IO.File.Exists(DBName) Then
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            Dim myCommand As New OleDb.OleDbCommand("DELETE  FROM Hours", conn)
            myCommand.ExecuteNonQuery()
            Dim myCommand1 As New OleDb.OleDbCommand("DELETE  FROM Tips", conn)
            myCommand1.ExecuteNonQuery()
        End If
        Return True
    End Function

Open in new window

0
 

Author Comment

by:rawilken
ID: 37721273
The code throws an error...

ERROR: Unable to cast object of type 'System.String' to type 'System.IO.StreamWriter'. at tw=)GetPath)_ & csv)

Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        conn.Open()
        Dim csv As String = "\Paryoll.csv"
        Dim tw As StreamWriter
        tw = (GetPath() & csv)
        Dim sql = strExport
        Dim consql As New OleDb.OleDbCommand
        consql.CommandText = sql
        consql.Connection = conn
        Dim datRead As OleDb.OleDbDataReader
        datRead = consql.ExecuteReader
        Do Until datRead.Read = False
            tw.WriteLine(datRead.GetString(0) & " , " & datRead.GetString(1) & " , " & datRead.GetString(2) & _
                         " , " & datRead.GetDouble(3) & " , " & datRead.GetString(4) & " , " & datRead.GetDouble(5))
        Loop
        tw.Close()
        conn.Close()
        Return True
0
 

Author Comment

by:rawilken
ID: 37721700
The delete does not work. There are no error. It just does not delete the data?

Here is the code...

Public Function DeleteData() As Boolean
        Dim DBName As String = "Time & Pay.mdb"
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & GetPath() & "\" & DBName
        If System.IO.File.Exists(DBName) Then
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            Dim myCommand As New OleDb.OleDbCommand("DELETE FROM Hours", conn)
            myCommand.ExecuteNonQuery()
            Dim myCommand1 As New OleDb.OleDbCommand("DELETE FROM Tips", conn)
            myCommand1.ExecuteNonQuery()
        End If
        conn.Close()
        Return True
    End Function
0
 

Author Closing Comment

by:rawilken
ID: 37722540
not completely resolved
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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