How to create a new Ms Access file

  • Thread starter Thread starter Hai Ly Hoang
  • Start date Start date
H

Hai Ly Hoang

Hi,
In Visual C++,
My program want to store its result into new MSAccess
database file. So, I want to create new .mdb file on harddisk and then
connect to it by ODBC.
I've already know how to connect to .mdb file by ODBC, but i don't know how
to create a new Access file (.mdb file) on hard disk so that ODBC can
connect to.

My question is how to create a new .mdb file (programmatically)

Thanks
 
Hi,
In Visual C++,
My program want to store its result into new MSAccess
database file. So, I want to create new .mdb file on harddisk and then
connect to it by ODBC.
I've already know how to connect to .mdb file by ODBC, but i don't know how
to create a new Access file (.mdb file) on hard disk so that ODBC can
connect to.

My question is how to create a new .mdb file (programmatically)

Probably the easiest way is to distribute an empty (template) database
with your tables and stuff pre-defined. Then, when the user selects to
create a new database, copy the template file to the new name.
 
Adapted from Access 97 helpfiles and working in Access 2002. This is VBA
code which you'll need to translate. It uses the CreateDatabase method to
create a new, encrypted database named Newdb.mdb:

Sub CreateNewDB()
Dim wspDefault As DAO.Workspace
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld1 As DAO.Field
Dim fld2 As DAO.Field
Dim idx As DAO.Index
Dim fldIndex As DAO.Field

Set wspDefault = DBEngine.Workspaces(0)
' Create new, encrypted database.
Set dbs = wspDefault.CreateDatabase("Newdb.mdb", _
dbLangGeneral, dbEncrypt)
' Create new table with two fields.
Set tdf = dbs.CreateTableDef("Contacts")
Set fld1 = tdf.CreateField("ContactID", dbLong)
fld1.Attributes = fld1.Attributes + dbAutoIncrField

Set fld2 = tdf.CreateField("ContactName", dbText, 50)
' Append fields.
tdf.Fields.Append fld1
tdf.Fields.Append fld2
' Create primary key index.
Set idx = tdf.CreateIndex("PrimaryKey")
Set fldIndex = idx.CreateField("ContactID", dbLong)
' Append index fields.
idx.Fields.Append fldIndex
' Set Primary property.
idx.Primary = True
' Append index.
tdf.Indexes.Append idx
' Append TableDef object.
dbs.TableDefs.Append tdf
dbs.TableDefs.Refresh

Set dbs = Nothing
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
if i were you, i won't even bother to translate. Modify the code to VB
script and run it in a shell, done.

ben
 
Hai Ly Hoang

Since you are dealing exclusively with Access, specifically with the Jet
database, then you should consider using the Jet extensions for ADO - aka
ADOX. You can create an instance of the Catalog object, then append a
table - after you have created a table object and appended the columns.
Here is a sample piece of code that I quickly threw together. It adds one
table to a newly created database, and in that table there is one autonumber
integer field (which is then given the Primary key attribute), and three
varchar fields. I hope it helps.

regards
roy fine



//
// ADOX_Create.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#import "c:\program files\common files\system\ado\msadox.dll"


/* ***********************************************************************
*/
void MakeDatabase()
{
::CoInitialize(NULL);
ADOX::_CatalogPtr cat(__uuidof(ADOX::Catalog));

cat->Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\NewJet.mdb");

ADOX::_TablePtr tbl(__uuidof(ADOX::Table));
tbl->Name = _bstr_t("MyTable");

ADOX::_ColumnPtr col_CID(__uuidof(ADOX::Column));
col_CID->Name=_bstr_t(L"CID");
col_CID->put_Type(ADOX::adInteger);
col_CID->ParentCatalog = cat;
ADOX::PropertiesPtr props = col_CID->GetProperties();
props->GetItem(L"AutoIncrement")->PutValue(_variant_t(true));

ADOX::_ColumnPtr col_FNAME(__uuidof(ADOX::Column));;
col_FNAME->Name=_bstr_t(L"FNAME");
col_FNAME->ParentCatalog = cat;
col_FNAME->put_Type(ADOX::adVarWChar);
col_FNAME->put_DefinedSize(32);

ADOX::_ColumnPtr col_LNAME(__uuidof(ADOX::Column));;
col_LNAME->Name=_bstr_t(L"LNAME");
col_LNAME->put_Type(ADOX::adVarWChar);
col_LNAME->put_DefinedSize(32);

ADOX::_ColumnPtr col_ADDRESS(__uuidof(ADOX::Column));;
col_ADDRESS->Name=_bstr_t(L"ADDRESS");
col_ADDRESS->put_Type(ADOX::adVarWChar);
col_ADDRESS->put_DefinedSize(66);

tbl->GetColumns()->Append(_variant_t(col_CID,true),ADOX::adInteger,0);
tbl->GetColumns()->Append(_variant_t(col_FNAME,true),ADOX::adVarWChar,32);
tbl->GetColumns()->Append(_variant_t(col_LNAME,true),ADOX::adVarChar,32);
tbl->GetColumns()->Append(_variant_t(col_ADDRESS,true),ADOX::adVarChar,66);

ADOX::_KeyPtr prkey(__uuidof(ADOX::Key));
prkey->PutName(_bstr_t(L"CustOrder"));
prkey->PutType(ADOX::adKeyPrimary);
ADOX::ColumnsPtr cols = prkey->GetColumns();
cols->Append(L"CID",ADOX::adInteger,0);
tbl->GetKeys()->Append(L"PK",ADOX::adKeyPrimary,_bstr_t(L"CID"),L"",L"");

cat->GetTables()->Append(_variant_t(tbl,true));
}

/* ***********************************************************************
*/
int wmain(int argc, char *argv[])
{
try
{
::DeleteFile("c:\\NewJet.mdb");
MakeDatabase();
}
catch(_com_error &e)
{
_bstr_t errm(e.ErrorMessage());
_bstr_t errd(e.Description());
printf("\nError...\n%S\n\n",(wchar_t *)errd);
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top