i know how to create table and rows and columns of .mdb file in visual basic but how to set a primary key on any field.How is it possible.
Printable View
i know how to create table and rows and columns of .mdb file in visual basic but how to set a primary key on any field.How is it possible.
First at all I don't use the primary key anymore. It is annoying, hehe.
Better is in my opinion to create in Access the Indexes that you need and then just activate them in VB. If you use ADO then is it:
recordset.index ="NameCity" (if you created an index with name and city)
If you make then recordset.MoveFirst then you are on the first record of this Indexsort.
With recordset.Seek is it possible to seek then a record. Specially if you have many records is the index good.
My experience is that with less then 10.000 records is no index needed. Sort and Find is fast enough.
Franky
Hello Sujaanpals,
Try this code:
Private Sub Command1_Click()
Dim Ws As Workspace
Dim Db As Database
If Dir("c:\q.mdb") <> "" Then Kill ("c:\q.mdb")
Set Ws = DBEngine.Workspaces(0)
Set Db = Ws.CreateDatabase("c:\q.mdb", dbLangGeneral)
SqlSel = "Create Table Customer ( Customer Text, Project TEXT CONSTRAINT MyFieldConstraint PRIMARY KEY"
SqlSel = SqlSel + ", Plant" + " text"
SqlSel = SqlSel + ");"
Db.Execute SqlSel
Db.Close
End Sub
Nice regards,
Michelle.
Be aware that the method from Michelle is very dangerous for existing data, because it recreate always a new EMPTY Table.
Franky