Solved

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

Posted on 2000-02-26
8
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 112
Expand macro to ask for filename column 8 37
Whole sheet autoscrub still needed 19 54
Define unique primary key 9 95
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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