473,382 Members | 1,809 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,382 software developers and data experts.

Table defaults not used when adding record in code

I have a table with about 80 fields. I'm using an import process to populate
the table. It works fine, except for the following:

Users generally don't specify values for a lot of numerical fields on the
spreadsheet. I set the Access table up so that all the numerical values
would have a default value of zero.

I thought that when the record was appended to the table that the numerical
fields that do not have values would be defaulted to zero by Access. This is
not happening. The field values show nothing in the table.

How do I get Access to default these blank values to zero in the table using
my import process.

Thanks.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200610/1

Oct 15 '06 #1
5 1750
rdemyan via AccessMonster.com wrote:
I have a table with about 80 fields. I'm using an import process to
populate the table. It works fine, except for the following:

Users generally don't specify values for a lot of numerical fields on
the spreadsheet. I set the Access table up so that all the numerical
values would have a default value of zero.

I thought that when the record was appended to the table that the
numerical fields that do not have values would be defaulted to zero
by Access. This is not happening. The field values show nothing in
the table.

How do I get Access to default these blank values to zero in the
table using my import process.

Thanks.
A default is not applied when you append a row with a null in that field. The
default is applied when you append a row that does not include that field at
all.

For example: If I have a table with two fields [ID] and [SomeNumber] and
[SomeNumber] has a default value of zero then these are the results I will get
from two different append queries...

INSERT INTO TableName
VALUES(1, Null)

result: [ID]=1 SomeNumber = Null
INSERT INTO TableName
VALUES(1)

result: [ID] = 1 SomeNumber = 0

So as long as your Excel import includes all fields then your defaults will
never be applied.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Oct 15 '06 #2
you could then cycle the tabledef and run an update query for each field a
bit like this to make all the nulls zeros
This will of course mean that 80 queries will run

'Code assumes you are using DAO

Sub SubMakeNullZeros()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = CurrentDb
Set tdf = db.TableDefs("MyTableName")
For Each fld In tdf.Fields
db.Execute "UPDATE [" & tdf.Name & "] SET [" & fld.Name & "] = 0
WHERE [" & fld.Name & "] IS NULL"
Next
Set tdf = Nothing
Set db = Nothing

msgbox "Done"
End Sub
"Rick Brandt" <ri*********@hotmail.comwrote in message
news:JC******************@newssvr13.news.prodigy.c om...
rdemyan via AccessMonster.com wrote:
>I have a table with about 80 fields. I'm using an import process to
populate the table. It works fine, except for the following:

Users generally don't specify values for a lot of numerical fields on
the spreadsheet. I set the Access table up so that all the numerical
values would have a default value of zero.

I thought that when the record was appended to the table that the
numerical fields that do not have values would be defaulted to zero
by Access. This is not happening. The field values show nothing in
the table.

How do I get Access to default these blank values to zero in the
table using my import process.

Thanks.

A default is not applied when you append a row with a null in that field.
The default is applied when you append a row that does not include that
field at all.

For example: If I have a table with two fields [ID] and [SomeNumber] and
[SomeNumber] has a default value of zero then these are the results I will
get from two different append queries...

INSERT INTO TableName
VALUES(1, Null)

result: [ID]=1 SomeNumber = Null
INSERT INTO TableName
VALUES(1)

result: [ID] = 1 SomeNumber = 0

So as long as your Excel import includes all fields then your defaults
will never be applied.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com


Oct 15 '06 #3
"tombsy" <ic***********@tiscali.co.ukwrote in
news:45**********@mk-nntp-2.news.uk.tiscali.com:
you could then cycle the tabledef and run an update query for each
field a bit like this to make all the nulls zeros
This will of course mean that 80 queries will run

'Code assumes you are using DAO

Sub SubMakeNullZeros()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = CurrentDb
Set tdf = db.TableDefs("MyTableName")
For Each fld In tdf.Fields
db.Execute "UPDATE [" & tdf.Name & "] SET [" & fld.Name &
"] = 0
WHERE [" & fld.Name & "] IS NULL"
Next
Set tdf = Nothing
Set db = Nothing

msgbox "Done"
End Sub
A better what to do this would be to use a recordset that includes
only the numeric fields that you want to set to 0.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Oct 15 '06 #4
Since all the numeric fields were intended to default to zero,
couldn't you just test for datatype as you walk the fields and apply
the query only to numeric fields?

"David W. Fenton" <XX*******@dfenton.com.invalidwrote in message
news:Xn**********************************@127.0.0. 1...
"tombsy" <ic***********@tiscali.co.ukwrote in
news:45**********@mk-nntp-2.news.uk.tiscali.com:
>you could then cycle the tabledef and run an update query for each
field a bit like this to make all the nulls zeros
This will of course mean that 80 queries will run

'Code assumes you are using DAO

Sub SubMakeNullZeros()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = CurrentDb
Set tdf = db.TableDefs("MyTableName")
For Each fld In tdf.Fields
db.Execute "UPDATE [" & tdf.Name & "] SET [" & fld.Name &
"] = 0
WHERE [" & fld.Name & "] IS NULL"
Next
Set tdf = Nothing
Set db = Nothing

msgbox "Done"
End Sub

A better what to do this would be to use a recordset that includes
only the numeric fields that you want to set to 0.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Oct 16 '06 #5
"Kc-Mass" <co********@comcast.netwrote in
news:1f******************************@comcast.com:
"David W. Fenton" <XX*******@dfenton.com.invalidwrote in message
news:Xn**********************************@127.0.0. 1...
>"tombsy" <ic***********@tiscali.co.ukwrote in
news:45**********@mk-nntp-2.news.uk.tiscali.com:
>>you could then cycle the tabledef and run an update query for
each field a bit like this to make all the nulls zeros
This will of course mean that 80 queries will run

'Code assumes you are using DAO

Sub SubMakeNullZeros()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Set db = CurrentDb
Set tdf = db.TableDefs("MyTableName")
For Each fld In tdf.Fields
db.Execute "UPDATE [" & tdf.Name & "] SET [" & fld.Name
& "] = 0
WHERE [" & fld.Name & "] IS NULL"
Next
Set tdf = Nothing
Set db = Nothing

msgbox "Done"
End Sub

A better what to do this would be to use a recordset that
includes only the numeric fields that you want to set to 0.

Since all the numeric fields were intended to default to zero,
couldn't you just test for datatype as you walk the fields and
apply the query only to numeric fields?
Well, sure, but that's only better if you're creating a generic
subroutine. If you're hardwiring the tablename (as the code does),
then it's going to be more efficient to define which fields you want
it applied to.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Oct 16 '06 #6

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

Similar topics

3
by: Claudio Lapidus | last post by:
Hello Now perhaps this is a bit dumb, but... I just populated a new table via \copy. After that, I realize that perhaps is a good thing to have a row identifier in it, so I try clapidus=>...
5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
3
by: Andrew | last post by:
Here's my problem: I built a system for data entry and export based on a schema given to my by the state. The output of the data is fixed-width, and a good number of the fields in each row...
13
by: Shannan Casteel via AccessMonster.com | last post by:
I set up two tables (one with the regular claim info and another with ClaimNumber, PartNumber, and QuantityReplaced). The ClaimNumber is an autonumber and the primary key in both tables. I made a...
0
by: Hank | last post by:
This morning when I brought my Access 2000 database, some workers had problems signing in on the Time Clock. While debugging I noticed that, when adding a new record, the numerical fields in the ...
2
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new...
3
by: Amanduh | last post by:
Okay, this is kind of a weird question that probably has an obvious answer. I'm making a form that allows me to enter new visits for a study that has multiple visits. For example, Study1 has...
4
by: jbrumbau | last post by:
Hello, I have been successfully using a database I've created for several months to populate an equipment list for a project we've been working on. However, the form has recently stopped working...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.