Solved

Why is this an Invalid Operation?

Posted on 2001-08-06
9
539 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Back Up Your Microsoft Windows Server®

Back up 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.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

738 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