[VBA] Operation not allowed for this type of object.

Solved
ludobalu Posted messages 286 Status Membre -  
ludobalu Posted messages 286 Status Membre -
Hello,

I recently learned Visual Basic, but now that I'm using it with Access, I'm having some issues, particularly because I want to refer to fields in tables, etc., which is not the same as with Excel and I'm having a bit of trouble. So I hope you can forgive me if you ever find my mistakes to be glaring.

Before explaining my problem, let me outline the setup:

I have a table named "t_Card" containing a column named "i_Card" (which corresponds to the primary key of this table).
I have a second table named "t_Channel" that also contains the column "i_Card"; these are two linked tables, and for each value of i_Card in the t_Card table, there corresponds one or more records in the t_Channel table.

I would like to count the number of non-empty records in the t_Channel table for each value of i_Card in the t_Card table, and then assign this number to a new column in the t_Card table for each record.

(Sorry if I'm not explaining myself well, but basically I would like to display for all records in the t_Card table the number of non-empty records that correspond to them in relation to the t_Channel table)

Here’s the code I’ve written:

Sub essai()
Dim Card As Object
Dim t_Card As DAO.TableDef
Dim db As DAO.Database
Set db = CurrentDb
Set t_Card = db.TableDefs("t_Card")

For Each Card In t_Card
'For each record in the t_card table, i takes the value of i_card from the t_card table and
'assigns the variable nbre_channels_occupes to the channels_occupes field of the t_card table
i = i_Card.Value
'Count the total number of channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_total_channels = DCount("[i_Channel]", "t_Channel", "[i_Card] ='& i&'")
'Count the number of empty channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_channels_vides = DCount("[i_Channel]", "t_Channel", "IsNull([i_Signal_input])")
'Calculate the number of occupied channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_channels_occupes = nbre_total_channels - nbre_channels_vides
'For each value of i_Card from the t_Card table, the Total_Channels field takes the value of the variable nbre_channels_occupes
If t_Card.Fields("i_Card") = i Then
t_Card.Fields("Total_Channels") = nbre_channels_occupes
End If
Next Card

End Sub

The issue arises at the "For Each...", here's the error I get:

Operation not supported for this type of object.

Could you please indicate a solution? Thank you very much!

2 réponses

Heliotte Posted messages 1561 Status Membre 92
 
Card represents an Object, which one?
0
ludobalu Posted messages 286 Status Membre 6
 
Hello,

Thank you for your reply. Since yesterday, I've received help and I've arrived at this code:

Option Compare Database
Option Explicit

Sub essai()

Dim sql As String, i As Integer, nbre_total_channels As Integer, nbre_channels_vides As Integer, nbre_channels_occupes As Integer
Dim rst As dao.Recordset

Set rst = CurrentDb.OpenRecordset("t_Card")

Do Until rst.EOF = False

i = rst.Fields("i_Card")

'Count the total number of channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_total_channels = DCount("[i_Channel]", "t_Channel", "[i_Card] =" & i)
'Count the number of unused channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_channels_vides = DCount("[i_Channel]", "t_Channel", "IsNull([i_Signal_input])")
'Calculate the number of occupied channels in the t_Channel table for t_card.i_card=t_channel.i_card
nbre_channels_occupes = nbre_total_channels - nbre_channels_vides
'For each value of i_Card in the t_Card table, the Total_Channels field takes the value of the nbre_channels_occupes variable

If rst.Fields("i_Card") Then
rst.Fields("Total_Channels") = nbre_channels_occupes
End If

rst.MoveNext
Loop

End Sub

However, there are no more error messages, but nothing happens. That is, the Total_Channels column, which is supposed to be filled with the nbre_channels_occupes variable, is empty. I don't understand why...

Could you help me?

Thank you very much in advance!
0
Heliotte Posted messages 1561 Status Membre 92
 
Hello,
You're writing in a recordset!
If rst.Fields("i_Card") Then rst.Fields("Total_Channels") = nbre_channels_occupes End If

If you want to update records, shouldn't you be using an UPDATE?
0
ludobalu Posted messages 286 Status Membre 6
 
Yes, it serves to record the changes made, but it does not perform said changes,

We would need something like:

If rst.Fields("i_Card") Then Write the value of nbre_channels_occupes in the Total_Channels field of the t_Card table for the corresponding record rst.Update End If


Only, what is in italics I cannot translate into VBA, I tried doing this:

rst.Edit.Fields("Total_Channels") = nbre_channels_vides 

And I get this error message (it’s the same when replacing Edit with AddNew):
Compile error: Function or variable expected.


Regarding the ".Edit"

Do you have any idea?

Thank you!
0
Heliotte Posted messages 1561 Status Membre 92
 
I think I will use INSERT INTO if you want to add a record
CurrentDb.Execute "INSERT INTO [NomDeLaTable.Total_Channels] VALUES (nbre_channels_vides);"

In case you want to modify (update) a record, you will need to write UPDATE
To be tested!
0
ludobalu Posted messages 286 Status Membre 6
 
I tried and I think I'm going completely crazy... Here is the error message I get:
Runtime error '3011': The Microsoft Jet database engine could not find the object 't_Card.Total_Channels'. Make sure the object exists and that you have correctly spelled its name and path.


And yet, my table has exactly this name, as does the column to fill, proof in the image:

http://imagik.fr/view-rl/4756

I really don't know what to do anymore, I'm completely lost!
0
Heliotte Posted messages 1561 Status Membre 92
 
Do you work with ACCESS? If so, please put brackets around table names and field names for safety. For example: `[t_Card].[Total_Channels]`
Is the database still accessible? Open?
Show me the line you wrote, please.
Try again.
0