473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Empty field in Access

Hi

I am a beginner in VB.NET, and have a problem with empty field in Access

I have transfered a worksheet in Excel to Access table. Some of the cels are empty
I use VB.NET program to acces this Access table (product.mdb)

One of the statements is to validate whether the field empty or not
My statement is
If Trim(dr_Product("bt_m3event")) = "" Or Trim(dr_Product("bt_m3event")) = " " The
........
End I

dr_Product ==> is OleDbDataReade
bt_m3event ==>is the field nam

The bt_m3event is empty
When this statement is processed, I got a message like below

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dl
Additional information: Cast from type 'DBNull' to type 'String' is not valid

I have tried so many times and many ways, but it didn't work
Can anybody tell me how to validate an empty field

Thanks in advance

Joachim

Nov 20 '05 #1
3 2998
"Joachim" <an*******@discussions.microsoft.com> schrieb
Hi,

I am a beginner in VB.NET, and have a problem with empty field in
Access.

I have transfered a worksheet in Excel to Access table. Some of the
cels are empty. I use VB.NET program to acces this Access table
(product.mdb).

One of the statements is to validate whether the field empty or
not. My statement is :
If Trim(dr_Product("bt_m3event")) = "" Or
Trim(dr_Product("bt_m3event")) = " " Then
.........
End If

dr_Product ==> is OleDbDataReader
bt_m3event ==>is the field name

The bt_m3event is empty.
When this statement is processed, I got a message like below :

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll Additional information: Cast from type
'DBNull' to type 'String' is not valid.

I have tried so many times and many ways, but it didn't work.
Can anybody tell me how to validate an empty field ?

First, you should enable Option Strict. Your code can not be compiled here
because the type of the DataReader's Item property is Object. You can not
compare every object to a string and you can't pass it to the Trim function.

As the exception message says, the field contains a Null value. The only
instance of Null in the Framework is DBNull.Value:

If dr_Product("bt_m3event") Is DBNull.Value Then
msgbox "Null found"
else
dim FieldContent as string
FieldContent = dr_Product("bt_m3event").ToString
end if

For all other date types, you must use type casting:

If dr_Product("AnyIntegerField") Is DBNull.Value Then
msgbox "Null found"
else
dim FieldContent as Integer
FieldContent = Directcast(dr_Product("bt_m3event"), Integer)
end if
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
* "=?Utf-8?B?Sm9hY2hpbQ==?=" <an*******@discussions.microsoft.com> scripsit:
I have transfered a worksheet in Excel to Access table. Some of the cels are empty.
I use VB.NET program to acces this Access table (product.mdb).

One of the statements is to validate whether the field empty or not.
My statement is :
If Trim(dr_Product("bt_m3event")) = "" Or Trim(dr_Product("bt_m3event")) = " " Then


'If dr_Product("bt_m3event") Is DBNull.Value Then...'. The right part of
the 'Or' IMO doesn't make sense at all because 'Trim' will remove the spaces.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Armin and Herfried
Thanks to you. It works
But I have another problem. I have posted it in the other thread
I think you can help me out.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
5
by: Adrian Parker | last post by:
Hi. I have a date time picker in my program which uses ADO to read from an Access database. It works perfectly, unless the database is empty (no records) when opened. When you try to open an...
5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
2
by: ET | last post by:
Hi! I'll need help on what todo with this information in table... There has to be a unique on one column (Item ID, for inventory purpose) but in many cases that ID is not known... thus, many...
2
by: AA Arens | last post by:
I have button and added the command that places a date and some stripes in the Notes-field. That works fine. If there is already some text, it places the date at a next line: Private Sub...
1
by: G04 | last post by:
Hi All, I have a continuous form with all records. For each field there is a combo in the form header and the form also contains a Toggle button "Apply Filter". When clicked, it changes to...
0
by: Mich | last post by:
Hi, i made a gridview connected to a sql server db and i need to test the value of a field of the gridview.. The problem is that field can be empty (e.g. when the content of that field was...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.