【问题标题】:My userform turns blank because I can't edit linked table connection我的用户表单变为空白,因为我无法编辑链接表连接
【发布时间】:2016-11-03 18:23:15
【问题描述】:

我目前正在开发一个访问应用程序,它通过 VBA 模块动态构建用户表单。为了创建表单,我需要来自 MS SQL DB 的数据。我通过链接表连接收集这些数据,这些链接是在整个应用程序启动时使用 AttachDSNlessTable 方法建立的:

https://support.microsoft.com/en-us/kb/892490

用户输入被收集在用户表单中,随后插入到同一个 sql-server 上的一些其他链接表 (:ResultTables) 中。

我的问题是基于 VB 的链接表连接不允许我将用户表单输入插入结果表。当我在 FormView 中查看表单时,它完全是空白的。尽管如此,所有控件在 DesignView 中都是可见的。

当我手动创建链接表时,问题不存在,显然是因为我可以选择一个索引,然后我可以在表中填充新行。

我完全确定用户有权访问 sql-server 以执行更新/插入/删除过程。我试图在服务器上索引表,但索引不是 Access 中固有的。链接表格后,我无法编辑连接。我还尝试了针对空白用户表单建议的方法:
https://support.microsoft.com/en-us/kb/93261

Option Compare Database

'//Name     :   AttachDSNLessTable
'//Purpose  :   Create a linked table to SQL Server without using a DSN
'//Parameters
'//     stLocalTableName: Name of the table that you are creating in the current database
'//     stRemoteTableName: Name of the table that you are linking to on the SQL Server database
'//     stServer: Name of the SQL Server that you are linking to
'//     stDatabase: Name of the SQL Server database that you are linking to
'//     stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
'//     stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
    On Error GoTo AttachDSNLessTable_Err
    Dim td As TableDef
    Dim stConnect As String

    For Each td In CurrentDb.TableDefs
        If td.Name = stLocalTableName Then
            CurrentDb.TableDefs.Delete stLocalTableName
        End If
    Next

    If Len(stUsername) = 0 Then
        '//Use trusted authentication if stUsername is not supplied.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
    Else
        '//WARNING: This will save the username and the password with the linked table information.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
    End If
    Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
    CurrentDb.TableDefs.Append td
    CurrentDb.TableDefs.Refresh
    'td.CreateIndex (ID2)
    AttachDSNLessTable = True
    Exit Function

AttachDSNLessTable_Err:

    AttachDSNLessTable = False
    MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description

End Function

【问题讨论】:

    标签: sql-server ms-access vba userform linked-tables


    【解决方案1】:

    链接 SQL Server 表应自动检测主键。它适用于我使用DoCmd.TransferDatabase TransferType:=acLink

    显然它不适用于CurrentDb.CreateTableDef 方法。

    所以我建议使用这个命令来链接表格:

    DoCmd.TransferDatabase TransferType:=acLink, _
        DatabaseType:="ODBC", _
        DatabaseName:=stConnect, _
        ObjectType:=acTable, _
        Source:=stRemoteTableName, _
        Destination:=stLocalTableName, _
        StructureOnly:=False, _
        StoreLogin:=True
    

    我已经写了更多关于创建无 DSN 链接表 here 的文章,但其中大部分内容涉及链接 SQL Server 视图,其中不会自动检测到 PK。

    【讨论】:

      【解决方案2】:

      感谢您的回复!最后,我为主键创建了一个 create-sql-statement。它运行顺利:

      SQL_PrimaryKey = "CREATE INDEX PrimaryKey ON " & QMT_QMTmonitoring_TransactionFact & _
                       " (ID) WITH PRIMARY" 
      Database_Link.Execute SQL_PrimaryKey
      

      【讨论】:

        猜你喜欢
        • 2019-01-06
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多