Solved

CDaoDatabase: how to know if value of a field is empty?

Posted on 2000-02-26
8
194 Views
Last Modified: 2013-11-20
Hi,

in a .mdb database, the value of a field may be empty, that is , nothing in that field (actually, since the database initially is not created by me, so i dunno whether it is " " or just leave it empty there). is there any way to check whether it is empty?
i had use
COleVariant v1;

v1=rs.GetFieldValue(ex_fd[0]);
s1=(char *)v1.bstrVal;

to get the value .. however if i use strcmp(s1," ")==0) to check. it gives error....

pls help me!!! thanks a lot!
0
Comment
Question by:greenbird
  • 3
  • 3
  • 2
8 Comments
 
LVL 1

Expert Comment

by:hiap_ho
ID: 2567067
You should be able to use strcmp, however usually I would use it in the following manner

CString s1;
s1=(LPTSTR)v1.bstrVal;
if(!strcmp(s1,""))blablabla;
blablabla == action taken when s1 is empty string.

hope this will help
0
 

Author Comment

by:greenbird
ID: 2567668
Hi,

well the problem is that i can not use strcmp( it is only my opinion) since actually there is nothing in the field... so how to check it is EMPTY initially ? can strcmp(NULL...)? thanks!
0
 
LVL 1

Expert Comment

by:hiap_ho
ID: 2567706
nope
you can still check using strcmp, just use strcmp(s1,"") <--- will give you 0 if s1 is empty.
The mistake is probably lies on
s1=(char *)v1.bstrVal;

change that line to
use CString instead of char*
CString s1;
s1=(LPTSTR)v1.bstrVal;

another way to test if
if(s1=="") <--- this will also work if you use CString

hope that will help

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Expert Comment

by:hiap_ho
ID: 2567719
anyway you can still try to use char*

but use
strcpy(s1,(LPTSTR)v1.bstrVal);
or
strcpy(s1,(char*)v1.bstrVal);

I have not tested this method however it might just work.
Anyway
You cannot assign char* using "=" operator.

char* s1;
s1="baba" <=== not allowed
correct way should be
strcpy(s1,"baba");

0
 
LVL 3

Accepted Solution

by:
V_Bapat earned 10 total points
ID: 2569028
Derive a class from CDaoRecordset for your table. This will give you a member variable of type CString.
Now when you move to the record, you can check IsEmpty() for that.
0
 

Author Comment

by:greenbird
ID: 2571121
Hi,

could u pls illustrate it by some code? which member variable? sorry i am new to VC++, so know little abt db... thanks a lot!
0
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2571757
Derive a class from CDaoRacordset. Select the DAO in Datasource and provide your mdb filename. From the displayed list of tables for that database select the table for which you want to create the recordset.

This will give you member variables for all the fields which you can use directly instead of using GetFieldValue and SetFieldValue.

Suppose there is one text field 'Name' in your table, you will get (or you specify) the member variable m_Name, then you can use it as any other member variable.

CMyRecordset rs;  //CMyRecordset is derived from CDaoRecordset
rs.Open();
if(rs.m_Name.IsEmpty())
{
.....
}
else
{
.....
}


This method is RFX. Check out the technical articles(TNXXX where XXX is the article no) from MSDN on databases.
0
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2586533
Oooops! It is DFX and not RFX.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now