Problem creating Field

Ragnar78

Board Regular
Joined
Feb 10, 2004
Messages
210
Hy,

I use a Table Query to generate a table, and i want to add a Field in this newly created table.
So this is the code i use...

The problem is that it does not create the field and it blocks at
!WEEK_STATUS

Code:
Sub Moveinto()

Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim MyTable As DAO.TableDef
Dim x As String

Set MyDB = CurrentDb
Set MyTable = MyDB.CreateTableDef("Retreived_Data", dbOpenDynaset)

With MyTable
     .Fields.Append .CreateField("WEEK_STATUS", dbText, 7)
End With

Set MyRS = MyDB.OpenRecordset("Retreived_Data", dbOpenDynaset)
    
    With MyRS
         .MoveFirst
         .Requery
         While Not .EOF
               x = CStr(Year(.Fields("SYSMODTIME").Value) & "w" & Format(.Fields("SYSMODTIME").Value,"ww"))
               .Edit
               !WEEK_STATUS = x
                Debug.Print .Fields(0).Value & .Fields(4).Value
               .MoveNext
               .Update
         Wend
    End With

End Sub

Can anyone help me out? thx in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
For a start you need to append MyTable to MyDB tabledefs collection.

Also remove the dbOpenDynaset from the CreateTableDef method.
 
Upvote 0
How do i do that?
Code:
With MyTable 
     .Fields.Append .CreateField("WEEK_STATUS", dbText, 7) 
End With

Dosen't this part of the code Add a field to the table and considers it as a new added object?
 
Upvote 0
MyDB.TableDefs.Append MyTable
 
Upvote 0
Code:
Sub Moveinto()

Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim MyTable As DAO.TableDef
Dim K As Field
Dim x As String

Set MyDB = CurrentDb

Set MyTable = MyDB.TableDefs!Retreived_Data

With MyTable
     .Fields.Append .CreateField("WEEK_STATUS", dbText, 7)
End With


Set MyRS = MyDB.OpenRecordset("Retreived_Data", dbOpenDynaset)

    
    With MyRS
         .MoveFirst
         .Requery
         While Not .EOF
               x = CStr(Year(.Fields("SYSMODTIME").Value) & "w" & Format(.Fields("SYSMODTIME").Value, "ww"))
               .Edit
               !WEEK_STATUS = x
                Debug.Print .Fields(0).Value & .Fields(4).Value
               .MoveNext
         Wend
    End With

End Sub

Ok i managed to fix it with this code...
If you have any comment on it please tell me so that i might improve.
Thanks again for all the help...

(y)
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,816
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top