VBA访问获得行来源找到查找值来源、VBA

2023-09-08 11:12:54 作者:沐叶清风

VBA小白在这里(因为这哀悼),

VBA noob here (as of this mourning),

在MS访问我写了一个测试功能来找到你传递一些条件的备案基地的价值。

In MS Access I wrote a test function to find the value of a record base on some criteria you pass in.

该功能似乎很好地工作,除非情况下,有在列查找,我正在寻找。

The function seems to work fine except in cases where there is a lookup in the column that I am searching.

基本上,它可能会返回19和19对应于一些其他的表值。

Basically it might return "19" and 19 corresponds to some other table value.

看来,列的行来源是林之后,这样我就可以做第二次查询找到真正的价值。

It seems that the RowSource of the column is what Im after so I can do a second query to find the true value.

有人点我在正确的方向上寻找行来源假设我知道列名,然后利用它来寻找后,值Im?

Can someone point me in the right direction on finding the RowSource assuming I know the column name and then utilizing it to find the value Im after?

编辑:看来,我不是我自己解释清楚,这是什么,我试图让编程图片

It seems that Im not explaining myself clearly, Here is a picture of what I trying to get programatically

推荐答案

试试这个 - 我想,我终于明白,为什么你正在寻找行来源 - 抱歉,我没有得到它在第一。你想拉的字段是一个外键到说明表。

Try this -- I think I finally understand why you are looking for the RowSource -- sorry I didn't "get" it at first. The field you're trying to pull is a foreign key into a description table.

此功能应该工作作为所有这些领域的一般溶液(假定行来源始终具有主键第一,和描述第二)。如果没有行来源,它只会拉字段的值

This function should work as a general solution for all such fields (assuming the RowSource always has the primary key first, and the description second). If there is no RowSource, it will just pull the value of the field.

这是基于原来的code,而不是提出@ron的变化,但它应该设置你在正确的方向。你应该修复它,使其参数化,并允许变量的数据类型,罗恩建议(+1 RON)

It's based on your original code, rather than the changes proposed by @ron, but it should set you in the right direction. You should fix it to make it parameterized, and allow for variant data types, as ron suggests (+1 ron)

顺便说一句,用符号(&安培; )的加入串在一起的VBA来避免这样的事情: ABC =1 + 1 ,其中ABC现在等于代替11为你所期望的,如果这两个项目的目的是字符串2。

As an aside, use the ampersand (&) to join strings together in VBA to avoid things like this: abc = "1" + 1, where abc is now equal to 2 instead of "11" as you would expect if both items were intended to be strings.

Public Function lookUpColumnValue(Database As Database, column As String, table As String, lookUpColumn As String, lookUpValue As String) As String

    Dim sql As String
    Dim recordSet As DAO.recordSet
    Dim result As String

    lookUpColumnValue = "" 'Return a blank string if no result

    On Error Resume Next

    sql = "SELECT [" & table & "].[" & column & "] FROM [" & table & "] WHERE [" & table & "].[" & lookUpColumn & "] = '" & lookUpValue & "'"
    Set recordSet = Database.OpenRecordset(sql)

    If Not recordSet.EOF Then
        Dim td As DAO.TableDef

        'this gives your number - say, 19
        result = recordSet(column)

        Set td = Database.TableDefs(table)

        'Get the rowsource
        Dim p As DAO.Property
        For Each p In td.Fields(column).Properties
            If p.Name = "RowSource" Then
                RowSource = Replace(td.Fields(column).Properties("RowSource"), ";", "")
                Exit For
            End If
        Next

        If Not RowSource = "" Then
            Dim rs2 As DAO.recordSet
            Dim qd As DAO.QueryDef

            Set qd = Database.CreateQueryDef("", RowSource)
            Set rs2 = Database.OpenRecordset(RowSource)

            If rs2.EOF Then Exit Function

            PKField = rs2.Fields(0).Name

            rs2.Close
            qd.Close

            sql = "SELECT * FROM (" & RowSource & ") WHERE [" & PKField & "]=[KeyField?]"
            Set qd = Database.CreateQueryDef("", sql)
            qd.Parameters("KeyField?").Value = result

            Set rs2 = qd.OpenRecordset()

            If Not rs2.EOF Then
                'NOTE: This assumes your RowSource *always* has ID first, description 2nd.  This should usually be the case.
                lookUpColumnValue = rs2.Fields(1)
            End If
        Else
            'Return the field value if there is no RowSource
            lookUpColumnValue = recordSet.Fields(column)
        End If
    End If

End Function