The Format property cannot be set with DDL, so it's back to DAO to set that.
Some of these properties don't exist until you create them, so here's what I
actually use to do this kind of thing. You could call it as:
Call SetPropertyDAO(dbEngine(0)(0).TableDefs("tblDailyNonAttendees
").Fields("Contact"), dbText, "True/False")
Function SetPropertyDAO(obj As Object, strPropertyName As String, _
intType As Integer, varValue As Variant, Optional strErrMsg As String) As
Boolean
On Error GoTo ErrHandler
'Purpose: Set a property for an object, creating if necessary.
'Arguments: obj = the object whose property should be set.
' strPropertyName = the name of the property to set.
' intType = the type of property (needed for creating)
' varValue = the value to set this property to.
' strErrMsg = string to append any error message to.
If HasProperty(obj, strPropertyName) Then
obj.Properties(strPropertyName) = varValue
Else
obj.Properties.Append obj.CreateProperty(strPropertyName, intType,
varValue)
End If
SetPropertyDAO = True
ExitHandler:
Exit Function
ErrHandler:
strErrMsg = strErrMsg & obj.Name & "." & strPropertyName & _
" not set to " & varValue & ". Error " & Err.Number & " - " &
Err.Description & vbCrLf
Resume ExitHandler
End Function
Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
in message
Thanks for the reply Allen and Brendan,
I am using the following and it does change the Contact Field to a Yes/No
Data Type and I was wondering if there was a way to specify that the
format
was set to TRUE/FALSE for the Yes/No field?
DBEngine(0)(0).Execute "ALTER TABLE tblDailyNonAttendees ALTER COLUMN
Contact YESNO;"
Thanks again.
Alastair MacFarlane
Allen Browne said:
Last time I tried, I was also unable to change the data type of a column
using DAO.
In JET 4 (Access 2000 and later), you can do it with DDL. Try:
dbEngine(0)(0).Execute "ALTER TABLE Table1 ALTER COLUMN [Type] YESNO;"
"Alastair MacFarlane" <
[email protected]>
wrote
in message
Dear All,
If I am trying to change the datatype of a table field from an Integer
to
a
Boolean (TRUE/FALSE) why does the code below not work?
Private Sub ChangeDataType()
Dim db As DAO.Database
Dim tb As DAO.TableDef
Dim fd As DAO.Field
Dim pp As DAO.Property
Set db = CurrentDb()
Set tb = db.TableDefs("Table1")
Set fd = tb.Fields("Field1")
Set pp = fd.Properties("Type")
pp.Value = dbBoolean
End Sub
I get the error on the line: pp = dbBoolean
?err.Number
3219
?err.Description
Invalid operation.
?err.Source
DAO.Field
Can anyone advise me how I can change the data type of a field object
at
runtime?
Thanks again.
Alastair MacFarlane