B4J Question MySQL ComboBox Question

Hi Guys,

I have a MySQL table with two fields (Code, Name). I want to display the Name in the combo box but also capture the code as this is the unique identifier in which I need to reference later when pulling data from another MySQL table.

I currently have this to get the Name into the combobox.

Or alternatively, I want to be able to grab the code on ComboBox_ValueChanged

Sub UpdateListItems

ComboBox1.Items.Clear
Dim RS As ResultSet = sql1.ExecQuery("Select Code, Name from Store")

Do While RS.NextRow

ComboBox1.Items.Add(RS.GetString("Code"))

Loop

End Sub
 

MarkusR

Well-Known Member
Licensed User
you can add here ComboBox1.Items.Add a object / type / class
but i not know how to show only the text without id.

instead of a ComboBox you can also use a label/button and a pup up selection list.
a button have a tag property where you can store a selected object/type.

if you need this kind of combobox more than once a customview user control make sense.
 
Last edited:

BPak

Active Member
Licensed User
Here is a method I use a lot with sql and ComboBox.

B4X:
Sub Class_Globals
    Private AcntCmb As ComboBox
    Private AcntList As List
End Sub

Public Sub Initialize(Parent As Form)
    AcntList.Initialize
    ' Loads Acnt table int AcntCmb
    LoadAcntCmb
End Sub

' ExecuteMemoryTable loads into AcntList
' AcntCmb is loaded with 'Name' cols(1)
' First entry in AcntCmb is selected
' The AcntCmb_SelectedIndexChanged event is then fired
Sub LoadAcntCmb
    Dim cols() As String
    Dim Qry1 As String
    Qry1 = "SELECT AId, Name, AvgDiv, SRate, Target, TotTarg FROM Acnt"
    AcntList.Initialize
    AcntList = DBUtils.ExecuteMemoryTable(DM.SQL1, Qry1, Null, 0)
    If AcntList.Size > 0 Then
        For i = 0 To AcntList.Size-1
            cols = AcntList.Get(i)
            AcntCmb.Items.Add(cols(1))
        Next
        ' selects first entry in combobox
        AcntCmb.SelectedIndex = 0
    End If
End Sub

' Gets AId (PRIMARY KEY) from selected Account
' Use AcntCmb Index to get AId from AcntList
' Get any data from AcntList needed
Sub AcntCmb_SelectedIndexChanged(Index As Int, Value As Object)   
    Dim Cols() As String
    
    If Index = -1 Then Return
    Cols = AcntList.Get(Index)
    AId = Cols(0)
    TotalTarget = Cols(5)
    
    AcAvgDiivLbl.Text = $"$${Cols(2)}"$
    AcSRateLbl.Text = $"${Cols(3)}%"$
    TotTargLbl.Text = $"${Cols(5)}"$
    
    OrgTarget = Cols(4)
    
End Sub
 
Top