Access 2016创建包含组合框

时间:2017-04-18 15:50:39

标签: ms-access access-vba

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

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

表本身需要设置组合框,因为它最终会上传到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

1 个答案:

答案 0 :(得分: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开发来说确实是必须的。

相关问题