Click here to Skip to main content
Click here to Skip to main content

The alxBase classes for work with DBF files

By , 5 Nov 2002
 

Introduction

This classes is useful for work with dbf files. The current version supports formats: FoxPro, Visual FoxPro, dBASEIII - dBASE 5.x, Clipper and HiPer-SIx. The memo fields also are supported. CDBFRecordset provides all the useful functions for navigation and modification in table. CDBFCursorset it is possible to use for work only with the selected records. CDBFTableDef it is used for creation and updating of structure of the table.

Create/Update table structure

For creation/updating of structure of the table are available functions:

    <li><code>CreateTable
  • ModifyTable
  • AddField
  • InsertField
  • ModifyField
  • MoveField
  • DeleteField
  • UpdateTable

For cancels any pending updates you may call function CanselUpdateTable().

try
{
	CDBFTableDef mNewTable;
	// begin create
	mNewTable.CreateTable(szFileName);

	// add field
	FIELD_REC FieldRec;
	memset(&FieldRec, 0, sizeof(FIELD_REC));
	memcpy(FieldRec.field_name, szFieldName, 10);
	FieldRec.field_type = FLD_TYPE_CHARACTER;
	FieldRec.len_info.char_len = 20;
	mNewTable.AddField(&FieldRec);

	// ...

	// end create
	mNewTable.UpdateTable();
}
catch(CDBFException* e)
{
	e->ReportError();
	e->Delete();
}

Navigation

For navigation are supported functions:

    <li><code>Move
  • MoveNext
  • MovePrev
  • MoveFirst
  • MoveLast
  • SetAbsolutePosition

You also may use functions: IsEOF(), IsBOF(), GetRecordCount(), GetAbsolutePosition().

try
{
	CDBFRecordset m_Set;
	m_Set.Open("test.dbf");
	while(!m_Set.IsEOF())
	{
		// ...
		m_Set.MoveNext();
	}
}
catch(CDBFException* e)
{
	e->ReportError();
	e->Delete();
}

Append/Edit record

For addition, changes of record are functions:

    <li><code>AddNew
  • Edit
  • SetFieldValue
  • Delete
  • Undelete
  • Update

You also may use functions: LockRecord(), UnlockRecord(), GetFieldvalue().

try
{
	m_Set.Edit();
	m_Set.SetFieldValue("Key",varKey);
	m_Set.SetFieldValue("NAME",varName);
	m_Set.SetFieldValue("PHONE",varPhone);
	m_Set.Update();
}
catch(CDBFException* e)
{
	e->ReportError();
	e->Delete();
}

Find Functions

For find are supported functions:

    <li><code>FindFirst
  • FindNext
  • FindLast
  • FindPrev
try
{
	BOOL bFind = m_Set.FindFirst("LIKE('*Club*', NAME)");
	while(bFind)
	{
		// ...
		bFind = m_Set.FindNext();
	}
}
catch(CDBFException* e)
{
	e->ReportError();
	e->Delete();
}

Parameter of functions: FindFirst(), FindLast(), ... string expression of dBASE language.

Filter

For the filter SetFilter function is used:

    <li><code>SetFilter
try
{
	m_Set.SetFilter("LIKE('*Club*', NAME)");
	m_Set.Requery()
	// ...
}
catch(CDBFException* e)
{
	e->ReportError();
	e->Delete();
}

This function is supported only in classes CDBFCursorset and CDBFMemCursorset.

DBFView Demo

This sample shows some opportunities of library alxBASE.

More information about the product can be found at http://www.alxsoft.narod.ru.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alexey
Web Developer
Russian Federation Russian Federation
Member
Year of birth - 1974.
Eeducation - was ended by Kaliningrad State University in 1997.
Now I work as the engineer-programmer in Kaliningrad (RUSSIA).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralProblem in adding new recordsmemberMr.V.Stephen29 Jan '05 - 23:04 
I have tried the library.I did a simple Dialog based MFC project to create a new foxpro dbf file with a field called "NAME" and add new records to it.But when i run it,it is creating the database structure but while adding new record,it tells
 
" Debug Assertion failed"
" File: E:\alxBASE\Src\DBFRecordSet.cpp"
" Line : 450 "
 

void CDBFRecordset::AddNew()
{
ASSERT_VALID(this);
ASSERT(IsOpen()); "this is the line : 450 where it gets struck"
m_nEditMode = addnew;
 
memset(m_DBFFields.m_pCurRec, 0, m_pTableDef->m_DBFHead.rec_size);
 
COleVariant varEmpty;
varEmpty.Clear();
// ïðèñâàèâàåì âñåì ïîëÿì ïóñòîå çíà÷åíèå
for(short i = 0; i < m_DBFFields.GetFieldCount(); i++)
{
FIELD_REC* pFieldRec = m_DBFFields.GetFieldRec(i);
alxSetFieldValue(m_DBFFields.m_pCurRec, pFieldRec, varEmpty);
}
 
m_DBFFields.m_pCurRec[0] = REC_FLAG_NORMAL;
}
 
/* Èçìåíåíèå òåêóùåé çàïèñè */
void CDBFRecordset::Edit()
{
ASSERT_VALID(this);
ASSERT(IsOpen());
 
m_nEditMode = edit;
}
 

My program code is
 
#include "stdafx.h"
#include "newone.h"
#include "newoneDlg.h"
#include "DBFTableDef.h"
#include "DBFRecordset.h"
 

 
void CNewoneDlg::OnOK()
{
 
UpdateData();
try
{
// TODO: Add extra validation here
CDBFTableDef mNewTable;
CDBFRecordset m_Set;

char s[10];


// begin create
mNewTable.CreateTable(m_edit); //m_edit is the variable which holds the dbf file name given by the user.

 
// add field
FIELD_REC FieldRec;
memset(&FieldRec, 0, sizeof(FIELD_REC));
s[0]='N';s[1]='A';s[2]='M';s[3]='E';s[4]='\0';
memcpy(FieldRec.field_name,s, 20);
FieldRec.field_type = FLD_TYPE_CHARACTER;
FieldRec.len_info.char_len = 20;
mNewTable.AddField(&FieldRec);

// ...
 
// end create
mNewTable.UpdateTable();

 

m_Set.AddNew();
m_Set.Edit();
m_Set.SetFieldValue("Stephen",s);
m_Set.Update();

mNewTable.UpdateTable();

}
catch(CDBFException* e)
{
e->ReportError();
e->Delete();
}

CDialog::OnOK();
}
 

Please help me...Am i wrong anywhere?
Being a beginner,I m really interested in completing this small project .
 
Earnestly expecting your replies
 
V.Stephen

GeneralRe: Problem in adding new records PinmemberEndlessWinter10 Feb '05 - 16:18 
When you work with table structure, you use CDBFTableDef. But when you work with data itself you use CDBFRecordset. These are two different things. So, to add new records to the table, you must open it (see CDBFRecordset's Open method)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 6 Nov 2002
Article Copyright 2002 by Alexey
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid