How to create Access 2010 Calculated field using VBA ?

E

exebat

Is there a way to create a Calculated field in Access 2010 table using
VBA ?

I can create any type of field but cant find the way to create new
Calculated field type.
 
A

Arvin Meyer [MVP]

exebat said:
Is there a way to create a Calculated field in Access 2010 table using
VBA ?

I can create any type of field but cant find the way to create new
Calculated field type.

My 2010 machine is getting it's webcam repaired so I couldn't check this but
MVP Crystal Long wrote some sample code for you:

Sub CreateCalculatedField()
'Crystal 100421

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Dim sTblNm As String
Dim sFldNm As String
Dim i As Integer

i = 0

On Error GoTo Proc_Err

sTblNm = "People"
sFldNm = "FullName"

Set db = CurrentDb
Set tdf = db.TableDefs(sTblNm)
Set fld = tdf.CreateField(sFldNm)

fld.Properties("Expression") = _
"[NameLast] & ("" ""+[Sufx]) & ("", ""+[NameFirst]) & ("" ""+[MidNm])

tdf.Fields.Append fld

MsgBox "Done creating " & sFldNm _
& " in " & sTblNm

Proc_Exit:
On Error Resume Next
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
Exit Sub

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " CreateCalculatedField: " _
& "field: " & sFldNm & " table: " & sTblNm

Resume Proc_Exit
Resume

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top