Access VBA unable to create table - ms-access

I'm having trouble creating an Access table using VBA, then accessing it via open-recordset. My goal is to write out records into a table. If I manually create the table, my code works perfectly. If the table does not exist, the program terminates at the open-recordset attempt. The error msg is:
"The MSO Access Database engine could not find the object 'myTable'
Where "myTable" is the value of argTable. Here's the code snippet:
Dim tbl As DAO.TableDef
Dim db As DAO.Database
Dim fld As DAO.Field
Set db = CurrentDb()
Set tbl = db.CreateTableDef(argTable)
Set fld = tbl.CreateField("F1")
Set rstAccessTableOut = db.OpenRecordset(argTable, dbOpenTable)
What am I doing wrong? (Note I do not want to use SQL.)

After you finish adding the fields you have to append the new table def to the collection to be able to use it. Also the filed needs to be appended as well
Dim tbl As DAO.TableDef
Dim db As DAO.Database
Dim fld As DAO.Field
Set db = CurrentDb()
Set tbl = db.CreateTableDef(argTable)
Set fld = tbl.CreateField("F1", dbText, 20)
tbl.Fields.Append fld
db.TableDefs.Append tbl
Set rstAccessTableOut = db.OpenRecordset(argTable, dbOpenTable)

Related

How to cycle through record set and update table field values based on record set?

I am importing similar tables into a MS Access database to combine them into a larger data set. The first row of most of the columns are date fields. During import when the first row becomes field names, some of these dates stay dates "January-2018" and some of them become numbers "44001". I am writing a code to reference any numbers that are store as the field names and turn them into date values (ex. 44001 to January-2018).
Private Sub Command0_Click()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim rs As DAO.Recordset
Dim CurrentHead As String
Dim UpdateHead As String
Set db = CurrentDb
Set tdf = db.TableDefs("PL_1")
Set rs = db.OpenRecordset("TableUpdates")
rs.MoveFirst
Do While Not rs.EOF
For Each fld In tdf
If fld.Name = CurrentHead Then
fld.Name = UpdateHead
End If
rs.MoveNext
Loop
db.Close
Set db = Nothing
Set fld = Nothing
Set tdf = Nothing
MsgBox "Changed"
End Sub

ACCESS VBA adding fields Error 3219

i'm trying to add 2 columns to the table "schede" but i always return Error 3219. Can anyone help? Here is the code:
Dim db As Database
Set db = CurrentDb()
'add columns
Dim campo1 As Field
Set campo1 = db.TableDefs("SCHEDE").CreateField("cliente", dbText, 6)
CurrentDb.TableDefs("SCHEDE").Fields.Append (campo1)
Set campo1 = CurrentDb.TableDefs("SCHEDE").CreateField("da_canc", dbText, 3)
CurrentDb.TableDefs("SCHEDE").Fields.Append (campo1)
As Fields.Append is a sub (not a function) you have to pass its arguments without parentheses (or add aCallat beginning). Also reuse your database reference (db).
Dim db As DAO.Database 'Library name to clearfy reference (e.g ADODB has a database object too)
Set db = CurrentDb()
'add columns
With db.TableDefs("SCHEDE") 'for DRY
.Fields.Append .CreateField("cliente", dbText, 6)
.Fields.Append .CreateField("da_canc", dbText, 3)
End With

How to create a field in in a preexisting table with VBA for MS Access?

I have a Database Database and a table Table. I have found online how to create a field from a newly created database and table in VBA in MS Access, but I do not know how to do this when the database and table already exist before the macro has been run.
To do it without a preexisting database and table, I can run the following:
Sub CreateTable()
Dim db As DAO.Database
Dim table1 As DAO.TableDef
Set db = CurrentDb
Set table1 = db.CreateTableDef("ExampleTable")
With table1
.Fields.Append.CreateField("newField", String)
End With
But how could I adapt this to add the same field to a preexisting table?
Or more specifically, how do I modify
Set table1 = db.CreateTableDef("ExampleTable")
So that table1 points to an existing table in the db database?
You can add a field to an existing table by executing an Access DDL statement.
Dim strDdl As String
strDdl = "ALTER TABLE ExampleTable ADD COLUMN newField TEXT(255);"
CurrentProject.Connection.Execute strDdl
In TEXT(255), 255 is the field size --- the maximum number of characters that field can contain. And 255 is the absolute upper limit for an Access text field. When creating a TEXT field from the ADODB.Connection.Execute method, you must include a value for field size. Choose a smaller value if you prefer.
When you create a new field using DAO methods, as in your question, the field size for a text field is optional. However, when you don't specify a size, Access uses the "Default text field size" setting from your Access options.
Something like this. Just reference the table name in the TableDefs collection rather than creating one.
Public Sub CreateTable()
Dim table1 As DAO.TableDef
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
' Create the new Table Def object
Set table1 = New DAO.TableDef
' Name the Table
table1.Name = "Test"
' Create the new Field
Set fld = New DAO.Field
fld.Name = "newField"
fld.Type = DataTypeEnum.dbText
' Append the Field to the table
table1.Fields.Append fld
' Append the table to the Table Def Collection
' Without this the table just lives in memory
CurrentDb.TableDefs.Append table1
' Create another Field
Set fld = New DAO.Field
fld.Name = "newField2"
fld.Type = DataTypeEnum.dbText
' Append the New Field to the EXISTING table
CurrentDb.TableDefs("Test").Fields.Append fld
End Sub

Change all values in a field in a MS Access database to a constant

I have a MS Access database with 100 tables, each has a field named "M_ID"
I want all the values in the field named "M_ID" in each table to be set to a constant, for example "1"
how can I do that using VBA?
You can loop through each table and run an update statement with docmd.ExecuteSQL. Like this:
Option Explicit
Option Compare Database
Sub ClearAllFields()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fdf As DAO.Field
Set db = CurrentDb
DoCmd.SetWarnings False
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
For Each fdf In tdf.Fields
If fdf.Name = "M_ID" Then DoCmd.RunSQL "update " + tdf.Name + " set M_ID = 1"
Next
End If
Next
Set tdf = Nothing
Set db = Nothing
DoCmd.SetWarnings True
End Sub

Access VBA: New tables created in a loop causing error

I'm grabbing field row from one table and creating a new table for each row. The new tables will have names equal to the row they correspond to.
Here is my code:
Option Compare Database
Public Function createTables()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
strSQL = "Select SKUS from SKUS"
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
Set fld = rst.Fields("SKUS")
'MsgBox fld.Value
rst.MoveFirst
Do While Not rst.EOF
Set tdf = db.CreateTableDef(fld.Value)
Set fld = tdf.CreateField("SKUS", dbText, 30)
tdf.Fields.Append fld
Set fld = tdf.CreateField("Count", dbInteger)
tdf.Fields.Append fld
db.TableDefs.Append tdf
rst.MoveNext
Loop
End Function
The problem is that after the first iteration of the code (the first table is created), it gives me an error "Invalid operation" pointing to the line
...
Set tdf = db.CreateTableDef(fld.Value)
...
Why do you think this is? I have a feeling its because I need to re set fld or rst, but I'm not sure.
Can anyone help me out with this?
Thanks!
It doesn't seem like you're reading any new tuples from rst, thus the CreateTableDef will be called with the same value repeatedly. Try changing this:
[...]
Set fld = rst.Fields("SKUS")
rst.MoveFirst
Do While Not rst.EOF
Set tdf = db.CreateTableDef(fld.Value)
[...]
Into this:
[...]
rst.MoveFirst
Do While Not rst.EOF
Set fld = rst.Fields("SKUS")
Set tdf = db.CreateTableDef(fld.Value)
[...]
...if your intention is to create one table based on every tuple in the SKUS table.