ACCESS VBA adding fields Error 3219 - ms-access

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

Related

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

Delete all null rows - Access VBA

basic question.
I need to delete all null/blank rows of a table in my database. Trouble is there is no way to know how many fields there are, or the names of the fields before the delete. And I need to verify every field is null/blank before deleting. Not sure how to query for this in Access VBA.
In all the examples I find, they have a field name they can test for blanks.
Thanks in advance.
Change TestTabke to your table name. If you have an AutoNumber field, it must be skipped. I am using DAO. If you want ADO, convert the following code.
Function DeleteEmptyRows()
Dim db As DAO.database
Set db = CurrentDb
Dim rs As DAO.recordSet
Set rs = db.OpenRecordset("TestTable")
Do Until rs.EOF
For inx = 0 To rs.Fields.Count - 1
If IsNull(rs.Fields(inx).Value) Or Len(Trim(rs.Fields(inx).Value)) = 0 Then
Else: Exit For
End If
Next
If rs.Fields.Count = inx Then
rs.Delete
End If
rs.MoveNext
Loop
End Function

When creating new table and field in vba Access 2000. Getting Error Data Type Conversion Error

I am trying to create a new table with a few field on vba for Access 2000. I am able to create the table and a field when the data type of the field is Text But i need it to be Number. when I am changing the data type from Text to Number it gives me the error Data Type Conversion Error. Any Help would be Appreciated. Thanks
Public Sub CreateNewDB()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
'Initialize the Contractor table
Set db = CurrentDb()
Set tdf = db.CreateTableDef("new test table")
'Specify the fields
With tdf
'Create Sequence No Fied
' Set fld = .CreateField("Sequence No", dbText, 10) ' This works
Set fld = .CreateField("Sequence No", dbNumber, Long INteger) ' this gives me an error
.Fields.Append fld
End With
'save the table
db.TableDefs.Append tdf
Set fld = Nothing
Set tdf = Nothing
End Sub
try dbLong instead of dbNumber. Reference
Here's code to set the PK on a newly created table:
Set td = CurrentDb.CreateTableDef(sLocalTableName, dbAttachSavePWD, sRemoteTableName, sConnect)
CurrentDb.TableDefs.Append td
sExec = "CREATE UNIQUE INDEX PK ON [" & sLocalTableName & "]"
For iPK = 0 To iPkfldCount - 1
sPKfields = sPKfields & "[" & CurrentDb.TableDefs(sLocalTableName).Fields(iPK).Name & "],"
Next
sPKfields = Left(sPKfields, Len(sPKfields) - 1) ' strip trailing comma
CurrentDb.Execute sExec & " (" & sPKfields & ")"
after you set the PK fields, you shouldn't need to index or set to No Duplicates. All PKs work that way.

Access VBA unable to create table

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)

MS Access 2003 - VBA for altering a table after a "SELECT * INTO tblTemp FROM tblMain" statement

I use functions like the following to make temporary tables out of crosstabs queries.
Function SQL_Tester()
Dim sql As String
If DCount("*", "MSysObjects", "[Name]='tblTemp'") Then
DoCmd.DeleteObject acTable, "tblTemp"
End If
sql = "SELECT * INTO tblTemp from TblMain;"
Debug.Print (sql)
Set db = CurrentDb
db.Execute (sql)
End Function
I do this so that I can then use more vba to take the temporary table to excel, use some of excel functionality (formulas and such) and then return the values to the original table (tblMain). Simple spot i am getting tripped up is that after the Select INTO statement I need to add a brand new additional column to that temporary table and I do not know how to do this:
sql = "Create Table..."
is like the only way i know how to do this and of course this doesn't work to well with the above approach because I can't create a table that has already been created after the fact, and I cannot create it before because the SELECT INTO statement approach will return a "table already exists" message.
Any help? thanks guys!
I can think of the following ways you can achieve this
1. Create, then insert
You can do a CREATE TABLE tblTemp with all the columns you need. Of course, you will have more columns than TblMain contains, so your insert will contain column definitions.
INSERT INTO tblTemp (Col1, Col2, Col3) SELECT Col1, Col2, Col3 from TblMain
2. Insert Into, then add column
You can do your insert into, then add columns using multiple ways
In VBA, use the TableDef object to point to tblTemp and then add a column to it
Execute DoCmd.RunSQL "ALTER TABLE tblTemp Add Column MyNewColumn (OTTOMH)
There always more than one way to skin a feline. You could use DAO? This has the advantage of being able to set the various properties of the newly created field that get when creating new field via the user interface within Access. Just an idea :-)
This the sub l created and tested, in Access 2007 should be compatable with any version though.
Public Sub AddField()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strTable As String
Set dbs = CurrentDb
strTable = "tblTemp"
'create the TableDef object that
'wish to change
Set tdf = dbs.TableDefs(strTable)
'create new field
Set fld = tdf.CreateField("Name", dbText)
'set property of new created field
fld.AllowZeroLength = True
'append the new field to the tabledef
tdf.Fields.Append fld
End Sub