Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do While Loop not giving me correct output string

I'm trying to assign a list of Names to a combobox control in Access VBA.

My problem is that the output string of names is not correct.

Here is my code:

Private Sub command186_click()
    Dim firstName As String
    Dim lastName As String
    Dim rst As Object
    Dim rowSourceText As String
    Dim fullName As String

    Set rst = CurrentDb.OpenRecordset("Pool_Contact")

    Do While Not rst.EOF
        firstName = rst("FName").Value
        lastName = rst("LName").Value
        fullName = firstName + " " + lastName
        rst.MoveNext
    Loop

    Forms(FrmDaysAvailable).Controls("Combo202").rowSource = fullName
    Debug.Print fullName
End Sub

I know that the error is somewhere inside of my loop, where the variable fullName is written over by the second result.

how can I fix this loop to produce a string that looks like fullName , fullName, fullName ...

Thanks for all your help

like image 578
Paolo Bernasconi Avatar asked Sep 22 '26 22:09

Paolo Bernasconi


1 Answers

This can be a lot simpler:

 Forms(FrmDaysAvailable).Controls("Combo202").rowSource = _
           "SELECT ID, FName & ' ' & LName FROM Pool_Contact"

Or

Forms!FrmDaysAvailable.Combo202.rowSource = _
           "SELECT ID, FName & ' ' & LName FROM Pool_Contact"

Or

Me.Combo202.rowSource = "SELECT ID, FName & ' ' & LName FROM Pool_Contact"

Furthermore, use & not + to concatenate. Plus (+) will give you problems with nulls.

like image 121
Fionnuala Avatar answered Sep 27 '26 02:09

Fionnuala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!