Solved

VB Delete records from mdb tables

Posted on 2012-03-13
5
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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