Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Table using DSN less connection (part2)

Status
Not open for further replies.

jodjim

Programmer
Nov 5, 2004
69
0
0
CA
I've experimented on several options to copy a table from an external database into my local Access table. After so many attempts and internet searches, my efforts were unsuccessful. So far the script below looks promising as it's only giving me "Invalid Argument" error. Please help as I'm quite desperate already. Thanks a lot.

strsql = "SELECT * INTO MyLocalTable FROM [Provider=SQLOLEDB.1;User ID=MyID;password=MyPass;Initial Catalog=MyDatabase;Data Source = MyDataSource;].MyDbTables;"

With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = strsql
.CommandType = adCmdText
.Execute 'ERROR!!!! Invalid Argument
End With
 
Should the 'MyLocalTableName', 'MyID', 'MyPass', 'MyDatabase' and 'MyDataSources' be strings or are they variables? If they are variables then wouldn't you want something like:
strsql = "SELECT * INTO " & MyLocalTable & " FROM [Provider=SQLOLEDB.1;User ID=" & MyID & ";password=" & MyPass & ";Initial Catalog=" & MyDatabase & ";Data Source =" & MyDataSource & ";].MyDbTables;"
 
Actually they are string and I understand they should be entered without the single quote. The data source is entered as Data Source =MySource.OurCompany.com

Any thoughts? Thanks.
 
Got an idea from another forum, applied to my project, it worked. Here it is:

strsql = "SELECT * INTO " & MyLocalTable & " FROM [ODBC;Driver={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;UID=MyID;PWD=MyPwd].MyTable;"

strTableName = "tblSourceTable"
If TableExists(CurrentDb, strTableName) Then
DoCmd.DeleteObject acTable, strTableName
End If

With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = strSQL
.CommandType = adCmdText
.Execute
End With

Private Function TableExists(db As Database, strTableName As String) As Boolean
Dim td As DAO.TableDef
On Error Resume Next
Set td = db.TableDefs(strTableName)
TableExists = Err.Number = 0
End Function

====
Another way is to use pass-through query. Create pass-through query and in Properties, type:
ODBC;Driver={SQL Server};SERVER=MyServer;DATABASE=MyDatabase;UID=MyID;PWD=MyPwd


Hope this will be of help to others
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top