Advertisement

07.15.2008 at 05:02AM PDT, ID: 23565729
[x]
Attachment Details

TableDef DAO How to Create Index and Primary Key

Asked by cwaldick in Visual Basic Programming

Tags: TableDefs, CreateIndex, DAO, Jet, Access, VB

Here is just a sample of the code I usually run on my end users computers to update their database files.
The problem is I never created indexes or primary keys.
I do search on text as well as numeric field so would like to have some of the fields as indexes with allow duplicates set to true.

Here is my challenge. I have more than 1500 customers so I cannot manually update the database files and because they are no having a lot of data their programs are very slow. I manually did it for one of my customer and from 8 seconds to query a record it is now instant.

How can I update and EXISTING field to be an indexed field using DAO code in VB.
I need to do it with dbLong, dbDouble as well as some text fields.
Also all my existing AutoIncrement fields I would like to index with no duplicates allowed and make the primary keys.

Can I still do that to existing tables and fields ?
For example purposes I marked each field with a comment that I need indexed.
Unfortuntely I don't have any sample code of existing text fields but I do need some indexed so please show me how I would do that as well.

Thanks

Chris
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
'Created 14-Jul-08
 
'|About This Update....
'|Applicable Revision:
'|Min Revision Required: 20
'|
'|
'|
'|
'|
'_______________________________________________________________________
 
 
 
'COMPANY DATABASE FILE
 
   Set db = OpenDatabase(global_db)
 
'VALIDATE FIELDS IN TABLE  Departments
  Set tbl = db.TableDefs("Departments")
 
   'VERIFY FIELD: Table Departments Field CInvoice
     needitem = True
 
     For Each fld In tbl.Fields
        If UCase(fld.Name) = UCase("CInvoice") Then
            needitem = False
            Exit For
        End If
     Next fld
 
     If needitem = True Then
         Set fld = tbl.CreateField("CInvoice", dbLong)
         tbl.Fields.Append fld
         fld.DefaultValue = 0
 
         'UPDATE DATA
         Set rsu = db.OpenRecordset("SELECT * from Departments")
           While Not rsu.EOF = True
             rsu.Edit
             rsu!CInvoice = 0
             rsu.Update
            rsu.movenext
           Wend
         rsu.Close
     End If
 
 
   'VERIFY FIELD: Table Departments Field CWIP
     needitem = True
 
     For Each fld In tbl.Fields
        If UCase(fld.Name) = UCase("CWIP") Then
            needitem = False
            Exit For
        End If
     Next fld
 
     If needitem = True Then
         Set fld = tbl.CreateField("CWIP", dbLong)
         tbl.Fields.Append fld
         fld.DefaultValue = 0
 
         'UPDATE DATA
         Set rsu = db.OpenRecordset("SELECT * from Departments")
           While Not rsu.EOF = True
             rsu.Edit
             rsu!CWIP = 0
             rsu.Update
            rsu.movenext
           Wend
         rsu.Close
     End If
 
 
 
'SEE IF TABLE DepartmentsBlocked EXIST
   TableExists = False
 
   For Each tbl In db.TableDefs
       If UCase(tbl.Name) = UCase("DepartmentsBlocked") Then
          TableExists = True
          Exit For
       End If
   Next tbl
 
   If TableExists = False Then
     Set tbl = db.CreateTableDef("DepartmentsBlocked")
      Set fld = tbl.CreateField("ID", dbLong)'Would like it indexed Dups NOT OK and Primary must be set to true
 
      tbl.Fields.Append fld
      fld.Attributes = dbAutoIncrField
     db.TableDefs.Append tbl
   End If
 
Set tbl = db.TableDefs("DepartmentsBlocked")
 
 
 
 
   ' VERIFY FIELD: Table DepartmentsBlocked Field MasterDep
       needitem = True
 
       For Each fld In tbl.Fields
          If UCase(fld.Name) = UCase("MasterDep") Then
              needitem = False
              Exit For
          End If
       Next fld
 
       If needitem = True Then
         Set fld = tbl.CreateField("MasterDep", dbLong)'Would like it indexed Dups ok
 
         tbl.Fields.Append fld
         fld.DefaultValue = 0
 
         'UPDATE DATA
         Set rsu = db.OpenRecordset("SELECT * from DepartmentsBlocked")
           While Not rsu.EOF = True
             rsu.Edit
             rsu!MasterDep = 0
             rsu.Update
            rsu.movenext
           Wend
         rsu.Close
       End If
 
 
   ' VERIFY FIELD: Table DepartmentsBlocked Field NotAllowedDep
       needitem = True
 
       For Each fld In tbl.Fields
          If UCase(fld.Name) = UCase("NotAllowedDep") Then
              needitem = False
              Exit For
          End If
       Next fld
 
       If needitem = True Then
         Set fld = tbl.CreateField("NotAllowedDep", dbLong) 'Would like it indexed Dups ok
         tbl.Fields.Append fld
         fld.DefaultValue = 0
 
         'UPDATE DATA
         Set rsu = db.OpenRecordset("SELECT * from DepartmentsBlocked")
           While Not rsu.EOF = True
             rsu.Edit
             rsu!NotAllowedDep = 0
             rsu.Update
            rsu.movenext
           Wend
         rsu.Close
       End If
 
 
 
End Sub
[+][-]07.19.2008 at 10:31AM PDT, ID: 22043133

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Visual Basic Programming
Tags: TableDefs, CreateIndex, DAO, Jet, Access, VB
Sign Up Now!
Solution Provided By: aikimark
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906