DataBase - ODBC Connection to SQL Server Compact

Asked By Spiro
31-Jan-09 10:59 AM
Dear All,

I wrote VBA code in Access 2003 for a project...I have for a PDA...and
therefore it is required to access SQL Server Compact 3.5

Following that I came up with a problem though which I cannot find an aswer
for. In debugging mode when the line pConn.Open reached it produces the
following error:

(We suspect that there is a problem with the OLEDB provider...)


Method "Open" of object '_Connection' failed.

Any ideas of what it may be...?

...(For the purpose of testing I used the following code)...
Sub SyncStockItems()

Dim pConn As ADODB.Connection
Dim pRs As ADODB.Recordset

Set pConn = New ADODB.Connection
Set pRs = New ADODB.Recordset

pConn.ConnectionString = "PROVIDER=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data
Source=C:\Apog\Apog.sdf"

-----'pConn.ConnectionString =
If pConn.State = 1 Then
pConn.Close
End If
pConn.Open

'Delete All Records from Mobile Database
pConn.Execute "DELETE * FROM StockItems"
'pRs.Open

pRs.Close
pConn.Close

Set pRs = Nothing
Set pConn = Nothing

In terms of the project I will first explain that I started by trying to
a task that is crucial for the users of the project. So I wrote the code
supplied in Access 2003 (VBA editor) and from there I got the error message
in question. The next stage would be to write Visual Basic code in Visual
Studio 2005 that it would run on the PDA (using the data copied from Access
on the SQL Server Compact database) for collecting data and then transfer the
data to Microsoft Access for further manipulation...


Thank you in advance...

Spiros..
SQL Server Compact 3.5
(1)
ADODB.Connection
(1)
SQL Server 2005
(1)
SQL Server 2008
(1)
SQL Server
(1)
ADODB.Recordset
(1)
ADODB.Error
(1)
Virtual PC
(1)
  Sylvain Lafontaine replied...
31-Jan-09 06:13 PM
Your code looks fine, it's possible that you have a permission problem - for
example, a password is required - or that the database is already opened in
exclusive mode by another problem, for example either SSMS or VS.NET if you
are using one of these programs to create the SDF database file.

First, make sure that you have setup a reference to the Microsoft ActiveX
Data Objects 2.1 Library or later in the References dialog window of VBA.

Second, take a look at the errors collection returned by the connection
object; for example:

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset

conn.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5" _
& "Data Source=C:\Apog\Apog.sdf;"

On Error Resume Next

conn.Errors.Clear
conn.Open

If (conn.Errors.Count > 0) Then
Dim e As ADODB.Error

For Each e In conn.Errors
Debug.Print e.Description
Next

Exit Sub
End If

On Error Resume Next

If (conn.State = 1) Then
rs.Open "Select * from Table1", conn

While Not rs.EOF
Debug.Print rs(0) & ": " & rs(1)
rs.MoveNext
Wend

End If

If (conn.State = 1) Then
MsgBox "On ferme ..."
conn.Close
End If

Set rs = Nothing
Set conn = Nothing

The provider "SQLSERVER.MOBILE.OLEDB.3.0;" is for the older version of
SQL-Compact Edition that comes with SQL-Server 2005 (?) while
2008).  For more information on the parameters for the connection string,
see:

http://www.connectionstrings.com/sql-server-2005-ce

Finally, you should clear up something: OLEDB providers are not ODBC
providers.  OLEDB Providers are used primarily with ADO and ODBC with DAO
but it's also possible to use an ODBC providers directly with ADO if you use
the special provider MSDASQL as a midlayer.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)
  Spiro replied...
02-Feb-09 02:40 AM
Dear Sylvain,
Thanks for your time to deal with my problem and your suggestions. As far as
the password is concerned I am sure that is no need for that since when I am
opening the sdf file from SQL Management Studio...no username or password is
required to connect to database. I also make sure, when I am running the
Access code, that no other programs concerning SQL files is running. On the
other hand I am not sure if any programs are running as "services" that could
create sharing violation. I would like your suggestions on that if you
please...I will get back to you also when I try the rest of your suggestions.
Thank you in advance.
Spiros Kostopoulos.
  Sylvain Lafontaine replied...
02-Feb-09 01:51 PM
Did you try to take a look at the messages in the Errors collection returned
by the Connection object as sugested?

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)
  Sylvain Lafontaine replied...
05-Feb-09 04:04 PM
First, make sure that the RS is not empty.  Second, print the name of each
field of the RS recordset (and their other properties as well, too) by using
their numerical index (Rs(0), Rs(1), ...).  With that, probably that you
should be able to see what's wrong.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain aei ca (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)
  Spiro replied...
16-Feb-09 07:50 AM
Hi Sylvain,
I have tried all the possibilities...but with no luck so far..
1)First the "conn" object opens giving no errors in the error collection..
2)Secondly the RS recordset opens nicely but when I try to access the
objects (RS(0), RS(1)...) I get an error message "Run time error '3265'. Item
not found in the collection...
Finally I thought to use the Conn.Execute "SqlStr" statement where SqlStr is
VALUES (1, '14158-001', '8431319393071', 'Antik Negro/Tee Marron')". I tried
as query in SQL Management Studio and it worked fine and added a new record
in the table "StockItems" of the Apog.sdf....BUT it does not work from
Access...
I think this piece of code is making fun of me...
Any ideas?
Thank you for your time again.
Kind Regards,
Spiros.
  Sylvain Lafontaine replied...
16-Feb-09 01:24 PM
Possibly because you are trying to use two open recordset on the same
connection object. A connection object must be used with a single recordset
at a time and any previous recordset that has used this connection must have
been closed before opening a new one.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain aei ca (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)
  Spiro replied...
17-Feb-09 09:08 AM
Thanks again...I tried both options:
1) Use only 'execute' commands from con objet with no Rs (recordset) used.
2) Use only Rs recordset without using any commands from con object...
Both of them failded to add a new record on the remote database...What is
going wrong?

Regards,
Spiros.
  Spiro replied...
17-Feb-09 09:09 AM
"Sylvain Lafontaine" wrote:
  Sylvain Lafontaine replied...
21-Feb-09 04:05 AM
Sorry for the delay but I have to reinstall my machine these last days.

I have no idea what's going wrong.  It's your problem only with inserting
new records from ADO or if you can't read any record from the database as
well?

I will try some test this weekend but as I don't have the save version as
you (I have the 3.5), I'm not sure if I will be able to reproduce your
problem.  Last time I checked, I don't remember having any problem using
this OLEDB driver from VBA.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain aei ca (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)
  Spiro replied...
21-Feb-09 07:58 AM
Both of them (i.e. no adding records and no viewing the records neither)...
Thanks again.
Spiros.
Create New Account
help
Might be outgrowing Access but daunted by SQL Server DataBase I am close to completing the consolidation of various small Access databases and a spent the last 10 hours downloading, installing and trying to get my head around Microsoft SQL Server 2008 Express and how it might help me out. It all started when I came across encouraging article on keeping your Access database as your front end application and linking to SQL Server database tables. I am finding it MUCH more complicated than I had hoped. Can anyone be happy to use my quite powerful workstation at the office as some kind of server. You may have guessed by now that I am no IT guru by any stretch
SQL Server DataBase Hall?schen, ich habe mit einem SQL Server noch nie was am Hut gehabt. Alles was ich bisher gemacht hatte, da war Access immer ausreichend. Nun aber ist eine Anforderung, das ein Backend auf einem SQL-Server laufen soll, wobei dar?ber noch nicht fertig diskutiert ist. Ich habe da noch eine alte Vollversion von VB6, dort ist ein SQL-Server dabei (fracht mich aber nicht welche Version, der Karton mit der VB6 steht noch in
proof DataBase https: / / mcp.microsoft.com / authenticate / validatemcp.aspx transcript: 944364 AccessCode: helloworld Access Discussions SQL Server 2008 R2 (1) SQL Server 2008 (1) SQL Server 2005 (1) Microsoft SQL Server Reporting Services (1) Regards Larry Linson Microsoft Office Access (1
Cannot link to SQL server DataBase Can any of you guys help? I am new to using SQL Server. I have a database that I wish to upsize from Access to put the back end on a server, and use Access as a front-end. However, I am stuck at the very first stage - I cannot link Access to the SQL server. I am using SQL Server Management Studio on a single computer for development purposes, no network involved yet. In
Using Mysql or SQL server as backend to Access front end DataBase Have a client having problems with a fairly end. Using access 2003 / mdb format with Access 2007 software. We are surmising that recent server problems or connectivity problems may be to blame. If we converted to MySQL or SQL server as the back end, would that eliminate the potential for Database corruption that can occur drivers? I also read that Jet will process query criteria that it can on the SQL server before the results are sent back. correct? does this also happen when you link to MySQL? A while back I got the book "Microsoft Access Developer's Guide to SQL Server" - SAMs - Chipman and Baron. As I recall, switching to SQL server is not for