简体   繁体   English

Excel VBA 连接到 MS Access(读/写)到多用户表/查询

[英]Excel VBA Connection to MS Access (Read/Write) to Multi User Tables/ Queries

I have got to the end of trying to figure out why I can't get correct settings for multi user read and update to MS Access tables from Excel (Office 2013).我已经试图弄清楚为什么我无法从 Excel (Office 2013) 获得多用户读取和更新到 MS Access 表的正确设置。 So I am hoping someone here can help.所以我希望这里有人可以提供帮助。

Background - I have an MS Access database that contains all data in tables that is used by an Excel application that runs on each users machine.背景 - 我有一个 MS Access 数据库,其中包含在每个用户计算机上运行的 Excel 应用程序使用的表中的所有数据。 The .accdb file resides on a shared QNAP NAS. .accdb 文件位于共享的 QNAP NAS 上。 I have designed the system so there is a user list table that is accessed on startup via a form, allowing users to 'login' to the system.我设计了这个系统,所以有一个用户列表,可以在启动时通过表单访问,允许用户“登录”到系统。 When a user is logged in, the intention is to record a Yes/No field in this table to prevent the same user name being used on two different machines accessing the same subset of data.当用户登录时,目的是在此表中记录是/否字段,以防止在访问相同数据子集的两台不同机器上使用相同的用户名。 Now this all works well from one machine or multiple excel instances on one machine.现在,这一切在一台机器上运行良好,或在一台机器上运行多个 excel 实例。 When I load the excel application on multiple machines, the Yes/No field is inconsistent between the machines, unless I stop the Excel application on the machine and restart.当我在多台机器上加载excel应用程序时,机器之间是/否字段不一致,除非我停止机器上的Excel应用程序并重新启动。 Sometimes I get access errors or database corruption errors in this multi user setup - so I am clearly doing something wrong - but can't figure it out.有时我会在这个多用户设置中遇到访问错误或数据库损坏错误 - 所以我显然做错了什么 - 但无法弄清楚。 Help Please!!请帮忙!!

The connection from the excel application is done as follows:来自excel应用程序的连接按如下方式完成:

' connect to the Access database
Set cn = New ADODB.Connection
With cn
 .Provider = "Microsoft.ACE.OLEDB.12.0"
 .Mode = adModeShareDenyNone
 .CursorLocation = adUseServer
 .Open ThisWorkbook.Worksheets("Title").Range("B1") & "cms.accdb"
End With

The recordset is opened in excel as follows (which does not show a user set as logged on another machine in via the yes/no field unless I start the application AFTER they have 'logged in')记录集在 excel 中打开如下(除非我在他们“登录”后启动应用程序,否则不会通过是/否字段显示在另一台机器上登录的用户设置)

Set rs = New ADODB.Recordset
sSQL = "SELECT [access list].* FROM [access list] where [access list].[user] = '" &        Me.userlist.Text & "';"
rs.Open sSQL, cn, adOpenDynamic, adLockPessimistic
Application.CalculateUntilAsyncQueriesDone ' not sure I need this????

The test for this user being logged in is:该用户登录的测试是:

If rs.Fields("Logged in").Value = -1 Then
    fname = rs.Fields("user").Value
    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
    MsgBox fname & " is already logged in - please select another user or exit"
Else
    userid = rs.Fields("user").Value
    rs.Close
    Set rs = Nothing
    sSQL = "Update [access list] Set [access list].[logged in] = '-1' WHERE [access list].[user] = '" & userid & "'"
    cn.Execute (sSQL)

    MsgBox "Welcome " & userid & "! Starting system for your contracts."
    cn.Close
    Set cn = Nothing
    Unload Me

End If

So what am I doing wrong?那么我做错了什么? I've tried all different ways with little difference.我尝试了所有不同的方法,但几乎没有什么不同。 (Note - I am not trying to test race conditions by both logging in to the same user at exactly the same time - a situation that is not of concern - this is just a simple lock out model I'm implementing). (注意 - 我不是试图通过完全同时登录到同一个用户来测试竞争条件 - 这种情况并不重要 - 这只是我正在实施的一个简单的锁定模型)。 I thought what I was doing was keeping record level locking, made sure I could see any other user change to the record in each application on each machine.我认为我所做的是保持记录级锁定,确保我可以看到任何其他用户对每台机器上每个应用程序中记录的更改。 I'm stumped!我难住了! Opening MS Access and looking at the database shows the inconsistency - on one machine the user is logged in, on the other the user is not.打开 MS Access 并查看数据库显示不一致 - 在一台计算机上,用户已登录,而在另一台计算机上,用户未登录。 I'm thinking its how I connect to the access database or some setting in the access database that I haven't got right - but thats just a thought.我在想它是如何连接到访问数据库或访问数据库中的一些我没有正确设置的 - 但这只是一个想法。

One last twist to this - my test environment machines are Apple MacBooks running Win 8.1/ Office 2013 in a VMware Fusion virtual machine.对此的最后一个转折 - 我的测试环境机器是在 VMware Fusion 虚拟机中运行 Win 8.1/Office 2013 的 Apple MacBook。 I do not have the environment to test on native Win 8.1 machines at the moment, but this will be the eventual target.我目前没有在本机 Win 8.1 机器上测试的环境,但这将是最终目标。 Maybe it will work on the target environment, but can't test that yet and as that is the last step, working out any problems with the code in the test environment is my current priority.也许它可以在目标环境上工作,但还不能测试,因为这是最后一步,解决测试环境中代码的任何问题是我当前的首要任务。

Russ拉斯

I see this post is old, so you may have moved on already, but thought I would answer in case it helps someone.我看到这篇文章很旧,所以你可能已经继续前进了,但我想我会回答,以防它对某人有帮助。 I have been investigating a similar issue where cursor location and lock type seem to be specified correctly, but I still sometimes get inconsistent read results.我一直在调查一个类似的问题,其中似乎正确指定了光标位置和锁定类型,但有时我仍然会得到不一致的读取结果。 I am no expert, but this may have to do with the cache refresh and write delays associated with JET/ACE/Access databases.我不是专家,但这可能与与 JET/ACE/Access 数据库相关的缓存刷新和写入延迟有关。 You may want to play around with the Page Timeout and Flush Transaction Timeout settings and consider using the DBEngine.Idle dbRefreshCache statement before doing a read - although this is a DAO feature.您可能想要尝试使用 Page Timeout 和 Flush Transaction Timeout 设置,并在进行读取之前考虑使用 DBEngine.Idle dbRefreshCache 语句 - 尽管这是 DAO 功能。

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

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