Solved DAO.field doesn't give the field name (1 Viewer)

freuzo

Member
Local time
Today, 11:47
Joined
Apr 14, 2020
Messages
98
Hello,

I have this code

Code:
'code....
    Dim tdf As DAO.TableDef
    Dim fld
    Set fld = New DAO.field
 
    sql = "select * from t_import_agence"
    Set rst = CurrentDb.OpenRecordset(sql)

    Set tdf = CurrentDb.TableDefs("t_import_agence")

    If Not rst.EOF Then       
        strlines = "UPDATE agence SET "       
        For Each fld In tdf.Fields
            rst.MoveFirst           
            strlines = strlines & fld.name           
            Do While Not rst.EOF               
                strlines = strlines & " = CASE WHEN " & rst!agence_id & " THEN " & Nz(fld.Value, "NULL")
                rst.MoveNext
            Loop           
            strlines = strlines & " END ,"     
        Next fld
    End If
'code...


but I get an error : 3420 Object invalid on the line : For Each fld In tdf.Fields.

I don't understand. Also in DAO.field the 'f' is always in lower case. I think the issue comes from there. I even imported all the projectg to another file but same issue.
What is hapening there ?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:47
Joined
May 21, 2018
Messages
8,529
When working with fields of table defs you need to save a pointer to the current db by creating a variable for the database.

dim db as dao.database
dim tdf as dao.tabledef
dim fld as dao.field
dim rst as dao.recordset

set db = currentDb
set tdf = db.tabledefs("t_import_agence")
 

Minty

AWF VIP
Local time
Today, 11:47
Joined
Jul 26, 2013
Messages
10,371
Firstly I would delete this line
Code:
Set fld = New DAO.field
Secondly, it looks like this is creating a SQL String to pass through to a SQL server, when a simple Update query could probably do the same thing.

Take a step back and explain the task at hand.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:47
Joined
Sep 21, 2011
Messages
14,302
Why haven't you defined fld as DAO.Field and instead left it as variant?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:47
Joined
May 21, 2018
Messages
8,529
Good article here explaining the need for a persistent datbase variable and why it differs from recordsets.
 

freuzo

Member
Local time
Today, 11:47
Joined
Apr 14, 2020
Messages
98
Firstly I would delete this line
Code:
Set fld = New DAO.field
Secondly, it looks like this is creating a SQL String to pass through to a SQL server, when a simple Update query could probably do the same thing.

Take a step back and explain the task at hand.

I'm trying to build a bulk update sql.
This is just the start of all the string I'll build but before go forward I can't get the field name.
 

freuzo

Member
Local time
Today, 11:47
Joined
Apr 14, 2020
Messages
98
When working with fields of table defs you need to save a pointer to the current db by creating a variable for the database.

dim db as dao.database
dim tdf as dao.tabledef
dim fld as dao.field
dim rst as dao.recordset

set db = currentDb
set tdf = db.tabledefs("t_import_agence")
I did this and It gave me the field's name but faild to give the value. Error 3219 Invalid operation on the line where I want the value (e.g Debug.print fld.Value)
 

freuzo

Member
Local time
Today, 11:47
Joined
Apr 14, 2020
Messages
98
Got the value by changing
Code:
For Each fld In tdf.Fields
to
Code:
For Each fld In rst.Fields

:):)

Thanks @MajP for pointing me in the right direction.
 

Users who are viewing this thread

Top Bottom