Tuesday, March 27, 2012

Access Database Datatypes, ADOX and VS2005 Question

I'm using ADOX 2.8 for table creation: The following is an example of a column defintion:

If CreateNewTable Then CreateNewTable = a.CreateColumn("ReferenceCount", ADOX.DataTypeEnum.adInteger)

If CreateNewTable Then CreateNewTable = a.CreateColumn("Document", ADOX.DataTypeEnum.adLongVarBinary) 'Oleobject

If CreateNewTable Then CreateNewTable = a.CreateColumn("EntityID", ADOX.DataTypeEnum.adWChar, 18) 'text

Where CreateColumn looks like this:

Public Function CreateColumn(ByVal ColumnName As String, ByVal Datatype As ADOX.DataTypeEnum, Optional ByVal Size As Integer = 0) As Boolean

'ADOX.CreateColumn- Called by Common.CreateNewTable

'CreateColumn creates a column described in the Table object so it assumes it is set.

'One method of setting it is to call Select Table after opening the database

If Not Me.ConnectionIsOpen Then

MsgBox("CreateColumn - Failed to Create Column : " _

& ColumnName, MsgBoxStyle.Exclamation, cNoConn)

Return False

End If

Dim col As New ADOX.Column

col.Name = ColumnName

Try

col.Type = Datatype

Catch e As Exception

MsgBox("CreateColumb - Failed to Create Column : " _

& ColumnName, MsgBoxStyle.Exclamation, e.Message)

col = Nothing

Return False

End Try

If Size <> 0 Then col.DefinedSize = Size

Try

Table.Columns.Append(ColumnName, Datatype)

Catch e As Exception

If Err.Number() <> 0 Then

MsgBox(Err.Source & "-->" & Err.Description, , "Error")

End If

MsgBox("CreateColumb - Failed to Append Column : " _

& ColumnName, MsgBoxStyle.Exclamation, e.Message)

Return False

End Try

col = Nothing

Return True

End Function

in CreateColumn("EntityID", ADOX.DataTypeEnum.adWChar, 18)

the 18 specifies the field width in the database. Yet no matter whether I use adWChar or

adVarWChar, Access always shows the field size to be 255.

Does anyone know why or how to fix that?

I can see that you created col variable, and assigned values to the fields of it. But, unless I'm missing something, I do not see how it is used in Table.Columns.Append call.

I guess that you should either use col instead of ColumnName, or add Size as a third parameter, and do not use col at all.

No comments:

Post a Comment