简体   繁体   English

使用VBA从Excel写入以访问多列组合框字段

[英]Write from Excel to Access multicolumn combobox field using VBA

I have an Access file with two tables: users & products. 我有一个包含两个表的Access文件:用户和产品。 Users keeps a list of who can write to the Access file (fields like userID, systemID, name). 用户保留了谁可以写入Access文件的列表(如userID,systemID,name之类的字段)。 Products keeps a list of product attributes including who made the last update to the record. 产品保留产品属性的列表,包括谁对记录进行了最后更新。 The last update field is a combobox with two columns: userID (bound to this), name (displays this due to column widths of 0";2"). 最后一个更新字段是一个具有两列的组合框:userID(绑定到此),名称(由于列宽为0“; 2”而显示此名称)。

I also have an Excel file, named simulator. 我还有一个名为模拟器的Excel文件。 Using VBA, the simulator reads from the products table, uses assorted prediction algorithms to simulate the product's future, then writes the predictions back to Access. 模拟器使用VBA读取产品表,使用各种预测算法来模拟产品的未来,然后将预测写回到Access。

When Excel writes back to a product's record, I'd like to record the last update author to be simulator. 当Excel写回产品记录时,我想将最后一个更新作者记录为模拟器。 Assuming this user exists (userID=100, name=Simulator, say), how do I do this? 假设该用户存在(userID = 100,name = Simulator,例如),我该怎么做?

I currently have 我目前有

' Open Access database, get products table, get product record
connection.Open ...
products.Open "Products", connection, ...
products.Filter = "ProductID = " & productNumber

' Update record
products("LastUpdateAuthor") = "100; Simulator"
products.Update
products.Close

And this writes "100; Simulator" to the correct field. 并将“ 100; Simulator”写入正确的字段。 How do I get it to recognize that 100 is the bound column and Simulator is the second column? 如何识别绑定列100和模拟器是第二列?

Should only save the UserID into LastUpdateAuthor field. 应该只将UserID保存到LastUpdateAuthor字段中。 Then multi-column combobox RowSource should be an SQL statement of Users table in order to retrieve and view the related UserName. 然后,多列组合框RowSource应该是Users表的SQL语句,以便检索和查看相关的UserName。 So have a record in Users with UserID 100 and name Simulator, then still just save the UserID. 因此,在用户ID为100的用户中有一条记录,并命名为Simulator,然后仍然只保存用户ID。

As long as the RowSourceType is Table/Query, it will see the 100; Simulator 只要RowSourceType是Table / Query,它将看到100; Simulator 100; Simulator value as a single string from the LastUpdateAuthor field. 100; Simulator值作为来自LastUpdateAuthor字段的单个字符串。 Can set combobox RowSourceType as ValueList then use code manipulating recordset and Add method to load the LastUpdateAuthor data to the RowSource and the semi-colon will be recognized as column separator. 可以将组合框RowSourceType设置为ValueList,然后使用操作记录集和Add方法的代码将LastUpdateAuthor数据加载到RowSource,并且分号将被识别为列分隔符。 However, if you do as described in first paragraph, this should not be necessary. 但是,如果您按照第一段所述进行操作,则没有必要。

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

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