• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

VB Delete records from mdb tables

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
rawilken
Asked:
rawilken
  • 4
1 Solution
 
rawilkenAuthor Commented:
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
 
Peter KipropCommented:
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
 
rawilkenAuthor Commented:
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
 
rawilkenAuthor Commented:
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
 
rawilkenAuthor Commented:
not completely resolved
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now