
Add field to linked table
This is not a problem. Alden gave you the rough strategy to use. Here
is some code that I grabbed from an old Access 2 database that did
what you want and even added an index too. I'd do it a bit differently
in 97(and it would look prettier), but not much. Watch for text
wrapping if you copy it. By the way, I think you could also use ALTER
TABLE with an In clause to identify the database.
Function AddFieldVer30 ()
On Error GoTo AddFieldVer30_Err
'Called from AutoExec macro on startup
'Upgrades pmdata.mdb from ver 2.x to 3.0 if necessary
'Adds AddresseeCode2 field to tblAddressees and adds index
Dim ws As WorkSpace, dbData As Database
Dim tdf As TableDef, fld As Field, idx As Index
Set ws = dbengine(0)
Set dbData = ws.OpenDatabase(abCurrDataPath("tblDefaults") &
"PMDATA.mdb")
Set tdf = dbData.tabledefs("tblAddressees")
On Error Resume Next
Set fld = tdf.fields("AddresseeCode2")
If Err = 0 Then GoTo AddFieldVer30_Exit
On Error GoTo 0
On Error GoTo AddFieldVer30_Err
If MsgBox("A new version of Postmaster has been installed on your
system. Postmaster will now update your data files to be compatible
with this new version of the software." & Chr$(13) & Chr$(10) &
Chr$(13) & Chr$(10) & "Your version of Postmaster will now allow a
second Code field to be filled in for each addressee.", 65, "Updating
to Add Addressee Code2 Field") = 2 Then
DoCmd Quit
GoTo AddFieldVer30_Exit
End If
Set fld = tdf.CreateField("AddresseeCode2", DB_TEXT, 30)
fld.AllowZeroLength = True
tdf.fields.Append fld
tdf.fields("AddresseeCode2").OrdinalPosition = 17
Set idx = tdf.CreateIndex("AddresseeCode2")
Set fld = idx.CreateField("AddresseeCode2")
idx.fields.Append fld
tdf.indexes.Append idx
Set tdf = dbData.tabledefs("tblArticles")
Set fld = tdf.CreateField("AddresseeCode2", DB_TEXT, 30)
fld.AllowZeroLength = True
tdf.fields.Append fld
tdf.fields("AddresseeCode2").OrdinalPosition = 16
MsgBox "Your PostMaster data has successfully been updated to the new
version.", , "Update Completed"
AddFieldVer30_Exit:
On Error Resume Next
dbData.Close
Exit Function
AddFieldVer30_Err:
Select Case Err
Case Else
MsgBox Err & " " & Error$, , "AddFieldVer30 Error"
End Select
Resume AddFieldVer30_Exit
End Function
Function abCurrDataPath (pstrTable As String) As String
On Error GoTo abCurrDataPath_Err
'Inputs:
' pstrTable -- name of an attached table in the current database
'Outputs:
' directory path for the data file
Dim db As Database, tdf As TableDef
Dim intI As Integer
Set db = CurrentDB()
Set tdf = db.tabledefs(pstrTable)
For intI = Len(tdf.connect) To 13 Step -1
If Mid(tdf.connect, intI, 1) = "\" Then Exit For
Next intI
abCurrDataPath = Mid$(tdf.connect, 11, intI - 10)
abCurrDataPath_Exit:
Exit Function
abCurrDataPath_Err:
Select Case Err
Case Else
MsgBox Err & " " & Error$, , "abCurrDataPath Error"
End Select
Resume abCurrDataPath_Exit
End Function
-- Andy
Quote:
>This seems easy, but I can't figure it out...trying to add a checkbox
>field to a linked table. I've tried the ALTER TABLE (but that doesn't
>work on linked tables), tried CREATEFIELD (again, not on linked tables).
>I have 40 cities using this database as a run-time Access 97 split
>database; I send them an updated program.mdb and it needs to change the
>table in their data.mdb. Is there an easy (or any) way to handle
>this???
>Any suggestions appreciated.
>Michelle Matthews