Solved

Why is this an Invalid Operation?

Posted on 2001-08-06
9
524 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now