Creating Access File in VB6

I would like to know how to create:
1 - Access File (.mdb)
2 - Add a Table to it (say MyTable)
3 - Add 4 Fields to "MyTable" and call them F1, F2, F3, and F4

Thank you in advance
Who is Participating?
MarineConnect With a Mentor Commented:
In my previouse comment i included that the references must be included. DAO3.6. Make sure you have it check there. Full Name of that library is
Microsoft DAO3.6 Object Library.
Please read the previouse comment on changing where i have inx where it hsould be field.
Here is a complete DAO solution for your task. Read this article
Here is ADO solution to create Access database from VB. It uses ADOX tools to do so. Here is a link
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

khaledcAuthor Commented:
I am afraid that is not enough, also I need a posted answer specified to my question.
I don't understand how you couldn't implement that sample here is the code. I tested it , it ran . with a database created.

Option Explicit

Private Sub Form_Load()
Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"
      Dim db As Database
      Dim tdEmployee As New TableDef
      Dim F1 As New Field
      Dim F2 As New Field
      Dim F3 As New Field
      Dim F4 As New Field
      Dim indxEmployee As New Index
      Dim indxStore As New Index

      Set db = CreateDatabase("Data.MDB", DB_LANG_GENERAL)
      tdEmployee.Name = "MyTable"
      'Define your fields
      'You didn't tell what the field type was so i made some integers and some text fields
      F1.Name = "F1"
      F1.Type = 4       'Long integer

      F2.Name = "F2"
      F2.Type = 10      'Text (32)
      F2.Size = 32

      F3.Name = "F3"
      F3.Type = 10           'Text (32)
      F3.Size = 32
      F4.Name = "F4"
      F4.Type = 10            'Text (256)
      F4.Size = 255
       ' THis adds this field to the fields collection.
      tdEmployee.Fields.Append F1
      indxEmployee.Fields.Append F2
      tdEmployee.Fields.Append F3
      tdEmployee.Fields.Append F4
      'Now to make an index omit this if you don't need one.
      indxEmployee.Name = "ind_my"
      indxEmployee.Fields = "F1" ' this sets an index on field F1
      indxEmployee.Unique = True
      indxEmployee.Primary = True 'this makes a primary key on the F1 field
      'Now to append the inex to the table tdEmployee
      tdEmployee.Indexes.Append indxEmployee
      'And finally append the table to the tables collection.
      db.TableDefs.Append tdEmployee
      'Now you have a table named tdEmployee with the 4 field F1,F2,F3 & F4.
End Sub

I forgot to mention please set reference to DAO library 3.6 FROM Project/References/And find there Ms Dao3.6
Ok small mismatch i found there where it says
indxEmployee.Fields.Append F2
it should be tdEmployee.Fields.Append F2
Also this database will be created in Programs Folder. If you want to specify another path do this.

Set db = CreateDatabase("C:\Data.MDB", DB_LANG_GENERAL)
Put whatever path you want in between thse quotes. The datbase then will be created there. Good luck with it now. It worked i tested 2 times.
khaledcAuthor Commented:
I could not run the application simply because the VB compiler returning the following error while highlighting the line "Dim db as Database":
"User-Defined Type not defined"

So I assume that some references or components need to be included in my project , which one?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.