VB.NET - OleDb write to Excel file works in XP but gives "must use updateable query" error in Windows 7

Posted on 2011-05-04
Last Modified: 2012-05-11
I'm writing a VB.NET application in VS2008 that uses Excel as a back-end to store some simple data. The entire application code is attached below. On Windows XP, this works exactly as intended, but on Windows 7, I get the following error:

System.Data.OleDb.OleDbException: Operation must use an updateable query.

Can anyone tell me how to update my code to make it Windows 7 compatible?
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        Dim x As Integer = 0
        If Not (txtFirstName.Text = "" Or txtLastName.Text = "" Or txtCompany.Text = "" Or txtEmail.Text = "") Then

            Dim FirstName, LastName, Email, Company, Title, Phone As String

            FirstName = txtFirstName.Text
            LastName = txtLastName.Text
            Email = txtEmail.Text
            Company = txtCompany.Text
            Title = txtTitle.Text
            Phone = txtPhone.Text

                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim MyCommand As New System.Data.OleDb.OleDbCommand
                MyConnection = New System.Data.OleDb.OleDbConnection _
                ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Data.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;""")

                MyCommand.Connection = MyConnection
                MyCommand.CommandText = "INSERT INTO [Sheet1$] ([First Name],[Last Name],[Email],[Company],[Title],[Phone]) values ('" & FirstName & "','" & LastName & "','" & Email & "','" & Company & "','" & Title & "','" & Phone & "')"

            Catch ex As Exception
            End Try

            txtFirstName.Text = ""
            txtLastName.Text = ""
            txtEmail.Text = ""
            txtCompany.Text = ""
            txtTitle.Text = ""
            txtPhone.Text = ""

            x = MsgBox("Please fill out all required fields.", vbOKOnly, "Required fields empty")
        End If

    End Sub

    Private Sub releaseObject(ByVal obj As Object)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub

End Class

Open in new window

Question by:LCMSdev
    LVL 83

    Accepted Solution

    Your Excel file is in C root. Windows 7 does not allow programs to access C root, windows folder, program files folder and many other places. Try moving it to another drive or within a folder such as desktop.
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Follow the CodeCruiser instructions, or run your program with administrative privileges to make it work (I prefer the CodeCruiser advice).

    Author Closing Comment

    Yup, that did it. Thanks!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now