473,405 Members | 2,310 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,405 software developers and data experts.

Allowing Nulls in Code


Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"
Marcus
******

Nov 13 '05 #1
4 1753
Marcus wrote:
Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"
Marcus
******


Perhaps the second # is not in the right place?

--
--
Lyle

"The aim of those who try to control thought is always the same. They
find one single explanation of the world, one system of thought and
action that will (they believe) cover everything; and then they try to
impose that on all thinking people."
- Gilbert Highet
Nov 13 '05 #2
Marcus:

This is probably a little more than what you asked for, but here's my
philosophy. (If you disagree with my, be gentle ... I'm just getting back
into posting at newsgroups after a long absence :-)

First, the simple answer and suggestion for your code. Check for nulls
and branch to a different SQL script depending on what the value is.

If IsNull([Forms]![frmSelect]![cboDate]) Then
strSQL = "SELECT * FROM tblData WHERE 1=1 AND strCriteria & ");"
Else
strSQL = "SELECT * FROM tblData WHERE tblData.Date = #" & _
[Forms]![frmSelect]![cboDate] & " AND strCriteria & ");"
End If
Now, the long answer.

I don't like referencing form controls in queries for two reasons:
1) The form might not be open, which fails quite ungracefully
2) You cannot easily do what you describe, substitute for NULLs

Here's the process I have been using lately:
1) Create a function that retrieves a value from a form
2) Handle all errors and missing data in the function
3) Use the function in the WHERE clause of the query.

It works something like this ...

Public Function GetFormDate() As Date
On Error Resume Next
Dim varValue As Variant
If IsLoaded("frmYourForm") Then
varValue = Forms!frmYourForm!txtDate
If IsNull(varValue) Or Not IsDate(varValue) Then
varValue = #12-31-1959#
End If
GetFormDate = varValue
Else
' insert bogus default data ... probably won't match anything
GetFormDate = #12-31-1959#
End If
End Function

Your Query now becomes ...
strSQL = "SELECT * FROM tblData WHERE tblData.Date = #" & _
GetFormDate() & "# AND " & strCriteria

If you're clever, you can return a String from this function and use
the LIKE operator to return ALL records by passing the wildcard char.

--
Danny J. Lesandrini
dl*********@hotmail.com
www.amazecreations.com/datafast
"Marcus" <to*******@yahoo.ca> wrote ...

Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"
Marcus
******

--
Danny J. Lesandrini
dl*********@hotmail.com
www.amazecreations.com/datafast/

"Marcus" <to*******@yahoo.ca> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com...
Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"
Marcus
******

Nov 13 '05 #3
Marcus wrote:
Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"


First of all, you've committed a cardinal sin 8) here by using a
reserved word, "Date" as the name of a field. DO NOT EVER DO THIS
AGAIN!!!! 8) It will confuse Access and its Jet SQL. I will use the
field name "TransDate" (short for "transaction date") as the replacement
in the stuff I'm writing below.

I have found that when constructing and SQL string in VBA, that using
the query by form notation simply doesn't work. In any event, the
structure you have above will not work proerly if it is transcribed the
way I think you re trying to do stuff into the query design grid. What
I do is this (air code):

dim strSQL as string

<construct strCriteria>

strSQL = "SELECT * FROM tblData WHERE "

if isnull(forms!frmSelect.cboDate) or forms!frmSelect.cboDate = "" then

strSQL = strSQL & strCriteria

else

strSQL = strSQL & "TransDate = #" & _
forms!frmSelect.cboDate & "#"

end if

This will actually put an actual value for the date in there each and
every time, rathger than relying on the query by form method. The
disadvantage is that you will need to run the code each time you change
the date, ie, make a new selection in your combo box. The code is very
quick, though, so it's not really a disadvantage...

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #4
Br
Marcus <to*******@yahoo.ca> wrote:
Wondering if it's possible to allow for Null enteries in SQL/VB code.
The example below doesn't work. Am I missing something? Or, is this
just not possible?

strSQL = "SELECT * FROM tblData " & _
"WHERE " & "tblData.Date = #" & [Forms]![frmSelect]![cboDate]
& " Or (" & [Forms]![frmSelect]![cboDate] Is Null & ")# AND (" &
strCriteria & ");"


Try:

strSQL = "SELECT * FROM tblData" & _
" WHERE " & "tblData.myDate = #" &
Format([Forms]![frmSelect]![cboDate], "mm/dd/yyyy") & _
" Or (" & [Forms]![frmSelect]![cboDate] Is Null & ") " & _
" AND (" & strCriteria & ");"

A few things..
- dates must be in american format
- don't use reserved words such as "date" for field names
- you had an extra # character in there
- make sure your combo box where you are getting the date from is bound
to the right field (or use full notation such as
[Forms]![frmSelect]![cboDate].Column(0))

--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #5

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

Similar topics

0
by: Dan Perlman | last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us> Subject: ODBC creating nulls? Date: Friday, July 09, 2004 10:43 AM Hi, Below is my VB6 code that writes data from an Access 2000 table to a PG table....
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
6
by: Modest Marsupial | last post by:
What is the DAO method of allowing a recordset to have null values? Thanks, marie
2
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
8
by: DaFrizzler | last post by:
Hi, I have received the following email from a colleague, and am quite frankly baffled by the idea. I am just wondering if anyone has any advice or suggestions about this???? === BEGIN MAIL...
5
by: John | last post by:
Hi I have a vb.net winform data app with sql server 2005 backend. The ID field in the tblClients table is an 'int' identity column that does not allow nulls. The problem is that when I try to...
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
0
by: csauer | last post by:
I am writing a program using visual basic.NET 2003 to be used to update and store data in a database I have stored in Access 2003. I am using a OdbcDataAdapter to generate my dataset, then using a...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.