Re: ODBC Connection to SQL Server Compact
- From: "Sylvain Lafontaine" <sylvainlafontaine2009@xxxxxxxx>
- Date: Thu, 5 Feb 2009 16:04:28 -0500
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)
"Spiros" <Spiros@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C9218E72-2B2C-437C-BE3D-BA4B141CB0F0@xxxxxxxxxxxxxxxx
Hi,
Thanks for the code again I did try it, and I found out the following...
1)By using the connection string "Conn.ConnectionString =
"PROVIDER=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=C:\\Apog\Apog.sdf"
I got Error Message: "The database file has been created by an earlier
version of SQL Server Compact. Please upgrade using SqlCeEngine.Upgrade()
method. [,,,,,]"
2)By using the connection string
"Conn.ConnectionString =
"PROVIDER=Microsoft.SQLSERVER.MOBILE.OLEDB.3.0;Data
Source=C:\Apog\Apog.sdf"
The conn object opened "nicely". But Rs recordset failed to recognise the
fields name (of the table) on the Apog database.
On Debugging Mode the Expression Rs![ItemCode]= LclRs("ItemCode") the
field
"ItemCode" of Rs recordset was not updated and putting the cursor the msg
"Item not found in the collection..." Following that I checked the Field
name
of the "StockItems" table in the Apog.sdf (using SQL Management Studio)
but
I did not find any mistakes (mispellening etc..)
Any clues? (The code follows...)
Thank you in advance..
Kind Regards...
Spiros.
Sub SyncStockItems()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim LclRs As ADODB.Recordset
Dim SqlStr, MsgStr As String
Dim RecsNo As Integer, LclRecsNo As Integer
On Error Resume Next
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
Set LclRs = New ADODB.Recordset
LclRs.Open "StockItems", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
LclRecsNo = LclRs.RecordCount
'Microsoft.SQLSERVER.CE.OLEDB.3.5
'Conn.ConnectionString = "PROVIDER=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data
Source=C:\\Apog\Apog.sdf"
Conn.ConnectionString =
"PROVIDER=Microsoft.SQLSERVER.MOBILE.OLEDB.3.0;Data
Source=C:\Apog\Apog.sdf"
Conn.Errors.Clear
Conn.Open
'SqlStr = "SELECT StockItems.* FROM StockItems;"
'LclRs.Open SqlStr, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
If (Conn.Errors.Count > 0) Then
Dim e As ADODB.Error
For Each e In Conn.Errors
Debug.Print e.Description
Next
End If
On Error Resume Next
If (Conn.State = 1) Then
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
Conn.Open
'Delete All Records from Mobile Database
RetVal = SysCmd(acSysCmdSetStatus, "Deleting Records from Mobile
Database....")
Conn.Execute "DELETE * FROM StockItems"
'Conn.
RetVal = SysCmd(acSysCmdSetStatus, "Records Deleted from Mobile
Database.")
Rs.Open "StockItems", Conn, adOpenForwardOnly, adLockOptimistic
'Delete All Records from Mobile Database
'Rs.Open "Delete * from StockItems", Conn
'Start Copying from Local Database to Mobile Database
LclRs.MoveFirst
RetVal = SysCmd(acSysCmdSetStatus, "Commencing Copy of Stock Items Records
to Mobile Database.")
I = 1
While I <= LclRecsNo
Rs.AddNew
Rs![ItemCode] = LclRs("ItemCode")
Rs![OnBoxCode] = LclRs("OnBoxCode")
Rs![FactoryCode] = LclRs("FactoryCode")
Rs![Description] = LclRs("Description")
Rs.Update
RecsNo = Rs.RecordCount
'Debug.Print Rs(0) & ": " & LclRs(0)
'Debug.Print Rs(1)
MsgStr = "Record " & I & " of " & LclRecsNo & "."
RetVal = SysCmd(acSysCmdSetStatus, MsgStr)
I = I + 1
LclRs.MoveNext
Wend
LclRs.Close
Rs.Close
Conn.Close
Set Rs = Nothing
Set Conn = Nothing
Set LclRs = Nothing
End Sub
"Sylvain Lafontaine" wrote:
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)
"Spiros" <Spiros@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BEEB5F86-C20B-45B2-8688-D92BCB768D91@xxxxxxxxxxxxxxxx
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" wrote:
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
"Microsoft.SQLSERVER.CE.OLEDB.3.5;" is for the latest edition
(SQL-Server
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)
"Spiros" <Spiros@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:524F5EE0-74E3-4AF0-BCD9-40CB0961E3B5@xxxxxxxxxxxxxxxx
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...)
"Run-time error -2147467259 (800004005)
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 =
"PROVIDER=Microsoft.SQLSERVER.MOBILE.OLEDB.3.0;Data
Source=C:\Apog\Apog.sdf"
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
"connect" the Access database with the SQL Server Compact Edition
since
it
is
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..
.
- Follow-Ups:
- Re: ODBC Connection to SQL Server Compact
- From: Spiros
- Re: ODBC Connection to SQL Server Compact
- References:
- Re: ODBC Connection to SQL Server Compact
- From: Sylvain Lafontaine
- Re: ODBC Connection to SQL Server Compact
- From: Spiros
- Re: ODBC Connection to SQL Server Compact
- From: Sylvain Lafontaine
- Re: ODBC Connection to SQL Server Compact
- From: Spiros
- Re: ODBC Connection to SQL Server Compact
- Prev by Date: Re: rename a form in an external database
- Next by Date: Re: Text Messages
- Previous by thread: Re: ODBC Connection to SQL Server Compact
- Next by thread: Re: ODBC Connection to SQL Server Compact
- Index(es):
Relevant Pages
|