Hi all,
my objective is to open a csv file and save it to new mdb file at "c:\my_file" directory.
how to create new mdb file in vb6 code? the file name of the mdb will be base on csv file name.
many thanks!
Printable View
Hi all,
my objective is to open a csv file and save it to new mdb file at "c:\my_file" directory.
how to create new mdb file in vb6 code? the file name of the mdb will be base on csv file name.
many thanks!
A new database for each new file? Sounds very unreasonable.
Instead, if csv files will always have the same layout then you can design a table and load it with new data.
For each new load you will need to assign so unique id or at least date it was loaded.
Whether you design one table or multple (they will have to relate somehow) is up to you.
Why did you start this thread when you marked your other similar thread as resolved?
Here is a very basic way to create an MDB file... First please set a reference to Microsoft DAO 3.6 Object Library
Remember this is a just a basic example of how to create an MDB. Please amend it as per your requirements...
vb Code:
'~~> Set reference to Microsoft DAO 3.6 Object Library Private Sub Command1_Click() '~~> Replace this with your path and filename CreateDB "c:\my_file\Mydatabase.mdb" End Sub Sub CreateDB(mdbPath As String) Dim tdefMDB As TableDef, txtFieldone As Field, txtFieldtwo As Field Dim dateFieldone As Field, memoFieldone As Field, dbDatabase As Database Dim sNewDBPathAndName As String sNewDBPathAndName = mdbPath Set dbDatabase = CreateDatabase(sNewDBPathAndName, dbLangGeneral, dbEncrypt) '~~> Create new TableDef (I am creating a table Table1) Set tdefMDB = dbDatabase.CreateTableDef("Table1") '~~> Add fields to MDB '~~> for eample I am creating two text fields, 1 date field and '~~> 1 memo field. Amend as applicable Set txtFieldone = tdefMDB.CreateField("txtField1", dbText, 20) Set txtFieldtwo = tdefMDB.CreateField("txtField2", dbText, 20) Set dateFieldone = tdefMDB.CreateField("dateField1", dbDate) Set memoFieldone = tdefMDB.CreateField("memoField1", dbMemo) '~~> Append the field objects to the TableDef tdefMDB.Fields.Append txtFieldone tdefMDB.Fields.Append txtFieldtwo tdefMDB.Fields.Append dateFieldone tdefMDB.Fields.Append memoFieldone '~~> Save TableDef definition by appending it to TableDefs collection. dbDatabase.TableDefs.Append tdefMDB '~~> Inform user. MsgBox "New .MDB Created - '" & sNewDBPathAndName & "'", vbInformation End Sub
Useful Code :)
- do you have a list of available options to hand? such as settng primary key? and indexed etc
@Wayne:
vb Code:
Dim ind As DAO.Index '~~> rest of the code as mentioned above .... Set ind = tdefMDB.CreateIndex("PrimaryKey")
As DAO is seriously outdated it is better (and actually recommended) option to use ADOX library.
There are plenty of samples throughout the this forum and also DB Forum.
I absolutely agree with Rhino that DAO is outdated and I myself use the ADOX but this is the only scenario where I use DAO :) probably because I remember this code like the back of my hand and it makes me create the MDB in no time.... Guess I am too lazy now a days...
i just found it useful yesterday because i cant find my office CD and needed to create an MDB [recent format means no office :(] - but its so simple i created the DB FASTER then in access, and didnt have to backup the mdb incase my code screwed it... good tool for devel :P - at least i thought so :) - cheers
Yeah I use it very often... like I mentioned, I am too lazy :)
Koolsid,
Thank you!:thumb: