【问题标题】:Access 2016 create table with field properties for comboboxAccess 2016 使用组合框的字段属性创建表
【发布时间】:2017-04-18 15:50:38
【问题描述】:

我正在尝试研究如何在 Access 2016 中使用创建表子来添加一个将控件属性设置为组合框的字段。

使用我从各种来源收集的代码,除了创建一个组合框之外,我已经设法运行以下代码。

表格本身需要设置组合框,因为它最终会上传到 SharePoint。

请帮忙?

Sub maketable()
Dim db As DAO.Database
 Dim myTable As DAO.TableDef
 Dim myField As DAO.Field

     Set db = CurrentDb

     Set myTable = db.CreateTableDef("TestTable")
         With myTable
         .Fields.Append .CreateField("DateD", dbDate)
         .Fields.Append .CreateField("Description", dbText)
         .Fields.Append .CreateField("Num1", dbDouble)
         .Fields.Append .CreateField("Num2", dbDouble)
         .Fields.Append .CreateField("yesno", dbBoolean)
         .Fields.Append .CreateField("listme", dbText)
     End With

     db.TableDefs.Append myTable

    Set myField = myTable.Fields("DateD")
     Call SetDAOProperty(myField, "Format", dbText, "Short Date")

    Set myField = myTable.Fields("Num1")
     Call SetDAOProperty(myField, "DecimalPlaces", dbByte, 2)
     Call SetDAOProperty(myField, "Format", dbText, "Standard")

     Set myField = myTable.Fields("listme")
     Call SetDAOProperty(myField, "DisplayControl", dbText, acComboBox)
     Call SetDAOProperty(myField, "RowSourceType", dbText, acvaluelist)
     Call SetDAOProperty(myField, "RowSource", dbText, "Test1;Test2")

    Application.RefreshDatabaseWindow

     Set myField = Nothing
     Set myTable = Nothing
     Set db = Nothing

End Sub

Function SetDAOProperty( _
     WhichObject As Object, _
     PropertyName As String, _
     PropertyType As Integer, _
     PropertyValue As Variant _
 ) As Boolean
 On Error GoTo ErrorHandler

Dim prp As DAO.Property

    WhichObject.Properties(PropertyName) = PropertyValue
     WhichObject.Properties.Refresh
     SetDAOProperty = True

Cleanup:
     Set prp = Nothing
     Exit Function

ErrorHandler:
     Select Case Err.Number
         Case 3270 ' "Property not found"
             Set prp = WhichObject.CreateProperty( _
                 PropertyName, _
                 PropertyType, _
                 PropertyValue _
             )
             WhichObject.Properties.Append prp
             WhichObject.Properties.Refresh
             SetDAOProperty = True
         Case Else
             MsgBox Err.Number & ": " & Err.Description
             SetDAOProperty = False
     End Select
     Resume Cleanup

 End Function

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您就快到了,只需要进行两项更改:

    1.

    Call SetDAOProperty(myField, "DisplayControl", dbText, acComboBox)
    

    DisplayControl 不是文本而是整数属性:

    Call SetDAOProperty(myField, "DisplayControl", dbInteger, acComboBox)
    

    2.

    这里VBA编辑器已经给出了有问题的提示:

    Call SetDAOProperty(myField, "RowSourceType", dbText, acvaluelist)
    

    acvaluelist 不存在。 RowSourceType 是文本属性,correct assignment 是:

    Call SetDAOProperty(myField, "RowSourceType", dbText, "Value List")
    

    注意:第二个会被捡起 Option Explicit 在每个模块的顶部。 它强制执行变量声明并在编译时报告未声明或拼写错误的变量/常量。

    要在新模块中自动启用此功能,请在 VBA 编辑器中设置 Require Variable Declaration 选项。 这确实是 VBA 开发的必备。

    【讨论】:

    • 安德烈,你是我的英雄,效果很好,谢谢!
    • 如果答案解决了你的问题,你可以accept它,这也标志着问题已解决。 @JimTavolaro
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多