如何将 Access tabledef 链接到 SQLServer 中的临时表

发布于 2024-11-03 17:41:58 字数 2938 浏览 1 评论 0原文

我正在将 MS Access 前端/后端应用程序移植为 MS Access 前端/sql server 后端。

该应用程序的一部分(原始形式)使用VB将CSV文件导入到前端的表中,然后运行各种SQL语句将该表的内容合并到一些后端表中。

我认为在新环境中,因为在存储过程中执行合并过程更有效,所以我的策略应该是

  1. 打开到后端的连接
  2. 在该连接上创建一个临时表
  3. 在VB中创建一个tabledef并尝试将其连接到这个临时表
  4. 使用 DoCmd.TransferText (正如我在原始表中所做的那样)将 CSV 文件导入到由表 def 链接到的表中
  5. 在打开的连接上执行存储过程,将临时表数据合并到后端的永久表中。

我认为我已成功执行步骤 1 和 2,但我不确定。我可以在 SQL Server Management Studio 中看到临时表,但尝试打开列失败并出现错误(当我使用 Management Studio 创建临时表时,它也会失败并出现相同的错误,所以我认为这是 Management Studio 问题) 。

我使用与启动应用程序时链接所有永久表相同的代码来执行步骤 3,但这一点不起作用。最后,我通过在非临时表上运行步骤 5 来证明它是有效的。

我为此使用的代码如下,

    Set conn = New ADODB.Connection
    conn.Open getStrConn

    'First create a temporary table on the server for the web site leads
    SQL = "CREATE TABLE [dbo].[#WebSiteLeads]("
    SQL = SQL & "[leadID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED,[Title] [nvarchar](255) NULL,[Firstname] [nvarchar](50) NULL,"
    SQL = SQL & "[Lastname] [nvarchar](50) NULL,[Sex] [nvarchar](10) NULL,[House] [nvarchar](50) NULL,"
    SQL = SQL & "[Address1] [nvarchar](50) NULL,[Address2] [nvarchar](50) NULL,[Street] [nvarchar](50) NULL,"
    SQL = SQL & "[Town_City] [nvarchar](50) NULL,[County] [nvarchar](50) NULL,[Postcode] [nvarchar](10) NULL,"
    SQL = SQL & "[Email] [nvarchar](50) NULL,[Allow_email] [nvarchar](5) NULL,[Telephone] [nvarchar](20) NULL,"
    SQL = SQL & "[Allow_tel] [nvarchar](5) NULL,[Cons_period] [nvarchar](20) NULL,[When] [nvarchar](20) NULL,"
    SQL = SQL & "[Procedure] [nvarchar](20) NULL,[Consultation] [nvarchar](10) NULL,[Info_pack] [nvarchar](10) NULL,"
    SQL = SQL & "[Source] [nvarchar](20) NULL,[Further_info] [nvarchar](255) NULL,[Callback] [nvarchar](50) NULL,"
    SQL = SQL & "[Date_added] [nvarchar](30) NULL,[Date_added_dt] [datetime] NULL,[Callback_range] [tinyint] NULL,"
    SQL = SQL & "[UcFname] [nvarchar](1) NULL,[UcLname] [nvarchar](50) NULL,[UcPcode] [nvarchar](10) NULL);"

    conn.Execute SQL

    For Each td In CurrentDb.TableDefs
        If td.name = "WebsiteLeads" Then
            CurrentDb.TableDefs.Delete "WebsiteLeads"
        End If
    Next
    'link to the table just created
    Set td = CurrentDb.CreateTableDef("WebsiteLeads",dbAttachSavePWD , "[dbo].[#WebSiteLeads]", getStrConn)

    CurrentDb.TableDefs.Append td
    importProgress = 0.1    'Update Progress bar
    DoEvents

    'Import our text file
    DoCmd.TransferText acImportDelim, "Leads Import v2", "WebsiteLeads", txtFileName
    importProgress = 0.3    'Update Progress bar
    DoEvents

    'We are going to do the rest as Access stored procedure

    Set leadsRS = conn.Execute("EXEC dbo.LeadsImport;")
    importProgress = 0.9    'Update Progress bar
    DoEvents

但它失败并

        CurrentDb.TableDefs.Append td

显示错误消息“无法找到可安装的 ISAM”。

这个错误消息是什么,为什么我会收到它

I am porting a MS Access front end/backend application to be MS Access front end/sql server backend.

One part of this application (in the original form) uses VB to import a CSV file into a table in the front end, and then run various SQL statements to merge the contents of this table into some backend tables.

I figure in the new environment, because it more efficient to perform the merge process in a stored procedure, that my strategy should be

  1. Open a connection to the backend
  2. Create a temporary table on this connection
  3. Create a tabledef in VB and attempt to connect it to this temporary table
  4. Use DoCmd.TransferText (as I do in the original) to import the CSV file into the table linked to by the table def
  5. Execute a stored procedure on the open connection which merges the temporary table data into the permanent tables on the backend.

I think I am successfully performing steps 1 and 2, but I am not sure. I can see the temp table in SQL Server Management Studio, but attempting to open the columns fails with an error (it also fails with the same error when I use Management Studio to create a temp table, so I think this a Management Studio issue).

I am using the same code to perform step 3 as I use to link in all the permanent tables when I start up the application, but its this bit that is not working. Finally, I have proven step 5 works by running it against a non temporary table.

The code I use for for this is as follows

    Set conn = New ADODB.Connection
    conn.Open getStrConn

    'First create a temporary table on the server for the web site leads
    SQL = "CREATE TABLE [dbo].[#WebSiteLeads]("
    SQL = SQL & "[leadID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED,[Title] [nvarchar](255) NULL,[Firstname] [nvarchar](50) NULL,"
    SQL = SQL & "[Lastname] [nvarchar](50) NULL,[Sex] [nvarchar](10) NULL,[House] [nvarchar](50) NULL,"
    SQL = SQL & "[Address1] [nvarchar](50) NULL,[Address2] [nvarchar](50) NULL,[Street] [nvarchar](50) NULL,"
    SQL = SQL & "[Town_City] [nvarchar](50) NULL,[County] [nvarchar](50) NULL,[Postcode] [nvarchar](10) NULL,"
    SQL = SQL & "[Email] [nvarchar](50) NULL,[Allow_email] [nvarchar](5) NULL,[Telephone] [nvarchar](20) NULL,"
    SQL = SQL & "[Allow_tel] [nvarchar](5) NULL,[Cons_period] [nvarchar](20) NULL,[When] [nvarchar](20) NULL,"
    SQL = SQL & "[Procedure] [nvarchar](20) NULL,[Consultation] [nvarchar](10) NULL,[Info_pack] [nvarchar](10) NULL,"
    SQL = SQL & "[Source] [nvarchar](20) NULL,[Further_info] [nvarchar](255) NULL,[Callback] [nvarchar](50) NULL,"
    SQL = SQL & "[Date_added] [nvarchar](30) NULL,[Date_added_dt] [datetime] NULL,[Callback_range] [tinyint] NULL,"
    SQL = SQL & "[UcFname] [nvarchar](1) NULL,[UcLname] [nvarchar](50) NULL,[UcPcode] [nvarchar](10) NULL);"

    conn.Execute SQL

    For Each td In CurrentDb.TableDefs
        If td.name = "WebsiteLeads" Then
            CurrentDb.TableDefs.Delete "WebsiteLeads"
        End If
    Next
    'link to the table just created
    Set td = CurrentDb.CreateTableDef("WebsiteLeads",dbAttachSavePWD , "[dbo].[#WebSiteLeads]", getStrConn)

    CurrentDb.TableDefs.Append td
    importProgress = 0.1    'Update Progress bar
    DoEvents

    'Import our text file
    DoCmd.TransferText acImportDelim, "Leads Import v2", "WebsiteLeads", txtFileName
    importProgress = 0.3    'Update Progress bar
    DoEvents

    'We are going to do the rest as Access stored procedure

    Set leadsRS = conn.Execute("EXEC dbo.LeadsImport;")
    importProgress = 0.9    'Update Progress bar
    DoEvents

But it fails at

        CurrentDb.TableDefs.Append td

with an error message "Could not find installable ISAM."

What is this error message, and why am I getting it

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

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

发布评论

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

评论(1

┼── 2024-11-10 17:41:58

另一种选择是根本不使用链接表,而是使用返回临时表的直通查询。您可能必须即时编写它,但是一旦它返回正确的临时表,它就可以用作表单或报表的记录源。

我经常创建替换链接表的已保存查询,尽管我从未使用连接到临时表的直通来完成此操作。

Another alternative would be to not use a linked table at all, but instead have a passthrough query that returns the temp table. You'd likely have to write it on the fly, but once it's returning the correct temp table, it can be used as the Recordsource of a form or report.

I've not infrequently created saved queries that replace linked tables, though I've never done it with a passthrough connecting to a temp table.

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