How to split a value in to multiple columns within a command statement VB.NET?

Good Afternoon,

Can someone tell me how to do a split of a value in a column in to multiple columns within a datatable?

I have a table within an access 2010 database that I need to export. The export is actually working fine with what I have at the moment.

The problem I am having is that in one of my columns “DebitAcc” in the access table, has values like this 0184-510554-320, I need to split these values in to 3 columns (SUCURSAL, NUMERO_BA and SUFIJO).

I have attempted this by using the “InStr” syntax in my command statement.

LEFT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], " - ") 1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUFIJO

Open in new window


But doing this is giving me an error “InvalidCastException was Unhandled” for “Conversion from string "SELECT Format(ChqNum, '000000') " to type 'Double' is not valid.”. But I cant understand why this error is being thrown because if I don’t include the above line in the statement, the code runs fine.

Example below of the datatable when I don’t include the line above to split the value.

dtmain
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        'Initialize the objects before use
        Dim dataAdapter As New OleDbDataAdapter
        Dim dataSet As New DataSet
        Dim command As New OleDbCommand
        Dim dtMain As New System.Data.DataTable()
        Dim conn As New OleDb.OleDbConnection

        'Assign connection string to connection object
        conn.ConnectionString = sConnString()
        command.Connection = conn
        command.CommandType = CommandType.Text

        'Run Select Statement
        command.CommandText = "SELECT Format(ChqNum, '000000') As REFERENCIA, Format(ChqDate, 'mm/dd/yyyy') As FECHA, Format(Amount, 'Standard') As IMPORTE, Format(PayTo) As NARRATIVA1, Format(Reference) As NARRATIVA2, LEFT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], " - ") 1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], " - ") 1) As SUFIJO, Format(Currency) As DIVISA FROM tblSBTHist"
        dataAdapter.SelectCommand = command

        Try

            Dim dc As System.Data.DataColumn
            Dim dr As System.Data.DataRow
            Dim colIndex As Integer = 0
            Dim rowIndex As Integer = 0

            'Fill data to datatable
            conn.Open()

            dtMain.Columns.Add("SECUENCIA")
            dtMain.Columns.Add("SUCURSAL")
            dtMain.Columns.Add("NUMERO_BA")
            dtMain.Columns.Add("SUFIJO")
            dtMain.Columns.Add("IMPORTE")
            dtMain.Columns.Add("DIVISA")
            dtMain.Columns.Add("FECHA")
            dtMain.Columns.Add("TRANSACION")
            dtMain.Columns.Add("REFERENCIA")
            dtMain.Columns.Add("NARRATIVA1")
            dtMain.Columns.Add("NARRATIVA2")
            dtMain.Columns.Add("NARRATIVA3")
            dtMain.Columns.Add("NARRATIVA4")
            dtMain.Columns.Add("GRUPO_CONT")
            dtMain.Columns.Add("COD_REFER")
            dtMain.Columns.Add("COD_USER1")
            dtMain.Columns.Add("COD_USER2")
            dtMain.Columns.Add("COD_ERROR")
            dtMain.Columns.Add("DES_ERROR")


            dataAdapter.Fill(dtMain)


            conn.Close()

Open in new window


End result of what the split will do within the datatable should look like this:

debitacc
Kindly advise.

Regards,
N
LVL 1
KevinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Your string concatenation is incorrect. You need to use a double double quote in some  places. Or just use a single quote. The places on your sql statement where you have " - " should actually be ""  - "". The line should read...


        command.CommandText = "SELECT Format(ChqNum, '000000') As REFERENCIA, Format(ChqDate, 'mm/dd/yyyy') As FECHA, Format(Amount, 'Standard') As IMPORTE, Format(PayTo) As NARRATIVA1, Format(Reference) As NARRATIVA2, LEFT([DebitAcc], Instr([DebitAcc], "" - "" ) +1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], "" - "" ) +1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], "" - "")+ 1) As SUFIJO, Format(Currency) As DIVISA FROM tblSBTHist"

Open in new window

OR
        command.CommandText = "SELECT Format(ChqNum, '000000') As REFERENCIA, Format(ChqDate, 'mm/dd/yyyy') As FECHA, Format(Amount, 'Standard') As IMPORTE, Format(PayTo) As NARRATIVA1, Format(Reference) As NARRATIVA2, LEFT([DebitAcc], Instr([DebitAcc],' - ') +1) As SUCURSAL, MID([DebitAcc], Instr([DebitAcc], ' - ') +1) As NUMERO_BA, RIGHT([DebitAcc], Instr([DebitAcc], ' - ')+ 1) As SUFIJO, Format(Currency) As DIVISA FROM tblSBTHist"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KevinAuthor Commented:
Thanks for spotting the error MlandaT.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.