Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 118,887 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 2,055 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Delete data from a Microsoft Access database

 
Reply to this topicStart new topic

Delete data from a Microsoft Access database

Nava_gini
post 1 Jul, 2008 - 02:46 AM
Post #1


New D.I.C Head

*
Joined: 17 Jun, 2008
Posts: 4

I want to create a code in vb.net 2008 that will delete data from its database table(Microsoft Access). The columns (attributes) in the database table are CWNo, Name, IC No, Nationality, Company, Join Date and Expiry Date.
Then it will insert data from a Microsoft Excel Sheet that contains the same attributes above into the same access database table.

Can you please give me a hint on how I can do this? Maybe you can provide me with an algorithm or even code that will be useful for me to complete the task above.

This post has been edited by Nava_gini: 1 Jul, 2008 - 03:03 AM
User is offlineProfile CardPM

Go to the top of the page


jjsaw5
post 1 Jul, 2008 - 05:08 AM
Post #2


D.I.C. Face

Group Icon
Joined: 4 Jan, 2008
Posts: 1,094



Thanked 5 times

Dream Kudos: 125

Expert In: HTML, CSS, Database,

My Contributions


QUOTE(Nava_gini @ 1 Jul, 2008 - 05:46 AM) *

I want to create a code in vb.net 2008 that will delete data from its database table(Microsoft Access). The columns (attributes) in the database table are CWNo, Name, IC No, Nationality, Company, Join Date and Expiry Date.
Then it will insert data from a Microsoft Excel Sheet that contains the same attributes above into the same access database table.

Can you please give me a hint on how I can do this? Maybe you can provide me with an algorithm or even code that will be useful for me to complete the task above.


You actually don't need code to do this.

In Access you have the ability to import spread sheets as tables in your database. What this allows you to do is any changes made to the spread sheet will automatically show up in your database and any changes made to the spread sheet(tables) in Access will show up in your spread sheet.

You can do this buy going to file....get external data and link tables

This will eliminate you having to delete the table every time access is opened to get new data.

But if this suggestion is not something you would like i can help you do what you originally asked.
User is offlineProfile CardPM

Go to the top of the page

dineeshd
post 1 Jul, 2008 - 05:16 AM
Post #3


D.I.C Addict

Group Icon
Joined: 30 Jun, 2008
Posts: 543



Thanked 15 times

Dream Kudos: 575
My Contributions


Hi Friend,

Try the below functions to solve your scenario. One important thing which I want to say before starting is that, below code has written for SQL Server Database, it means you have to make the necessary changes in the code to make it compatible with MS Access. I hope you won't mind that....

Import the below namesapces:
CODE

Imports System.Data
Imports System.Data.SqlClient


This sub will delete the data from databse:
CODE

Public Sub DeleteData()
        Dim sqlConString As String
        sqlConString = "user id=<PROVIDE USER ID>;password=<PROVIDE PASSWORD>;Database=<PROVIDE DATABASE NAME>;server=<PROVIDE SERVER NAME>;"
        Dim myConn As New SqlConnection(sqlConString)
        Dim DtAdapter As New SqlDataAdapter()
        Dim myDs As New DataSet()
        Try
            DtAdapter = New SqlDataAdapter("<WRITE YOUR SQL DELETE STATEMENT HERE>", myConn)
            DtAdapter.Fill(myDs)
            MessageBox.Show("Successfully Deleted", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch exp As Exception
            MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            myConn.Close()
            DtAdapter = Nothing
            myDs = Nothing
        End Try
    End Sub


Below 3 functions will help you out in trasnfering data from excel to database:
CODE

Private Function GetConnection(ByVal fstrExcelFileName As String) As String
        Return "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & fstrExcelFileName & ";" & _
          "Extended Properties=""Excel 8.0;"""
    End Function


CODE

Public Function GetDataFromExcel(ByVal foQuery As ArrayList, ByVal fstrDataSetName As String) As DataSet
        Dim oMyData As OleDb.OleDbDataAdapter = Nothing
        Dim oMyTable As DataTable = Nothing
        Dim oMyDataSet As DataSet = Nothing
        Dim strQuery As String
        Dim oConn As OleDb.OleDbConnection = Nothing
        Dim strXLFileName As String = "<PROVIDE YOUR XL FILE PATH HERE>"
        Try
            oConn = New OleDb.OleDbConnection()
            oConn.ConnectionString = GetConnection(strXLFileName)
            oConn.Open()
            oMyDataSet = New DataSet()

            If Not IsNothing(foQuery) AndAlso foQuery.Count > 0 Then
                Dim iLoopIndex As Integer
                Dim iLoopCount As Integer = foQuery.Count - 1

                For iLoopIndex = 0 To iLoopCount
                    strQuery = foQuery(iLoopIndex)
                    oMyData = New OleDb.OleDbDataAdapter(strQuery, oConn)
                    oMyTable = New DataTable()
                    oMyTable.TableName = Replace(strQuery.Substring(strQuery.LastIndexOf(CChar("[")) + 1), "$]", "")
                    oMyData.Fill(oMyTable)
                    oMyDataSet.Tables.Add(AddRowNo(oMyTable))
                Next
            End If
            oMyDataSet.DataSetName = fstrDataSetName
            Return oMyDataSet
        Catch oEx As Exception
            Throw oEx
        Finally
            oMyTable = Nothing
            oMyDataSet = Nothing
            oMyData.Dispose()
            oMyData = Nothing
            oConn.Dispose()
            oConn = Nothing
        End Try
    End Function


CODE

Private Function AddRowNo(ByVal foTable As DataTable) As DataTable
        Try
            Dim iLoopIndex As Integer
            Dim iLoopCount As Integer = foTable.Rows.Count - 1
            For iLoopIndex = 0 To iLoopCount
                foTable.Rows(iLoopIndex).Item("ROW_NO") = iLoopIndex + 2
            Next
            Return foTable
        Catch oEx As Exception
            Throw oEx
        End Try
    End Function



I hope this will be usefull to you....
Get back to me for any Clarification....

This post has been edited by dineeshd: 1 Jul, 2008 - 07:30 AM
User is offlineProfile CardPM

Go to the top of the page

Nava_gini
post 1 Jul, 2008 - 10:31 PM
Post #4


New D.I.C Head

*
Joined: 17 Jun, 2008
Posts: 4

I've tried using the code you gave me and for the line below,

Dim strXLFileName As String = "C:\Documents and Settings\SL\Desktop\Project\CW 2008I-Update.xls\Sheet 4"

Is the file path correct?

I am supposed to refer to only Sheet 4 of the Excel file.

This post has been edited by Nava_gini: 2 Jul, 2008 - 01:31 AM
User is offlineProfile CardPM

Go to the top of the page

dineeshd
post 2 Jul, 2008 - 01:35 AM
Post #5


D.I.C Addict

Group Icon
Joined: 30 Jun, 2008
Posts: 543



Thanked 15 times

Dream Kudos: 575
My Contributions


QUOTE(Nava_gini @ 1 Jul, 2008 - 10:31 PM) *

I've created the code below to create an Connection object to connect to the Access DB. Then delete all the data from the Access table using the code you gave me but I modified it to OleDBConnection.Is the code below correct?


Public Class frmMain

Code below to create a Connection object to connect to the Access DB.

CODE
Inherits System.Windows.Forms.Form
    
    Friend WithEvents OleConn As New System.Data.OleDb.OleDbConnection()
      
    Try
        OleConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data   source= C:\CWMaster.mdb"
        OleConn.Open()
        Catch exc As Exception
                lblStatus.Text = exc.Message.ToString
     End Try


Code below to delete the data from the database. For the highlighted line, can I just have it as OleConn(oledbConString)? This is because I have already declared OleCon as OleDBConnection in the create connection section (above code).

CODE
Public Sub DeleteData()
       Dim oledbConString As String
       oledbConString = "user id=<PROVIDE USER ID>;password=<PROVIDE PASSWORD>;Database=<PROVIDE DATABASE NAME>;server=<PROVIDE SERVER NAME>;"
      [b] Dim OleConn As New OleDBConnection(oledbConString)[/b]
       Dim DtAdapter As New OleDBDataAdapter()
       Dim myDs As New DataSet()
       Try
            DtAdapter = New OleDBDataAdapter("DELETE * FROM CwMaster", OleConn)
            DtAdapter.Fill(myDs)
            MessageBox.Show("Successfully Deleted", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch exp As Exception
                 MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK,  MessageBoxIcon.Error)
       Finally
            OleConn.Close()
            DtAdapter = Nothing
            myDs = Nothing
        End Try
End Sub


End Class



You can modified it to "OleDBConnection", even I was using the same before.

"Dim OleConn As New OleDBConnection(oledbConString)" is very well acceptable.
User is offlineProfile CardPM

Go to the top of the page

dineeshd
post 2 Jul, 2008 - 01:50 AM
Post #6


D.I.C Addict

Group Icon
Joined: 30 Jun, 2008
Posts: 543



Thanked 15 times

Dream Kudos: 575
My Contributions


QUOTE(Nava_gini @ 1 Jul, 2008 - 10:31 PM) *

I've tried using the code you gave me and for the line below,

Dim strXLFileName As String = "C:\Documents and Settings\SL\Desktop\Project\CW 2008I-Update.xls\Sheet 4"

Is the file path correct?

I am supposed to refer to only Sheet 4 of the Excel file.



You should not specify sheet name in the file path. You have to keep the sheet, from where you want to take data as the first sheet in the excel file. Or if you want to pick it from sheet 4 itself, then I don't have any solution right now, I will get back to you soon with a solution.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 03:13AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month