2

请就这个话题提供帮助

  1. 通过 VBA 表单(Access 项目),我需要连接到 SQL Server 2008 并加载表(或连接)。
  2. 我需要针对 VBA 表单中的表生成一些报告(在 VBA 内部编写的查询)。

请参阅任何教程来启动此任务。如果论坛中已经存在此问题,请提供链接。

提前致谢。

最好的问候, 巴拉

4

2 回答 2

1

此链接显示如何将表与 VBA 链接。您很可能需要一个表名表,以便您的代码可以循环并创建或刷新链接,甚至将它们设置为不同的 SQL Server 实例。该链接显示了更多链接方式,但如果 SQL Server 表具有主键,则此代码将起作用。这是此链接中的代码。

' Some variable to make the code more generic
Dim strConnectionString As String
Dim strNameInAccess As String
Dim strNameInSQLServer As String
' set the connection string
strConnectionString = "ODBC;DRIVER=SQL Server; " & _
"SERVER=.\SQLExpress;DATABASE=MyDatabase;Trusted_Connection=Yes"
' specify the tables you want to link. The table can be
' known by a different name in Access than the name in SQL server
strNameInAccess = "tblYacht"
strNameInSQLServer = "tblSailingBoat"
' delete the table from the local database if it exists
On Error Resume Next
DoCmd.RunSQL "drop table " & strNameInAccess
On Error GoTo 0
DoCmd.TransferDatabase acLink, "ODBC Database", _
strConnectionString, acTable, strNameInSQLServer, strNameInAccess
于 2012-11-15T18:34:16.023 回答
1

您应该使用用于连接外部表的 Access 向导来连接这些表。然后可以轻松地为表单和报告操作 SQL Server 表。http://office.microsoft.com/en-ie/access-help/import-or-link-to-sql-server-data-HA010200494.aspx

于 2012-11-15T16:35:57.040 回答