Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / April 2005



Tip: Looking for answers? Try searching our database.

ADO Access and SQL SERVER

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AUDI - 27 Apr 2005 11:29 GMT
I used the following code to copy some records from one MDB file to another

strDst = "C:\Folder\accessfile.mdb"
Set cnnSrc = New ADODB.Connection
With cnnSrc
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=strDst;Mode=ReadWrite;Persist Security Info=False"
   .Open
   If .State = adStateOpen Then
     MsgBox "Source connection opened successfully"
   Else
     MsgBox "Cannot connect to Source data"
   End If
 End With
 strSQl = "INSERT INTO tbl_table1 (Field1, Field2) in '" & strDstData & _
   "' SELECT Field1, Field2  From tbl_table2 WHERE field1=0"
 cnnSrc.Execute (strSQL)

This was successfull when the source and destination databases are Access

What about SQl SERVER ?

strDst = "C:\Folder\accessfile.mdb"
Set cnnSrc = New ADODB.Connection
With cnnSrc
    .ConnectionString = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;" & _
           "Initial Catalog=DBNAME;Data Source=PCNAME"
    .Open
   If .State = adStateOpen Then
     MsgBox "Source connection opened successfully"
   Else
     MsgBox "Cannot connect to Source data"
   End If
 End With
 strSQl = "INSERT INTO tbl_table1 (Field1, Field2) in '" & strDstData & _
   "' SELECT Field1, Field2  From tbl_table2 WHERE field1=0"
 cnnSrc.Execute (strSQL)

This doesn't work ?

Is there any way to fix this
Andrea Montanari - 27 Apr 2005 12:01 GMT
hi,
> What about SQl SERVER ?
>
[quoted text clipped - 18 lines]
>
> Is there any way to fix this

strSQl = "INSERT INTO [owner].[tbl_table1] (Field1, Field2) " & _
   " SELECT Field1, Field2  From [" & strDstData &].[owner].[tbl_table2]
WHERE field1=0"
or
strSQl = "INSERT INTO [" & strDstData &"].[owner].[tbl_table1] (Field1,
Field2) " & _
   " SELECT Field1, Field2  From [owner].[tbl_table2] WHERE field1=0"

cnnSrc.Execute (strSQL)
depending on your actual database context...
your database user will need granted access to both databases..
Signature

Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtm        http://italy.mvps.org
DbaMgr2k ver 0.11.1  -  DbaMgr ver 0.57.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
--------- remove DMO to reply

AUDI - 28 Apr 2005 09:39 GMT
Thanks for your reply but I guess that you misunderstand me because  your
suggestions did not work. and I get the following error
" Error number is -2147217865 and Error description is Invalid object name
'C:\Pathname\datafile.mdb.tablename'.
Did you notice that I want to insert some records in a table in Access data
file and the source table in MS SQLServer.

> hi,
> > What about SQl SERVER ?
[quoted text clipped - 38 lines]
> interface)
> --------- remove DMO to reply
Andrea Montanari - 28 Apr 2005 10:57 GMT
hi AUDI,
> Thanks for your reply but I guess that you misunderstand me because
> your suggestions did not work. and I get the following error
> " Error number is -2147217865 and Error description is Invalid object
> name 'C:\Pathname\datafile.mdb.tablename'.
> Did you notice that I want to insert some records in a table in
> Access data file and the source table in MS SQLServer.

if you like to use a mixed scenario, with JET and SQL Server data source,
you can have a look at a linked server solution with an SQL Server
connections...

you can add a linked server pointing to the Access database like following,
where a pesudo copy of the original JET table is created an populated from
the original data

SET NOCOUNT ON
USE master
GO
-- adding linked server
EXEC sp_addlinkedserver
  @server = 'my_JET_db',
  @provider = 'Microsoft.Jet.OLEDB.4.0',
  @srvproduct = 'OLE DB Provider for Jet',
  @datasrc = 'D:\NWIND.MDB' -- point to Nortwind JET database
GO

-- use your database.. here just define a new table in tempdb
USE tempdb
GO
CREATE TABLE dbo.MSDE_customers (
CustomerID VARCHAR(60) PRIMARY KEY ,
CompanyName VARCHAR(60) ,
ContactName VARCHAR(60) ,
ContactTitle VARCHAR(60) ,
Address VARCHAR(60) ,
City VARCHAR(60) ,
Region VARCHAR(60) ,
PostalCode VARCHAR(60) ,
Country VARCHAR(60) ,
Phone VARCHAR(60) ,
Fax VARCHAR(60)
)
GO
PRINT 'SELECT from the linked server Jet database'
SELECT TOP 10 * FROM my_JET_db...Customers

PRINT 'Import rows to the MSDE database via INSERT INTO'
INSERT INTO dbo.MSDE_customers SELECT * FROM my_JET_db...Customers

PRINT ''
PRINT 'SELECT from the MSDE database'
SELECT TOP 10 * FROM dbo.MSDE_customers
GO
-- cleanup
DROP TABLE dbo.MSDE_customers
GO
USE master
go
EXEC sp_dropserver 'my_JET_db', 'droplogins'

further information and relative synopsis about sp_addlinkedserver system
stored procedure can be found at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_a
dda_8gqa.asp

Signature

Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtm        http://italy.mvps.org
DbaMgr2k ver 0.11.1  -  DbaMgr ver 0.57.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
--------- remove DMO to reply

AUDI - 28 Apr 2005 15:23 GMT
Hi Andrea,

I would like to greatly thank you and I tested what you suggested and it
works fine after adding remote login...
But again this is not my case. My case is as : I have a database on
sqlserver on PC1
I want users from other machines to connect and do every thing ( Insert ,
delete , update and so on...) What else I want is to print reports as
backend each user has his GUI interface (  VB Application ) and as I was
used to do the reporting task with access database and Crystal report for
now Ver 7...
So I want the effective way to insert records into the user access file
selected from the Central ( SQL SERVER DATABASE)
I hope I am very clear this time.

Any tips will be highly appreciated

> hi AUDI,
> > Thanks for your reply but I guess that you misunderstand me because
[quoted text clipped - 59 lines]
> further information and relative synopsis about sp_addlinkedserver system
> stored procedure can be found at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_
sp_adda_8gqa.asp
> --
> Andrea Montanari (Microsoft MVP - SQL Server)
[quoted text clipped - 3 lines]
> interface)
> --------- remove DMO to reply
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.