I'm using DAO 3.6, Access 9.0, Windows 2000 with SP4, and CVI 7.0 with 7.1 RTE
I found a piece of Visual Basic code on Microsoft's support page that I typed into the Access editor. When I run this, it does exactly what I want on an existing table:
// ***********************************************************************************
'---------------------------------------------------------------
'PURPOSE: Adds a field index to a table.
'ACCEPTS: Nothing.
'RETURNS: Nothing.
'---------------------------------------------------------------
Function AddIndex()
Dim dbs As DAO.Database, tdf As DAO.TableDef
Dim idx As DAO.Index, fld As DAO.Field
Set dbs = CurrentDb()
' Open the table definition.
Set tdf = dbs.TableDefs("test1")
' Create an index called PrimaryKey for this TableDef
' and turn on the Primary and Required properties.
Set idx = tdf.CreateIndex("PrimaryKey")
With idx
.Name = "PrimaryKey"
.Primary = True
.Required = True
.IgnoreNulls = False
End With
' Create an index field with the same name as a table field,
' then append it to the index.
Set fld = idx.CreateField("field1")
idx.Fields.Append fld
' Append the index to the TableDef.
tdf.Indexes.Append idx
End Function
//*************************************************************************************
The following is my implementation of this in CVI:
// ************************************************************************************
#include
#include "DAO3_6.h"
#include "toolbox.h"
#include
#include "DAO_Functions.h" // my own implementation of DAO functions
char test [MAX_PATHNAME_LEN];
static DAO36Obj_Database Obj_DB;
static DAO36Obj_TableDef Obj_TableDef;
static DAO36Obj_TableDefs Obj_TableDefs;
static DAO36Obj_Field Obj_Field;
static DAO36Obj_Fields Obj_Fields;
static DAO36Obj_Index Obj_Index;
static DAO36Obj_Indexes Obj_Indexes;
static HRESULT Err;
static VARIANT field;
static VARIANT value;
char text[101];
void main ()
{
// open data base
Fmt (test, "%s", "C:\\test.mdb");
if (OpenDataBase (test, &Obj_DB)) // this is my own function
{
// get table to add index to
Err = DAO36_DatabaseGetTableDefs (Obj_DB, NULL, &Obj_TableDefs);
CA_VariantSetCString (&value, "test1");
Err = DAO36_TableDefsGetItem (Obj_TableDefs, NULL, value, &Obj_TableDef);
// create the index as a primary key
CA_VariantSetCString (&field, "PrimaryKey");
Err = DAO36_TableDefCreateIndex (Obj_TableDef, NULL, field, &Obj_Index);
Err = DAO36_IndexSetPrimary (Obj_Index, NULL, VTRUE);
Err = DAO36_IndexSetRequired (Obj_Index, NULL, VTRUE);
Err = DAO36_IndexSetIgnoreNulls (Obj_Index, NULL, VTRUE);
// add field to this index - "Field1" already exists in the data base
CA_VariantSetCString (&field, "field1");
CA_VariantSetInt (&value, DAO36Const_dbInteger);
Err = DAO36_IndexCreateField (Obj_Index, NULL, field, CA_DEFAULT_VAL, value, &Obj_Field);
// append to index.fields
Err = DAO36_IndexGetFields (Obj_Index, NULL, &value);
Err = CA_VariantGetObjHandle (&value, &Obj_Fields);
Err = DAO36_FieldsAppend (Obj_Fields, NULL, Obj_Field);
// the failure occurs with the Append Method. Error is "No such Interface Supported"
CA_GetAutomationErrorString (Err, text, 101);
Err = DAO36_FieldsRefresh (Obj_Fields, NULL);
// append index to indexes
Err = DAO36_TableDefGetIndexes (Obj_TableDef, NULL, &Obj_Indexes);
Err = DAO36_IndexesAppend (Obj_Indexes, NULL, Obj_Index);
Err = DAO36_IndexesRefresh (Obj_Indexes, NULL);
// close data base
CloseDataBase (&Obj_DB); // this is my own function
}
CA_DiscardObjHandle (Obj_DB);
Obj_DB = 0;
CA_DiscardObjHandle (Obj_TableDef);
Obj_TableDef = 0;
CA_DiscardObjHandle (Obj_TableDefs);
Obj_TableDefs = 0;
CA_DiscardObjHandle (Obj_Field);
Obj_Field = 0;
CA_DiscardObjHandle (Obj_Fields);
Obj_Fields = 0;
CA_DiscardObjHandle (Obj_Index);
Obj_Index = 0;
CA_DiscardObjHandle (Obj_Indexes);
Obj_Indexes = 0;
}
// ***********************************************************************************
I have checked Microsoft's support site for known bugs related to this error, and found a lot of info related to DLL's that were not properly registered, but nothign that helped this problem. Besides, if it was a DLL problems then I suspect that the VB version would not have worked either?
If you have any ideas on this problem, I would definately appreciate it.
Thank you,
Chris Poore