[英]MS access VBA Field.Properties.Append method fails
我有一个创建表格的表格。 除一点外,一切正常。
我希望创建的表的一个字段显示为 combobox,因此我必须将其DisplayControl
属性更改为acComboBox
。
据我所知,财产首先必须存在。 如果没有,那么你必须创建它,然后将它 append 添加到集合中。
问题是,当涉及到 append 属性时,它会抛出Run-time error '3219': Invalid operation.
.
这是此时的代码:
Private Sub bInsert_Click()
Dim accApp As Access.Application
Dim DB As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim indx As DAO.Index
Dim rst As DAO.Recordset
Dim i As Integer, iFields As Integer
Dim sForm As String, str As String
Dim frm As Access.Form
Dim sCtrl() As String
If Not Application.IsCompiled Then _
Application.RunCommand acCmdCompileAndSaveAllModules
'there is a subform for the fields:
Set rst = Me.subfFields.Form.Recordset
rst.MoveFirst
'completion check:
If IsNull(Me.tName) Then
MsgBox "Insert table name."
Exit Sub
ElseIf rst.AbsolutePosition = -1 Then
MsgBox "Insert at least one field."
Exit Sub
End If
'create a db that will use later:
If Dir(Me.tDB) = "" Then
Set accApp = New Access.Application
accApp.NewCurrentDatabase Me.tDB
accApp.Quit
Set accApp = Nothing
End If
'create Table:
Set DB = Application.CurrentDb
Set tbl = DB.CreateTableDef(Me.tName)
'ID as PK:
Set fld = tbl.CreateField("ID", dbLong)
fld.Attributes = dbAutoIncrField
tbl.Fields.Append fld
Set indx = tbl.CreateIndex("IDindex")
indx.Primary = True
Set fld = indx.CreateField("ID")
indx.Fields.Append fld
tbl.Indexes.Append indx
Set indx = Nothing
Set fld = Nothing
'add rest of the fields:
Do Until rst.EOF
i = Me.subfFields.Form!cType
If i = dbText Then
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i, Nz(Me.subfFields.Form!tSize, 255))
Else
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i)
End If
tbl.Fields.Append fld
If Me.subfFields.Form!cControl = 111 Then
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
End If
rst.MoveNext
Loop
End Sub
Sub SetDAOProperty(WhichObject As Field, PropertyName As String, PropertyType As Integer, PropertyValue As Variant)
Dim prp As DAO.Property
On Error GoTo ErrorHandler
WhichObject.Properties(PropertyName) = PropertyValue
WhichObject.Properties.Refresh
Cleanup:
Set prp = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3270 ' "Property not found"
Set prp = WhichObject.CreateProperty(PropertyName, PropertyType, PropertyValue)
'=====================================
'the next line throws the error:
'=====================================
WhichObject.Properties.Append prp
WhichObject.Properties.Refresh
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume Cleanup
End Sub
有人可以解释一下是什么问题吗? 好像我错过了什么。 是否存在某种语法错误? 我的母语不是英语。
因此,正如 June7 所建议的那样,首先附加表格然后修改字段的属性,效果很好。
这是最终代码,以防有人需要它:
'create Table:
Set DB = Application.CurrentDb
Set tbl = DB.CreateTableDef(Me.tName)
'ID as PK:
Set fld = tbl.CreateField("ID", dbLong)
fld.Attributes = dbAutoIncrField
tbl.Fields.Append fld
Set indx = tbl.CreateIndex("IDindex")
indx.Primary = True
Set fld = indx.CreateField("ID")
indx.Fields.Append fld
tbl.Indexes.Append indx
Set indx = Nothing
Set fld = Nothing
'add rest of the fields:
Do Until rst.EOF
i = Me.subfFields.Form!cType
If i = dbText Then
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i, Nz(Me.subfFields.Form!tSize, 255))
Else
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i)
End If
tbl.Fields.Append fld
If Me.subfFields.Form!cControl = 111 Then
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
End If
rst.MoveNext
Loop
'append table:
DB.TableDefs.Append tbl
'format comboboxes:
rst.MoveFirst
Do Until rst.EOF
If Me.subfFields.Form!cControl = 111 Then
Set fld = tbl.Fields(Me.subfFields.Form!tName)
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
SetDAOProperty fld, "RowSourceType", dbText, "Value List"
SetDAOProperty fld, "RowSource", dbText, "Test1;Test2"
SetDAOProperty fld, "ColumnCount", dbInteger, 2
SetDAOProperty fld, "ColumnWidths", dbText, "0;1"
SetDAOProperty fld, "ListRows", dbInteger, 4
SetDAOProperty fld, "LimitToList", dbBoolean, -1
SetDAOProperty fld, "AllowValueListEdits", dbBoolean, 0
SetDAOProperty fld, "ShowOnlyRowSourceValues", dbBoolean, -1
End If
rst.MoveNext
Loop
此答案可能与此类似,但不是重复的。 目标相似,但面临的问题(错误)不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.