smk081
smk081

Reputation: 1151

ADO.NET with VB.NET - SELECT INTO query

I have been successful with setting up a connection and retrieving information from my MS Access database to my Windows Form in Visual Studio using VB.NET and SELECT statements with some of the sample code below.

    Dim dataset As New DataSet

    Dim datatable As New DataTable
    dataset.Tables.Add(datatable)

    Dim data_adaptor As OleDbDataAdapter
    data_adaptor = New OleDbDataAdapter("SELECT * FROM test1", odbconnect)

    data_adaptor.Fill(datatable)

However when I set my DataAdaptor to a query like below, I have run into some trouble executing a SELECT INTO query.

    data_adaptor = New OleDbDataAdapter("SELECT first_name INTO newtable FROM test1", odbconnect)  

I do not get any error but the table does not get created in the my access database. I understand that I would have to redeclare or declare a new, Data Adaptor to actually get information from the newly created "newtable" but if I execute this, then open my Access Database, "newtable" does not exists. However, if I run this query in Access, it works but I get a pop up box confirming that I want to paste data into a new table. I imagine this could be a possible hang up with working with MS Access in this manner. Is there some other setting or object I must use to execute a SELECT INTO query to create a new table?

Upvotes: 1

Views: 2560

Answers (1)

Steve
Steve

Reputation: 216353

Do not use a OleDbDataAdapter for this. Use simply a OleDbCommand and call ExecuteNonQuery

dbCmd = New OleDbCommand("SELECT first_name INTO newtable FROM test1", odbconnect)
cbCmd.ExecuteNonQuery()

Upvotes: 2

Related Questions