简体   繁体   English

在MS-ACCESS中使用“查找”表进行更新查询

[英]Using a 'lookup' table in MS-ACCESS for an update query

I have an Access Database with a table [tblManipulate] with the following four fields populated with data: 我有一个带有表[tblManipulate]的Access数据库,其中的以下四个字段填充了数据:

[tblManipulate].[Name] 
[tblManipulate].[Description] 
[tblManipulate].[Price]
[tblManipulate].[Account code]

I also have an 150 entry table of descriptions called [tblDescLookup] that needs to be utilized like a lookup table in order to manipulate account codes. 我还有一个名为[tblDescLookup]的描述的150条目表,需要像查找表一样使用它来操纵帐户代码。 Example entries follow: 条目示例如下:

[tblDescLookup].[Description Lookup]    [tblDescLookup].[Account Code Result]
*demonstration*                         10000
*coding*                                12000
*e-mail*                                13000

What is the best way to take every record in [tblManipulate] and check the [tblManipulate].[Description] field against [tblDescLookup].[Description Lookup], assigning the account code result into the original table if a 'like' match is found? 最好的方法是获取[tblManipulate]中的每条记录,并对照[tblDescLookup]。[Description Lookup]检查[tblManipulate]。[Description]字段,如果“ like”匹配为,则将帐户代码结果分配到原始表中发现了什么?

This seems to me like one of those instances where Access is not the best tool for the job, but it is what I have been instructed to use. 在我看来,这似乎是Access并不是最佳的工具,但这是我被指示使用的实例之一。 I would appreciate any help or insight (or alternatives!). 我将不胜感激任何帮助或见解(或替代方案!)。 Thank you! 谢谢!

Something like this should do it for you. 这样的事情应该为您做。

Dim Description As String
Dim lookupDescription As String

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset(SELECT * FROM tblManipulate) 

 If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst 'good habit

       Do Until rs.EOF = True


       Description = rs("Description")

       Dim rsLookUp As DAO.Recordset
       Set rsLookUp = CurrentDb.OpenRecordset(SELECT * FROM tblDescLookup) 


              If Not (rsLookUp .EOF And rsLookUp .BOF) Then
                      rsLookUp .MoveFirst 'good habit

                      Do Until rsLookUp.EOF = True

                      lookupDescription = rsLookUp("Description Lookup") 

                      If() Then    'match criteria
                       'assign value 
                      End if

                      rsLookUp.MoveNext
                      Loop

                     Else
                          MsgBox "No records in the recordset."
                      End If


              rs.MoveNext
            Loop


            Else
                MsgBox "No records in the recordset."
 End If

Oy. Oy公司。 You're going to need a loop here. 您将需要在此处循环。 I would open up tblDescLookup in a recordset: 我将在一个记录集中打开tblDescLookup:

Set rec = CurrentDB.OpenRecordset ("Select * from tblDescLookup")

Then loop through each record and run the query that way: 然后遍历每条记录并以这种方式运行查询:

Do While rec.EOF = False

    Set rec2 = CurrentDB.OpenRecordset ("Select * from rec where Description like '" & rec("Description Lookup") & "'")

    rec.MoveNext

Loop

Or maybe you need to make that an Update statement instead? 或者,也许您需要使它成为Update语句? I can't write that off the top of my head, but you get the idea. 我不能从头顶上写下来,但是你明白了。

Have you tried something like this? 你尝试过这样的事情吗?

Update tblManipulate as t1
Set [Account Code] = (Select [Account Code Result] from [tblDescLookup] where [Description Lookup] = t1.[Description])

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

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