Solved

Why is this an Invalid Operation?

Posted on 2001-08-06
9
533 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
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 8

Accepted Solution

by:
DennisBorg earned 50 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

809 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