?
Solved

VB Delete records from mdb tables

Posted on 2012-03-13
5
Medium Priority
?
177 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 1000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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