Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Accessing a SQL Server CE database from VBA is not a problem:

VB
Set objConn = New ADODB.Connection
objConn.ConnectionString = _
    "PROVIDER=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=D:\testDB.sdf"
objConn.Open


There is an example of creating a new SQL CE database using C# here.[^]

Is it possible to create a new SQL Server CE database from VBA?
Posted
Updated 25-Apr-17 7:51am

If you want to create a database from VBA then you would need to do that in a stored procedure and implement it in VBA through ADO.

SQL
create procedure _procedureName
as
begin
create table _newTable
(
 [<field_names> <datatypes>]
)
end
</datatypes></field_names>
 
Share this answer
 
v2
Comments
Uros Calakovic 30-Aug-10 9:39am    
Thank you for the comment. I am aware of the possibility to use SQL DDL commands to create a database from VBA and I would do that if this was a 'regular' SQL Server. But, in SQL Server CE database is file and file is database (I figure), and this is I am having problems with: how to connect to SQL Server CE without an existing .sdf file? If this was possible, I could then go on and try to use SQL to create one.
To answer the question, Dennis Wallentin has provided VBA code to create a Sql Server CE database here[^]. Copy of the code for future reference:

VB
Option Explicit

'Reference to Microsoft ActiveX Data Objects x.x Library.
'Reference to Microsoft ADO Ext.x.x for DDL and Security

Sub Create_SSCE_Database()

'Name of the SSCE database.
Const C_stDBName = "XLDennis.sdf"

'Path to the SSCE database.
Const C_stPath = "C:\Users\Dennis Wallentin\Documents\SSCE\"

'Objects to create database, table and fields.
Dim xCat As ADOX.Catalog
Dim xTable As ADOX.Table
Dim xCol As ADOX.Column

Dim stConnection As String

'Connection string.
stConnection = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;" & _
        "Data Source=" & C_stPath & C_stDBName & ";" & _
        "Persist Security Info=False;"

Set xCat = New ADOX.Catalog
Set xTable = New ADOX.Table

'In case we have an already created database.
On Error Resume Next
Kill C_stPath & C_stDBName
On Error GoTo 0

'Create the empty database.
xCat.Create stConnection

'All the properties of the connection string is printed to
'the Immediate Window.
Debug.Print xCat.ActiveConnection

'Create a table with some fields.
With xTable
  .Name = "ReportedData"
  .Columns.Append "Department", adVarWChar, 10
  .Columns.Append "Quater", adVarWChar, 6
  .Columns.Append "Budget", adSmallInt, 4
  .Columns.Append "Result", adSmallInt, 4
End With

'Add the created table to the database.
xCat.Tables.Append xTable

'Cleaning up.
Set xCol = Nothing: Set xTable = Nothing: Set xCat = Nothing

End Sub
 
Share this answer
 
Comments
Dalek Dave 9-Sep-10 14:29pm    
Good answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900