Field Properties in VBA (Primary key, etc)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How do I set the following properties for a field from VBA (primary key,
Lookup, Row Source Type, Row Source,Limit to List)? I'm using an ADODB
Recordset.

I have an existing database with lots of tables and fields for which i need
to modify the properties. I want to use code to do the changes instead of
having to go through the gruelling task of manual modifications. I have tried
using currentdb.tabledef(i).field(i).property(i).value..., but I can't get
the properties mentioned above.
 
You cannot set these properties with ADO or ADOX.

They can only be set with the native Access library - DAO.

This example lets you pass in any object, such as:
Call SetPropertyDAO(CurrentDb().TableDefs("MyTable").Fields("MyField"), _
"DisplayControl", dbInteger, CInt(acComboBox))

--------------code starts--------------
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
--------------code ends--------------
 
Back
Top