• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

Why is this an Invalid Operation?

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
MacRena
Asked:
MacRena
  • 4
  • 2
  • 2
  • +1
1 Solution
 
mgrattanCommented:
It should work if you don't explicitly reference the .Value property.  Just remove the .Value from your code and try again...
0
 
devthaCommented:
I agree with MG
0
 
MacRenaAuthor Commented:
mgrattan,

Thanks for replying!

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

Good thought, though.

Mac
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
DennisBorgCommented:
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
 
mgrattanCommented:
Dennis is quite correct; you need to loop through the Fields collection of the Recordset to get values, not the TableDef.
0
 
MacRenaAuthor Commented:
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
 
MacRenaAuthor Commented:
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
 
DennisBorgCommented:
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
 
MacRenaAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now