Office中国论坛/Access中国论坛

标题: 用VB创建ACCESS字段怎样设置他的默认值呀? [打印本页]

作者: winxp    时间: 2002-10-28 18:23
标题: 用VB创建ACCESS字段怎样设置他的默认值呀?
欢迎解答,我都问了好长时间了。[em24][em24][em24][em24]
作者: winxp    时间: 2002-10-29 02:29
是没有办法了吗?
作者: winxp    时间: 2002-11-1 20:40
神呀救救我呀?
作者: zhuyiwen    时间: 2002-11-1 21:16
在VB引用DAO或ADOX,你想怎么设就怎么设。[em26]
作者: winxp    时间: 2002-11-4 22:06
您的意思是我先用VBA把数据库和表创出来,然后再用DAO将每个字段设为默认值。当我对其中的一些字段赋值后,那些没赋值的字段就为默认值?
作者: zhuyiwen    时间: 2002-11-4 23:42

下面是DAO3.60中帮助中的例程:
Sub DefaultValueX()

    Dim dbsNorthwind As Database
    Dim tdfEmployees As TableDef
    Dim strOldDefault As String
    Dim rstEmployees As Recordset
    Dim strMessage As String
    Dim strCode As String

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set tdfEmployees = dbsNorthwind.TableDefs!Employees

    ' Store original DefaultValue information and set the
    ' property to a new value.
    strOldDefault = _
        tdfEmployees.Fields!PostalCode.DefaultValue
    tdfEmployees.Fields!PostalCode.DefaultValue = "98052"

    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees", _
        dbOpenDynaset)

    With rstEmployees
        ' Add a new record to the Recordset.
        .AddNew
        !FirstName = "Bruce"
        !LastName = "Oberg"

        ' Get user input. If user enters something, the field
        ' will be filled with that data; otherwise, it will be
        ' filled with the DefaultValue information.
        strMessage = "Enter postal code for " & vbCr & _
            !FirstName & " " & !LastName & ":"
        strCode = DefaultPrompt(strMessage, !PostalCode)
        If strCode <> "" Then !PostalCode = strCode
        .Update

        ' Go to new record and print information.
        .Bookmark = .LastModified
        Debug.Print "  FirstName = " & !FirstName
        Debug.Print "  LastName = " & !LastName
        Debug.Print "  PostalCode = " & !PostalCode

        ' Delete new record because this is a demonstration.
        .Delete
        .Close
    End With

    ' Restore original DefaultValue property because this is a
    ' demonstration.
    tdfEmployees.Fields!PostalCode.DefaultValue = _
        strOldDefault

    dbsNorthwind.Close

End Sub

Function DefaultPrompt(strPrompt As String, _
    fldTemp As Field) As String

    Dim strFullPrompt As String

    ' Ask user for new DefaultValue setting for the specified
    ' Field object.
    strFullPrompt = strPrompt & vbCr & _
        "[Default = " & fldTemp.DefaultValue & _
        ", Cancel - use default]"
    DefaultPrompt = InputBox(strFullPrompt)

End Function


[em26]
[此贴子已经被zhuyiwen于2002-11-4 15:42:03编辑过]


作者: winxp    时间: 2002-11-7 02:29
斑竹你的DAO帮助到哪里可以下载呀?还有这么好的东东。
作者: 李啸林    时间: 2002-11-7 06:13
晕.就在ACCESS的帮助文件中。很容易找到,不过是英文的,但仔细阅读话,还是能看懂大多数意思.
作者: winxp    时间: 2002-11-7 22:21
晕,梦里寻她千百度,默然回首那物却在自己的机子里




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3