Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Take apostrophe mark out of excel with sql query

I have a small problem.  When I run the following code it imports from an excel spreadsheet into a temp table in a sql DB.  Problem is that the excel file has apostrophe's in some of the address like so
118 Joe's Lane
this makes the import error out.  I am trying to make this automated so that I don't need to mess with any part of the operation.  Is there a way to run a query first to take out apostrophies from the excel spreadsheet before I import.  Thanks
Private Sub ImportAir()
        Dim connectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Personnel\Sad Listing.xls;Extended Properties='Excel 8.0;'"
        Dim connection As New OleDb.OleDbConnection(connectString)
        Dim command As New OleDb.OleDbCommand
        Dim myDataAdapter As New OleDb.OleDbDataAdapter
        Dim myDataSet As New DataSet
        connection.Open()
 
        command.Connection = connection
        command.CommandText = "select * from [sheet 1$]"
        myDataAdapter.SelectCommand = command
        myDataAdapter.Fill(myDataSet)
        myDataTable = New DataTable
        myDataTable = myDataSet.Tables(0)
        connection.Close()
 
        insertUpdateDelete("delete from tblTempPersonnel")
 
        For Each myDataRow In myDataTable.Rows
            sql = "insert tblTempPersonnel (SSN, Name, Grade, Status, HomePhone, Address, City, " _
                & "DOB, Unit, PasCode, YrsSvc, DAFSC, PAFSC, ETS) " _
                & "values " _
                & "('" & myDataRow(0) & "', '" & myDataRow(1) & "', '" & myDataRow(2) & "', '" & myDataRow(3) & "', '" & myDataRow(4) & "', '" & myDataRow(5) & "', " _
                & "'" & myDataRow(6) & "', '" & myDataRow(7) & "', '" & myDataRow(8) & "', '" & myDataRow(9) & "', '" & myDataRow(10) & "', '" & myDataRow(11) & "', " _
                & "'" & myDataRow(12) & "', '" & myDataRow(13) & "')"
 
            insertUpdateDelete(sql)
        Next
 
    End Sub

Open in new window

Avatar of Chris Luttrell
Chris Luttrell
Flag of United States of America image

I am not an Excel expert but from .Net apps we use parameters in the sql statement and put the text data into the paramters, then special characters do not "break" the sql string.  Again I am not qualified to give you the syntax in Excel, but maybe this will give you something to look for.
ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kdeutsch

ASKER

rrjegan17,

Thanks for the start in the right direction, I had to move it up a column because of the zero start and revised it as following and it works great.  Thanks
"'" & Replace(myDataRow(5), "'", "''") & "',