Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

In VB Use SQL to Copy a table

Posted on 2001-08-10
4
180 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 127
Best way to parse out a json string in VB6? 10 221
Sending a email via excel using vba 6 97
fso.FolderExists("\\server\HiddenFolder$") 4 78
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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