Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,075 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,799 people online right now. Registration is fast and FREE... Join Now!




ALTER queries to Access trough VB.NET

 
Reply to this topicStart new topic

ALTER queries to Access trough VB.NET

pe_mitev
2 Feb, 2008 - 07:26 AM
Post #1

D.I.C Head
Group Icon

Joined: 28 Dec, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Hello,
I managed to execute queries like SELECT * FROM a, but I cannot execute the ones with ALTER, because the following error appear: Cannot execute data definition statements on linked data sources.

I read something about Back-end & Front-end queries(they are different), but at end I didn't succeed. I've tried 15 different source codes and each with problems.
The last I tried is:

CODE


        Dim strConnectionString As String

        strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & file_path

        Dim oConnection As New System.Data.OleDb.OleDbConnection(strConnectionString)

        oConnection.Open()

        Dim oCommand As System.Data.OleDb.OleDbCommand

        oCommand = oConnection.CreateCommand()

        oCommand.CommandText = "Alter Table procProductsList Add NewColumn Text"

        oCommand.ExecuteNonQuery()


It is just very strange how it does not want to work and what would be the other sollution, that I haven't tried. I've tried this one too:

CODE

  Cn = New ADODB.Connection
        Cat = New ADOX.Catalog
        objTable = New ADOX.Table

        'Open the connection
        Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & file_path)

        'Open the Catalog
        Cat.ActiveConnection = Cn

        'Create the table


        objTable.Name = "procProductsList"
        objTable.Columns.Append("LastName", ADOX.DataTypeEnum.adVarWChar, 40)
        objTable.Columns.Append("ID", ADOX.DataTypeEnum.adInteger)
        objTable.Columns.Append("Department", ADOX.DataTypeEnum.adVarWChar, 20)

        ''append tables to database
        ADOXcatalog.Tables.Append(ADOXtable)
        Append the newly created table to the Tables Collection
        Cat.Tables.Append(objTable)


        ' clean up objects
        'objKey = Nothing
         objTable = Nothing
         Cat = Nothing
         Cn.Close()
        Cn = Nothing


The problem with it is that it is used only for new table to the current database and I cannot decide how to change it so it can be used to update a database, which
already exists before that moment. The error in this case is "Database already exists", which is enough proof that the script tries to create the database, not to update it.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 08:01 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
MSDN has a write-up on using Distributed Queries on linked servers/tables.
User is offlineProfile CardPM
+Quote Post

pe_mitev
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 08:24 AM
Post #3

D.I.C Head
Group Icon

Joined: 28 Dec, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Thanks, but I understood that I cannot use it for ALTER. I am asking what I should use for ALTER?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 08:40 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
First question, why alter the table at run-time, why not just add the column at design time?
User is offlineProfile CardPM
+Quote Post

pe_mitev
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 09:34 AM
Post #5

D.I.C Head
Group Icon

Joined: 28 Dec, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Okay, I will tell you. This .mdb is created by one programm with some data. I have to add 8 more columns somehow and not on hand! So it should be at run-time. We have to check if they already exist and if not, they should be created. Then I have to use these fields and add additional data with my application.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 11:34 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(pe_mitev @ 2 Feb, 2008 - 12:34 PM) *

have to add 8 more columns somehow and not on hand! So it should be at run-time. We have to check if they already exist and if not, they should be created. Then I have to use these fields and add additional data with my application.


Create another table, like procProductsListEx, with the same primary keys as procProductsList, but no autoincrement. Put any extra columns in there that you need. Then just add all data to that table.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

pe_mitev
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 11:49 AM
Post #7

D.I.C Head
Group Icon

Joined: 28 Dec, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Won't be that very slow? I mean transferring all that data which exists before the operation... This is a kind of sollution but I think there should be a way to create "ALTER" queries. It can't be impossible! Thanks for the sollition, anyway. If nothing appears, I will try this but I prefer something else, using ALTER or alternative way.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 12:12 PM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
If you're altering a table to add columns, then those columns are empty. If you create a new table, it's empty. If you add data to the new columns, you update. If you add data to the new table, you update or insert. I'm not sure how it would be any more or less slow.

If complete flexibility, make a table with three columns: ExtendedData(ParentTable, ParentPK, Key, Value). Make the key and value varchar. Use them to add or drop extended data from an existing schema as you like.

ALTER ADD COLUMN, btw, will affect the entire table. It in effect creates a new table copies everything in, drops the old table, and then renames the new table. It's probably one of the slowest DDL operations you can do.

User is offlineProfile CardPM
+Quote Post

pe_mitev
RE: ALTER Queries To Access Trough VB.NET
2 Feb, 2008 - 01:48 PM
Post #9

D.I.C Head
Group Icon

Joined: 28 Dec, 2007
Posts: 72


Dream Kudos: 50
My Contributions
So, if I want to create additional columns to the table, I should use the method of creating a new table, creating the previous columns + the ones I want to add + all the data? Okay, thank you for the help. I will try everything tomorrow and if problems appear, I will write.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month