简体   繁体   English

如何在 MS Access 和 SQL Server 之间创建实时连接?

[英]How to create a live connection between MS Access and SQL Server?

The data I am using is stored in a SQL database and an Access database.我使用的数据存储在 SQL 数据库和 Access 数据库中。 Regular data entry needs to be made in the Access database, however, I would like to write my queries in SQL Management Studio.需要在 Access 数据库中进行常规数据输入,但是,我想在 SQL Management Studio 中编写查询。 I have connected the two databases using an ODBC connection and I can now view the tables in Management studio but the connection is not live, so any updates in Access are not reflected in the tables in Management Studio.我已经使用 ODBC 连接连接了两个数据库,现在可以查看 Management Studio 中的表,但连接不是实时的,因此 Access 中的任何更新都不会反映在 Management Studio 中的表中。

You can create a linked table from Access to SQL Server (INSERT INTO).您可以从 Access to SQL Server (INSERT INTO) 创建链接表。 You can use SQL or VBA to export all data from Access to SQL Server.您可以使用 SQL 或 VBA 将所有数据从 Access 导出到 SQL Server。

Here is one idea.这是一个想法。

Sub modExportToPG()
    Dim rs As DAO.Recordset
 'We only want to export physical user defined visible and hidden tables
       Set rs = CurrentDb.OpenRecordset("SELECT Name " &  _
        " FROM MSysObjects " & _
        "   WHERE Type=1 AND Flags < 9 ORDER BY Name;")

    Do Until rs.EOF
        'export the tables but export as lower case table names
        DoCmd.TransferDatabase acExport, "ODBC Database" _
         , "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=mydb;SERVER=myserver;PORT=5432;UID=myuser;PWD=mypwd" _ 
         , acTable, rs("Name"), LCase(rs("Name"))
        rs.MoveNext
    Loop
    rs.Close
End Sub

Also, see this for more ideas.另外,请参阅this以获取更多想法。

https://www.mssqltips.com/sqlservertip/1480/configure-microsoft-access-linked-tables-with-a-sql-server-database/ https://www.mssqltips.com/sqlservertip/1480/configure-microsoft-access-linked-tables-with-a-sql-server-database/

Came upon this s little late but I am in the midst of working through a project where we update SQL side tables at the same time that we update local and shared (server side) Access tables.有点晚了,但我正在完成一个项目,我们在更新 SQL 侧表的同时更新本地和共享(服务器端)访问表。 It is a fairly extensive application, but the SQL updates are relatively easy and there are options.这是一个相当广泛的应用程序,但 SQL 更新相对容易,并且有多种选择。 The application integrates with a Point of Sale back office server and we are sensitive to the data that gets updated instantly, so those items get tucked into batches that require approval before they get "posted" to SQL.该应用程序与销售点后台服务器集成,我们对即时更新的数据很敏感,因此这些项目会被打包成需要批准的批次,然后才能“发布”到 SQL。 Those items that are not sensitive we post immediately using the afterupdate property for each field on the main form using the following code (sample only):对于那些不敏感的项目,我们使用以下代码(仅示例)使用主表单上每个字段的 afterupdate 属性立即发布:

Private Sub Field.AfterUpdate私有子字段.AfterUpdate

Dim Var1 as string
Dim Var2 as string
Dim Var3 as string

'Capture Before and after values for the field (as well as using the_
 newvalue   to post to SQL, we store these separately as audit records_
 so we capture both old and new).

    Var1 = Me.Field.OldValue
    Var2 = Me.Field.NewValue
    Var3 = Me.PID.Text

'Execute the SQL update
DoCmd.DbExecute "Update dbo.SQLTABLE SET SQLField = '" & Var2 & "'_
WHERE SQLTABLE.PID = '" & Var3 & "'

End Sub结束子

Note: PID is a record id attached to every record in the access form that matches a corresponding record in a SQL Table (primary key).注意:PID 是附加到访问表单中与 SQL 表(主键)中的相应记录匹配的每条记录的记录 ID。

It is a lot of work if you have a number of fields to post but it's as good as it gets using linked tables in my experience.如果您要发布多个字段,那将需要大量工作,但根据我的经验,使用链接表的效果会很好。 This is a simple example and ours are far more extensive, but it does the trick.这是一个简单的例子,我们的例子要广泛得多,但它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM