Solved

VB Delete records from mdb tables

Posted on 2012-03-13
5
153 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 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