使用VBA将ID转换为Name

时间:2014-08-27 17:35:07

标签: vba ms-access access-vba ms-access-2010

所以在我的数据库中,我有一个联结表,其中列出了所有"目的"有人要贷款。只显示值(以数字形式)时代码有效,但在我的第二个循环中,我在第二个表中搜索目的的实际名称,我遇到了问题。它显示了正确的数字目的,但不幸的是,该文本重复了第一个找到的目的,而不是显示所有适用的目的。

以下是我使用的代码:

Private Sub cmdMsgBox_Click()

Dim DB As Database
Dim tblOpp2LP As Recordset
Dim tblLoanPurpose As Recordset
Dim ctlFindRecord As Control
Dim lngHoldOpportunityID As Variant
Dim intRecordCount As Integer
Dim valTestBox As String
Dim valLP As String
Dim valName As String

Set DB = CurrentDb
Set tblInputOpp2LP = DB.OpenRecordset("Opp2LP")

Set tblLoanPurpose = DB.OpenRecordset("LoanPurpose")
Set ctlFindRecord = Me.ctlFindRecord


lngHoldOpportunityID = CLng(ctlFindRecord)
valTestBox = ""

On Error GoTo ErrorHandling_Err:

tblInputOpp2LP.FindFirst "[OpportunityID] = " & lngHoldOpportunityID

If tblInputOpp2LP.NoMatch Then
    MsgBox "No Matching Record Found"
    Exit Sub
Else
Do Until tblInputOpp2LP.EOF
    If lngHoldOpportunityID = tblInputOpp2LP![OpportunityID] Then
        valLP = tblInputOpp2LP![LPID]
        intCounter = intCounter + 1

        Do Until tblLoanPurpose.EOF
            If valLP = tblLoanPurpose![LPID] Then
                valName = tblLoanPurpose![Name]
            End If
            tblLoanPurpose.MoveNext
        Loop

        If valTestBox = "" Then
            valTestBox = valName
        Else
            valTestBox = valTestBox & ", " & valName
        End If
    End If

    tblInputOpp2LP.MoveNext
Loop

txtMsgbox = valTestBox

End If

ErrorHandling_Exit:
    Exit Sub

ErrorHandling_Err:
    MsgBox Err.Description & " - " & Err.Number
    Resume ErrorHandling_Exit
End Sub

思考?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了!

事实证明,我需要做的是添加"退出do"在第二个循环中。

请参阅以下代码示例。

Private Sub cmdMsgBox_Click()

Dim DB As Database
Dim tblOpp2LP As Recordset
Dim tblLoanPurpose As Recordset
Dim ctlFindRecord As Control
Dim lngHoldOpportunityID As Variant
Dim intRecordCount As Integer
Dim valTestBox As String
Dim valLP As String
Dim valName As String

Set DB = CurrentDb
Set tblInputOpp2LP = DB.OpenRecordset("Opp2LP")

Set tblLoanPurpose = DB.OpenRecordset("LoanPurpose")
Set ctlFindRecord = Me.ctlFindRecord


lngHoldOpportunityID = CLng(ctlFindRecord)
valTestBox = ""

On Error GoTo ErrorHandling_Err:

tblInputOpp2LP.FindFirst "[OpportunityID] = " & lngHoldOpportunityID

If tblInputOpp2LP.NoMatch Then
    MsgBox "No Matching Record Found"
    Exit Sub
Else
Do Until tblInputOpp2LP.EOF
    If lngHoldOpportunityID = tblInputOpp2LP![OpportunityID] Then
        valLP = tblInputOpp2LP![LPID]
        intCounter = intCounter + 1

        Do Until tblLoanPurpose.EOF
            If valLP = tblLoanPurpose![LPID] Then
                valName = tblLoanPurpose![Name]
                exit do
            End If
            tblLoanPurpose.MoveNext
        Loop

        If valTestBox = "" Then
            valTestBox = valName
        Else
            valTestBox = valTestBox & ", " & valName
        End If
    End If

    tblInputOpp2LP.MoveNext
Loop

txtMsgbox = valTestBox

End If

ErrorHandling_Exit:
    Exit Sub

ErrorHandling_Err:
    MsgBox Err.Description & " - " & Err.Number
    Resume ErrorHandling_Exit
End Sub