使用VBA设置启动显示形式

发布于 2024-12-20 00:40:27 字数 116 浏览 5 评论 0原文

是否可以使用VBA更改启动显示形式?使用“访问选项”页面很容易做到这一点,但我试图从 VBA 中执行此操作,但似乎没有任何效果。我到处搜索寻找允许这样做的数据库属性或设置。

有人对这个问题有任何见解吗?

Is it possible to change the Startup Display Form using VBA? It is very easy to do using the Access Options page, but I am trying to do this from within VBA and nothing seems to work. I've searched all over looking for Database properties or settings that would allow for this.

Does anyone have any insight into this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×眷恋的温暖 2024-12-27 00:40:27

您可以通过 将内置数据库属性 StartUpForm 设置为表单的名称。

要从启动中删除此表单,您只需删除该属性即可。

Private Sub debug_properties()

    ListProperties

    SetProperty "StartupForm", 10, "Form_Name"    'UPDATE TO YOUR FORM NAME
'    DeleteProperty "StartupForm"
    
'    ListProperties
    
End Sub

Public Function SetProperty(ByVal propName As String, ByVal propType As Long, propValue As Variant)

''SetProperty will create a property if it doesn't exist
    
    On Error GoTo SetProperty_Err
    
    Dim dbs As DAO.Database         ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = CurrentDb
    
    Dim prps As DAO.Properties      ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set prps = dbs.Properties
    
    ''attempt to set property
    prps(propName) = propValue
    
    
SetProperty_Exit:
    On Error Resume Next
    
    On Error GoTo 0
    Exit Function
    
SetProperty_Err:
    Select Case Err.Number
        Case 3270   ''The property was not found
            ''create property
            Dim prp As DAO.Property
            Set prp = dbs.CreateProperty(Name:=propName, _
                                         Type:=propType, _
                                         Value:=propValue)
            ''add new property to collection
            dbs.Properties.Append prp
        Case Else
            MsgBox "SetProperty, Error " & Err.Number & ": " & Err.Description
    End Select
    
    Resume SetProperty_Exit
    
End Function

Private Function DeleteProperty(ByVal propName As String)

    On Error GoTo DeleteProperty_Err
    
    Dim dbs As DAO.Database         ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = CurrentDb             
    
    dbs.Properties.Delete propName
    
    
DeleteProperty_Exit:
    On Error Resume Next
    
    On Error GoTo 0
    Exit Function
    
DeleteProperty_Err:
    Select Case Err.Number
        Case 3265   ''The property was not found
            MsgBox "Property '" & propName & "' does not exist."
        Case Else
            MsgBox "DeleteProperty, Error " & Err.Number & ": " & Err.Description
    End Select
    
    Resume DeleteProperty_Exit
    
End Function

Private Sub ListProperties()

''Lists DB properties created in code (as well as built-in properties)

    Dim dbs As DAO.Database             ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = Application.CurrentDb
    
    Debug.Print vbCrLf & Format(Now, "hh:mm:ss") & " ======="
    
    Debug.Print """" & Mid(CurrentDb.Name, _
                            InStrRev(CurrentDb.Name, "\") + 1, _
                            InStr(CurrentDb.Name, ".accdb") - InStrRev(CurrentDb.Name, "\") - 1) & _
                """ Properties: "
    
    On Error Resume Next    'skips the 'connection' property which has no 'value' for some reason
    
    Dim prp As DAO.Property
    For Each prp In dbs.Properties
        Debug.Print prp.Name & ": " & prp.Value
    Next prp
    
    On Error GoTo 0
    
End Sub

You can set a form to automatically open up in access VBA by setting the built-in database property StartUpForm to the name of the form.

To remove this form from startup you can simply delete that property.

Private Sub debug_properties()

    ListProperties

    SetProperty "StartupForm", 10, "Form_Name"    'UPDATE TO YOUR FORM NAME
'    DeleteProperty "StartupForm"
    
'    ListProperties
    
End Sub

Public Function SetProperty(ByVal propName As String, ByVal propType As Long, propValue As Variant)

''SetProperty will create a property if it doesn't exist
    
    On Error GoTo SetProperty_Err
    
    Dim dbs As DAO.Database         ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = CurrentDb
    
    Dim prps As DAO.Properties      ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set prps = dbs.Properties
    
    ''attempt to set property
    prps(propName) = propValue
    
    
SetProperty_Exit:
    On Error Resume Next
    
    On Error GoTo 0
    Exit Function
    
SetProperty_Err:
    Select Case Err.Number
        Case 3270   ''The property was not found
            ''create property
            Dim prp As DAO.Property
            Set prp = dbs.CreateProperty(Name:=propName, _
                                         Type:=propType, _
                                         Value:=propValue)
            ''add new property to collection
            dbs.Properties.Append prp
        Case Else
            MsgBox "SetProperty, Error " & Err.Number & ": " & Err.Description
    End Select
    
    Resume SetProperty_Exit
    
End Function

Private Function DeleteProperty(ByVal propName As String)

    On Error GoTo DeleteProperty_Err
    
    Dim dbs As DAO.Database         ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = CurrentDb             
    
    dbs.Properties.Delete propName
    
    
DeleteProperty_Exit:
    On Error Resume Next
    
    On Error GoTo 0
    Exit Function
    
DeleteProperty_Err:
    Select Case Err.Number
        Case 3265   ''The property was not found
            MsgBox "Property '" & propName & "' does not exist."
        Case Else
            MsgBox "DeleteProperty, Error " & Err.Number & ": " & Err.Description
    End Select
    
    Resume DeleteProperty_Exit
    
End Function

Private Sub ListProperties()

''Lists DB properties created in code (as well as built-in properties)

    Dim dbs As DAO.Database             ''Reference: Microsoft Office 16.0 Access database engine Object Library    'ACEDAO.DLL    'Access.References.AddFromGuid "{4AC9E1DA-5BAD-4AC7-86E3-24F4CDCECA28}", 0, 0
    Set dbs = Application.CurrentDb
    
    Debug.Print vbCrLf & Format(Now, "hh:mm:ss") & " ======="
    
    Debug.Print """" & Mid(CurrentDb.Name, _
                            InStrRev(CurrentDb.Name, "\") + 1, _
                            InStr(CurrentDb.Name, ".accdb") - InStrRev(CurrentDb.Name, "\") - 1) & _
                """ Properties: "
    
    On Error Resume Next    'skips the 'connection' property which has no 'value' for some reason
    
    Dim prp As DAO.Property
    For Each prp In dbs.Properties
        Debug.Print prp.Name & ": " & prp.Value
    Next prp
    
    On Error GoTo 0
    
End Sub
霞映澄塘 2024-12-27 00:40:27

我不知道如何直接更改启动表单属性,但您可以尝试创建一个可以加载所需启动表单的表单。通过这种方式,您可以将要加载的表单存储在配置表中并通过 VBA 更改它。

缺点是实际上启动了两种形式,而不是一种。

I don't know about directly changing the startup form property but what you could try is creating a form that can load your desired startup form. This way you can for instance store the form to be loaded in a config table and change it via VBA.

Downside is that effectively two forms are started and not one.

拥醉 2024-12-27 00:40:27

看一下 autoexec 宏。当您的数据库启动时,它会为您运行代码。使用它来加载表单。

Have a look at the autoexec macro. It'll run code for you when your database is launched. Use this to load a form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文