我的userform变为空白,因为我无法编辑链接表连接

时间:2016-07-01 11:00:41

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

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

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

userinputs收集在userforms中,随后插入到同一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

2 个答案:

答案 0 :(得分: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未被自动检测到。

答案 1 :(得分:0)

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

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