PDA

Click to See Complete Forum and Search --> : How to do this in SQL???


elpower
Oct 4th, 2000, 09:41 AM
Hi Anyone,

I have gotten this to work with an Access File.
does anyone know how to do this with SQL Server 7.0?

Let me know.
Thanks,
EL.

Public DbFile As Database
Public RstFile As Recordset
Public FileName As String
Public TblFile As TableDef
Public IdxFile As Index
Public FLD As Field
Public WrkDefault As Workspace
Public DbsNew As Database

Public Sub CreateDatabase()
FileName = App.Path & "\database.mdb"
If Dir(FileName) <> "" Then
'open database file
Set DbsNew = OpenDatabase(FileName)
Set RstFile = DbFile.OpenRecordset("Recordset")
Else
Set WrkDefault = DBEngine.Workspaces(0)
Set DbsNew = CreateDatabase(FileName, dbLangGeneral)
Set TblFile = DbFile.CreateTableDef("Recordset") ' create recordset
With TblFile
.Fields.Append .CreateField("FieldName", dbText, 10)
DbFile.TableDefs.Append TblFile
End With
Set IdxFile = TblFile.CreateIndex("IdxFile")
Set FLD = IdxFile.CreateField("Fieldname")
With IdxFile
.Primary = True
.Unique = True
.Required = True
End With
IdxFile.Fields.Append FLD
TblFile.Indexes.Append IdxFile
DbFile.Close
End If
End Sub

bar
Oct 5th, 2000, 02:44 AM
elpower,

I don't think that DAO is going to cut it to create a database on a SQL Server. I've never actually created a database throught code, but here goes.

Using an ADO command object, you can send a T-SQL statement to the server via ODBC or whichever provider you want to use. The following is from the SQL Server book online:

CREATE DATABASE database_name
[ ON [PRIMARY]
[ <filespec> [,...n] ]
[, <filegroup> [,...n] ]
]
[ LOG ON { <filespec> [,...n]} ]
[ FOR LOAD | FOR ATTACH ]

<filespec> ::=

( [ NAME = logical_file_name, ]
FILENAME = 'os_file_name'
[, SIZE = size]
[, MAXSIZE = { max_size | UNLIMITED } ]
[, FILEGROWTH = growth_increment] ) [,...n]

<filegroup> ::=

FILEGROUP filegroup_name <filespec> [,...n]

Exactly how this works, I'll be able to tell you later on, but I don't have enough time to play with it at the moment, hope this helps.