将链接表添加到 Access 2003,同时在 MDB 中保留 ODBC 连接信息

发布于 2024-08-30 02:41:13 字数 390 浏览 3 评论 0原文

我有一个 Access 2003 数据库 MDB,其中所有表都作为 SQL Server 2005 中的链接表存在。MDB 文件包含指向正确 SQL Server 和登录凭据(受信任连接)的所有 ODBC 信息。

我想要做的是将新链接表添加到 MDB 文件,但我不确定如何指定 ODBC 连接信息。当我尝试添加新的链接表时,不断提示我查找或创建 DSN 文件。我不想在每台计算机上创建新的 DSN 条目,而是希望将所有信息存储在 Access MDB 文件本身中。

在现有数据库中,我可以将“鼠标悬停”在表名称上,并以工具提示形式查看 ODBC 连接信息。我需要做的就是使用相同的连接信息添加另一个链接表。

我确实可以访问表链接到的 SQL Server。我已经创建了我想要添加的新表。我只需要找到一种链接到它的方法。

I have an Access 2003 database MDB where all of the tables exist as linked tables within SQL Server 2005. The MDB file contains all of the ODBC information that points to the correct SQL Server and log-on credentials (trusted connection).

What I would like to do is add a new linked table to the MDB file however I am not sure how to go about specifying the ODBC connection information. When I try to add a new linked table I keep getting prompted to locate or create a DSN file. I don't want to have to create a new DSN entry on every machine, rather I would like all that information stored within the Access MDB file itself.

In the existing database I can "hover" over the table names and see the ODBC connection info as a tool-tip. All I need to do is add another linked table using the same connection information.

I do have access to the SQL Server where the tables are linked to,. I have already created the new table I wanted to add. I just need to find a way to link to it.

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

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

发布评论

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

评论(3

懷念過去 2024-09-06 02:41:13

就其价值而言,我很懒。我在我的开发机器上保留了一个 DSN,并用它来创建新的链接表。然后我运行 Doug Steele 的代码将链接转换为 dsnless 连接,然后再分发前端到最终用户。

For what it's worth, I'm lazy. I keep a DSN on my development machine, and use it to create new linked tables. I then run Doug Steele's code to convert the links to dsnless connections before distribution of the front end to the end users.

梦巷 2024-09-06 02:41:13

您可以使用现有表中的连接字符串,也可以执行以下操作:

''This is a basic connection string, you may need to consider password and so forth
cn = "ODBC;DSN=TheDSNName;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=TheDatabaseName;"

有一些方法用于连接表:

sLocalName = "TABLE_SCHEMA" & "_" & "TABLE_NAME"

With CurrentDb
    If DLookup("Name", "MSysObjects", "Name='" & sLocalName & "'") <> vbNullString Then
        If .TableDefs(sLocalName).Connect <> cn Then
            .TableDefs(sLocalName).Connect = cn
            .TableDefs(sLocalName).RefreshLink
        End If
    Else
        ''If the table does not have a unique index, you will neded to create one
        ''if you wish to update.
        Set tdf = .CreateTableDef(sLocalName)
        tdf.Connect = cn
        tdf.SourceTableName = "TABLE_NAME"
        .TableDefs.Append tdf
        .TableDefs.Refresh
    End If
End With

如果表没有唯一索引,这将产生一个消息框

  DoCmd.TransferDatabase acLink, "ODBC Database", cn, acTable, "TABLE_NAME", sLocalName

You can use the connection string from an existing table, or you can do something like:

''This is a basic connection string, you may need to consider password and so forth
cn = "ODBC;DSN=TheDSNName;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=TheDatabaseName;"

There are a few was to connect a table:

sLocalName = "TABLE_SCHEMA" & "_" & "TABLE_NAME"

With CurrentDb
    If DLookup("Name", "MSysObjects", "Name='" & sLocalName & "'") <> vbNullString Then
        If .TableDefs(sLocalName).Connect <> cn Then
            .TableDefs(sLocalName).Connect = cn
            .TableDefs(sLocalName).RefreshLink
        End If
    Else
        ''If the table does not have a unique index, you will neded to create one
        ''if you wish to update.
        Set tdf = .CreateTableDef(sLocalName)
        tdf.Connect = cn
        tdf.SourceTableName = "TABLE_NAME"
        .TableDefs.Append tdf
        .TableDefs.Refresh
    End If
End With

This will produce a message box if the table does not have a unique index

  DoCmd.TransferDatabase acLink, "ODBC Database", cn, acTable, "TABLE_NAME", sLocalName
老娘不死你永远是小三 2024-09-06 02:41:13

我能够成功添加表格,并想在此处详细说明步骤。

  1. 我通过单击 Access 中的“新建”添加了新表,并选择“链接表”
  2. 当出现“链接文件”对话框提示时,我从文件类型列表框中选择了 ODBC
  3. 我创建了一个新的 DSN 项(仅用于初始链接到表)
  4. 继续创建 DSN 后,您将遵循链接到 SQL Server 中创建的表的过程,
  5. 表应该显示在 Access 中,

然后我在代码模块中创建了以下子例程。它实质上循环访问 Access 中具有 ODBC 连接的所有表,并将正确的 ODBC 连接信息设置到表定义中。您可以删除之前创建的 DSN,因为不再需要它。

Public Sub RefreshODBCLinks()

    Dim connString As String
    connString = "ODBC;DRIVER=SQL Server Native Client 10.0;" & _
             "SERVER=<SQL SERVER NAME>;UID=<USER NAME>;" & _
             "Trusted_Connection=Yes;" & _
             "APP=<APP NAME>;DATABASE=<DATABASE NAME>"

    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Set db = CurrentDb
    For Each tb In db.TableDefs
    If Left(tb.Connect, 4) = "ODBC" Then
        tb.Connect = connString
        tb.RefreshLink
        Debug.Print "Refreshed ODBC table " & tb.Name
    End If
    Next tb
    Set db = Nothing

End Sub

注意...为了执行上述子例程,我只需将其名称输入到访问代码模块内的“立即”窗口中。您还可以创建一个执行此例程的宏,然后每当您创建新表时都可以运行该宏。

感谢“Remou”的解答和帮助!

PS 如果您对连接字符串中的 APP= 及其用途感兴趣,请查看本文。 http://johnnycoder.com/blog/2006 /10/24/利用应用程序名称/

I was able to add the table successfully and wanted to detail the steps here.

  1. I added the new table by clicking "New" within Access and chose "Link Table"
  2. When prompted with the Link file dialog I chose ODBC from the file type list box
  3. I created a new DSN item (only used for initial linking to table)
  4. Proceed with creating DSN and you will follow process of linking to the table created in SQL Server
  5. Table should show up in Access

I then created the following sub routine in the code module. It essentially loops through all your tables in Access that have ODBC connections and sets the proper ODBC connection info into the Table definitions. You can delete the DSN you created previously as it is no longer needed.

Public Sub RefreshODBCLinks()

    Dim connString As String
    connString = "ODBC;DRIVER=SQL Server Native Client 10.0;" & _
             "SERVER=<SQL SERVER NAME>;UID=<USER NAME>;" & _
             "Trusted_Connection=Yes;" & _
             "APP=<APP NAME>;DATABASE=<DATABASE NAME>"

    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Set db = CurrentDb
    For Each tb In db.TableDefs
    If Left(tb.Connect, 4) = "ODBC" Then
        tb.Connect = connString
        tb.RefreshLink
        Debug.Print "Refreshed ODBC table " & tb.Name
    End If
    Next tb
    Set db = Nothing

End Sub

NOTE ... To execute the above SubRoutine I just typed in it's name into the "immediate" windows within the Access code module. You could also create a macro that executes this routine and then whenever you create a new table you could just run the macro.

Thanks to "Remou" for his answer and assistance!

P.S. If you are interested in APP=<APP NAME> in the connection string and what it is for check out this article. http://johnnycoder.com/blog/2006/10/24/take-advantage-of-application-name/

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