LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating the index field using DAO and LabWindows/CVI

I'm attempting to create tables using DAO under CVI. I don't have any problems creating a table and adding the fields, but I want to designate one of those fields as the "PrimaryKey" Thus far, I have only found Visual Basic examples and procedures for doing this, but when I try to implement the code, I always get an error when I attempt to append the Index. The table is created, but not indexed.

Any examples or suggestions would be appreciated.
0 Kudos
Message 1 of 3
(2,911 Views)
It may depend on how you are implementing the "primary key". Could you attach the code you are using to set the "primary key". Also, what is the error code you are receiving when you set the primary key of your table?

Tyler T.
0 Kudos
Message 2 of 3
(2,894 Views)
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
0 Kudos
Message 3 of 3
(2,892 Views)