Solved

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

Posted on 2000-02-26
8
195 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum28 challenge 31 115
only14 challenge 19 81
isEverywhere  challenge 19 82
zeroFront challenge 7 111
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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