0

Ive been trying to find a solution for this problem without any success:

I'm using VB.NET and I need to read and update records from an Excel file. I use the OleDB API for that. It works fine for all the reading, but impossible to write to the file (Update or Insert queries)

Here is what I have:

My connection string:

Public connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\...\Resources\attempt.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'"

Select query that works fine:

        Dim checkQuery = "SELECT * FROM [Sheet1$] WHERE [TravellerPN] = @T"
        Try
            Using connection As New OleDb.OleDbConnection(Form.connString)

                Dim checkCommand As New OleDbCommand(checkQuery, connection)
                checkCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN

                connection.Open()

                Dim reader As OleDbDataReader = checkCommand.ExecuteReader()
                Dim path As String = ""

                While reader.Read()
                    path = reader("Datapath").ToString
                End While

                reader.Close()
                MsgBox(PN & " " & DP & " " & path,,)

If a record for the part number entered doesnt exist, and nothing is returned, insert a new line

                If path = "" Then
                    Dim addQuery = "INSERT INTO [Sheet1$] ([TravellerPN],[Datapath]) VALUES (@T, @D)"

                    Dim addCommand As New OleDbCommand(addQuery, connection)

                    addCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN
                    addCommand.Parameters.Add("@D", OleDbType.VarChar).Value = DP

                    Dim rows As Integer = addCommand.ExecuteNonQuery()

And this was where it returns the error.

                    MsgBox(rows & " row added!",, "") 'Never diplayed

And other query that doesn't work either:

                Else 'If does exist, confirm replacement'
                    Dim replaceResponse = MsgBox("A path already exists for " & PN & "." & vbNewLine & "Do you want to replace " & path & " with " & DP & "?", MsgBoxStyle.YesNo, "Overwrite previous datapath?")

                    Dim replaceQuery = "UPDATE [Sheet1$] SET [Datapath] = @D WHERE [TravellerPN]=@T"

                    Dim replaceCommand As New OleDbCommand(replaceQuery, connection)
                    replaceCommand.Parameters.Add("@D", OleDbType.VarChar).Value = DP
                    replaceCommand.Parameters.Add("@T", OleDbType.VarChar).Value = PN

                    Dim rows As Integer = replaceCommand.ExecuteNonQuery()
                    MsgBox(rows & " row updated!",, "")
                End If

                connection.Close()
            End Using

I've tried to fix the issue: it could be caused by permissions, so I authorized even guests accounts to modify my folder.

If it were a ReadOnly mode in the connection: I tried adding Mode=ReadWrite but my connection String didn't work after that. (That option seems only available for ADO connections?)

I tried running the app as administrator

And finally, I'm posting this here hoping to get some help. Sorry or the long post, I was trying to give all the elements that could potentially be a problem.

Thanks.

Ali
  • 3
  • 1
  • 7
  • Another SO post marked as the answer is stating to remove the IMEX part from the connection string. – Charles May Jul 11 '17 at 14:20
  • Omg why so easy. Thanks so much, sorry I thought I looked well but didn't read that post apparently. Thanks again! – Ali Jul 11 '17 at 14:22

0 Answers0