簡體   English   中英

MS 訪問 VBA Field.Properties.Append 方法失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM