?
Solved

Why is this an Invalid Operation?

Posted on 2001-08-06
9
Medium Priority
?
552 Views
Last Modified: 2008-02-01
Hello Experts,

tdfPref.Fields(intI).Value  gives an Invalid Operation error.

I am trying to test the value of all fields in my table.
If there is a null, then I want to put in a value.

This breaks when I try to set the variant to the Value of the field (even though the current field is not Null)

It breaks the first time through on this line in the For / Next Loop ...
varValue = tdfPref.Fields(intI).Value

even though " .Value" is offered as a Property of the TableDef object.

It also breaks when I use it raw like...

If IsNull(tdfPref.Fields(intI).Value) Then

Here is the code
**************************************

Private Sub Form_Close()
'test the felds in preferences to be sure there are no Nulls
Dim db As Database
Dim tdfPref As TableDef
Dim rsPref As DAO.Recordset
Dim intI As Integer
Dim varValue As Variant
Set db = DBEngine.Workspaces(0).OpenDatabase("C:\MyDB.mdb")
Set tdfPref = db.TableDefs("Preferences")
Set rsPref = db.OpenRecordset("Preferences")

For intI = 0 To tdfPref.Fields.Count - 1
          varValue = tdfPref.Fields(intI).Value    '<--  WAAAAA !!!
          If IsNull(varValue) Then
                    rsPref.Edit
                    If rsPref.Fields(tdfPref.Fields(intI).Name).Type = dbText Then
                              rsPref.Fields(tdfPref.Fields(intI).Name).Value = ""
                    ElseIf rsPref.Fields(tdfPref.Fields(intI).Name).Type = dbNumeric Then
                              rsPref.Fields(tdfPref.Fields(intI).Name).Value = 0
                    End If
                    rsPref.Update
          End If
Next
rsPref.Close
Set rsPref = Nothing
Set db = Nothing
End Sub

**********************************************


I've got MS DAO 2.5/3.5 Object library checked.

I thought this should work.  Anyone know how I can do this?


Thanks,
Mac
0
Comment
Question by:MacRena
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:mgrattan
ID: 6357128
It should work if you don't explicitly reference the .Value property.  Just remove the .Value from your code and try again...
0
 
LVL 6

Expert Comment

by:devtha
ID: 6357139
I agree with MG
0
 
LVL 4

Author Comment

by:MacRena
ID: 6357143
mgrattan,

Thanks for replying!

Nope,  varValue = tdfPref.Fields(intI)  breaks with the same message.

Good thought, though.

Mac
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 8

Accepted Solution

by:
DennisBorg earned 200 total points
ID: 6357239
Mac:

The Fields in a TableDef do not have values .... they merely help define the structure of the table.

The *recordset* you opened is the object which access the data contained in the table. The *tabledef* does not access the data, only the Table Definition.

Recordset objects points to only one record at a time. To see if any of the fields of a recordset (for *all* records) are null, you can use code similiar to the following:

   Dim db     As DAO.Database
   Dim rsPref As DAO.Recordset
   Dim intI   As Integer

   Set db = DBEngine.Workspaces(0).OpenDatabase("C:\MyDB.mdb")
   Set rsPref = db.OpenRecordset("Preferences", dbOpenDynaset)
   Do While Not rsPref.EOF
      For intI = 0 To rsPref.Fields.Count - 1
         If IsNull(rsPref.Fields(intI).Value) Then
            rsPref.Edit
            Select Case rsPref.Fields(intI).Type
               Case dbText
                  rsPref.Fields(intI).Value = ""
               Case dbNumeric
                  rsPref.Fields(intI).Value = 0
            End Select
            rsPref.Update
         End If
      Next intI

      rsPref.MoveNext 'Go to the next record
   Loop
   rsPref.Close
   Set rsPref = Nothing
   db.Close
   Set db = Nothing


Though, I don't know why you are replacing all NULLS with empty strings. I usually make sure there are no empty strings and use Nulls instead.

-Dennis Borg
0
 
LVL 14

Expert Comment

by:mgrattan
ID: 6357255
Dennis is quite correct; you need to loop through the Fields collection of the Recordset to get values, not the TableDef.
0
 
LVL 4

Author Comment

by:MacRena
ID: 6357313
DennisBorg,
First, thank you, Dennis.  You are right.
Second, since this is a Preferences table, there is only 1 record, so no Loop is required.
Third, now that I think about it, I don't really need to replace Text Nulls, but I do need to replace Numeric Nulls because there are calculations that use DLookup for the values here, and if the user neglects to fill in a value, it causes the functions to fail, and I want to "IdiotProof" this program.

Thanks to you also mgrattan & devtha.

Mac
0
 
LVL 4

Author Comment

by:MacRena
ID: 6357330
Before you tell me to use Nz...
Finding all the calculations and adding Nz is less attractive than just fixing it once and for all.  I didn't write this program, it is pretty big and started as a Clipper ap, then went to Access 2.0, and is now Access 97.  Nz didn't exist back then, and retrofitting would take more time (un-billable time, I might add)
Thanks again!
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6357946
Mac:

You're welcome, Mac.

A couple suggestions I have concerning the numeric fields and the null values.

You are doing right in replacing all nulls with zeros where zeros are expected.

I would suggest the following (perhaps you've already done them):

   1) Modify the Table Structure, setting the Default Value property of these numeric fields to zero. This way, if a user does not enter anything in for these numeric fields, they will be zero by default.

   2) While modifying the table structure, also set the Required Field property for these numeric fields to True. This way, if a user attempts to leave the field null (by deleting its value), the attempt will fail.


-Dennis Borg
0
 
LVL 4

Author Comment

by:MacRena
ID: 6358943
Hi Dennis,
I always make the Default = 0 when I create a program, but this is for an upgrade to an existing program and the original programmer didn't do that.  Now I have dozens of copies of the program out there that may have nulls in already existing records, so I want to force this problem to go away.
Thanks for the help.
Mac
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

764 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