質問

Dear all and thank you in advance.

Problem:

Instead of having two dropdown boxes for list A and list B. I would like to combine the two lists (they do relate) and display them to the user as 1 list. Then split it back into two so that I can store the relevant info.

List 1

Machines
1. Machine x
2. Machine y

List 2 Test Types

1. Test Type ab
2. Test Type ac
3. Text Type ad.

so Machine 1 can do test type ab, and ac. Machine 2 can do test type ac and ad.

It will be stored under 3 different tables (not really but just think that it will). 2 tables will contain the list and the third will contain the relationship between the two lists. I.e. which items from list 1 pair up with which items from list 2 etc.

To the user it would be displayed as follows

Machine X - ab
Machine x - ac
Machine y - ac
Machine y - ad

The user would then select 1 from the list and I would then decode the two items selected.

My thought so far is to use bits (and/or) as required.

There will be three functions

public function joinAB(a as long, b as long) as long

end function

Public function getA(ab as long) as long

end function

public function getB(ab as long)as long 
end function

So just to clarify this is not to join text together, but to join/split ID's of the individual items in these two lists.

Anyone else have any other ideas. This will be done in a legacy system (VB6) which I have inherited. My VB6 coding skills are above average.

Thank you for any help/code snippets provided or general advice.

If you need more information please let me know.

役に立ちましたか?

解決

Assuming a and b are numeric variables as your 3 functions suggest I would use the .ItemData() property of the items in the combined list just like Mark and use division and remained to obtain the separate parts:

Public Function joinAB(a As Long, b As Long) As Long
  joinAB = a * 100 + b
End Function

Public Function getA(ab As Long) As Long
  getA = ab \ 100
End Function

Public Function getB(ab As Long) As Long
  getB = ab Mod 100
End Function

this assumes that b will never be higher than 100, and that neither a or b will be negative

If a and b are string variables then i would show the joined strings as the text int he combobox and split the selected text to get the seprate parts

Public Function joinAB(a As String, b As String) As String
  joinAB = a & " - " & b
End Function

Public Function getA(ab As String) As String
  getA = Left$(ab, InStr(ab, " - ") - 1)
End Function

Public Function getB(ab As String) As String
  getB = Mid$(ab, InStr(ab, " - ") + 3)
End Function

他のヒント

make use of the 3rd table in which the relation is mapped , if the relation have a unique id then you can make use of that to join/split the list....

or provide us with the table structure and data...

There are probably a few solutions to this.

The simplest is that you create a unique 32-bit integer field on your join table. This can then be embedded in the ItemData property of the VB6 ListBox.

A couple of helper functions for this are:

Private Sub AddListBoxItem(ByRef lst as ListBox, ByVal in_nKey As Long, ByRef in_sDisplay As String)
    With lst
        .AddItem in_sDisplay
        .ItemData(.NewIndex) = in_nKey
    End With
End Sub

Private Function GetSelectedListBoxKey(ByRef in_lst As ListBox) As Long
    With in_lst
        GetSelectedListBoxKey = .ItemData(.ListIndex)
    End With
End Function

As for implementing your functions, I would simply use two collection.

m_col_A_B_to_AB would be keyed by A & "_" & B to return AB. m_col_AB_to_A_B would be keyed to AB to return A and B.

Helper functions would be:

Private Sub AddRow(ByVal in_nA As Long, ByVal in_nB As Long, ByVal in_nAB As Long)
    Dim an(0 To 1) As Long
    an(0) = in_nA
    an(1) = in_nB
    m_col_A_B_to_AB.Add an(), CStr(in_nAB)
    m_col_AB_to_A_B.Add in_nAB, CStr(in_nA) & "_" & CStr(in_nB)
End Sub

Private Sub Get_A_B(ByVal in_nAB As Long, ByRef out_nA As Long, ByRef out_nB As Long)

    Dim vTmp As Variant
    vTmp = m_col_A_B_to_AB.Item(CStr(in_nAB))

    out_nA = vTmp(0)
    out_nB = vTmp(1)

End Sub

Private Function GetA(ByVal in_nAB As Long) As Long
    Get_A_B in_nAB, GetA, 0&
End Function

Private Function GetB(ByVal in_nAB As Long) As Long
    Get_A_B in_nAB, 0&, GetB
End Function

Private Function JoinAB(ByVal in_nA As Long, ByVal in_nB As Long) As Long

    JoinAB = m_col_AB_to_A_B.Item(CStr(in_nA) & "_" & CStr(in_nB))

End Function
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top