Solved

In VB Use SQL to Copy a table

Posted on 2001-08-10
4
177 Views
Last Modified: 2010-05-02
Is there a way (I KNOW there must be!) to use an SQL statement from VB, to copy (Duplicate) a table and its data and data type settings?

I have tried to duplicate the table in code but I am unable to set the properties of each field, so duplicating it like this would be best.

Thanks
0
Comment
Question by:kazraver
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
idorey earned 300 total points
ID: 6371624
Hey Kaz! How are you? Long time (Bla bla)

Try this:

Dim MyConn As ADODB.Connection
Dim MyTable As ADODB.Recordset
Set MyConn = New ADODB.Connection
MyConn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\<DB NAME>" + ";"
MyConn.Open
Set MyTable = New ADODB.Recordset
MyTable.Open "SELECT <TABLE NAME>.*,* INTO <NEW TABLENAME> FROM <TABLE NAME>", MyConn, adOpenDynamic, adLockOptimistic
If MyTable.State = adStateOpen Then MyTable.Close

If this works mail me. I just moved and I need to give you the address.
0
 

Author Comment

by:kazraver
ID: 6371630
Whoa! Thats was quick!

I just realized that I posted the blasted Q twice!! Oh well off to Support!

I will test this later and drop you a mail.
0
 

Author Comment

by:kazraver
ID: 6371650
Hey Cool! Thats worked okay. I got a bit confused there for a moment but it works just fine.

What you doing sitting on the VB Question section?

I will mail you later (I am at work right now!)
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6371658
idorey is correct though it is a slight waste of resources using a recordset object to do this. You can do the same thing using the execute method of the connection object:

Using the code supplied above:

Dim MyConn As ADODB.Connection
Set MyConn = New ADODB.Connection
MyConn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\<DB NAME>"
+ ";"
MyConn.Open
MyConn.Execute "SELECT <TABLE NAME>.*,* INTO <NEW TABLENAME> FROM <TABLE NAME>"
MyConn.Close
Set MyConn = Nothing
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now