p27
p27

Reputation: 2222

How to populate listbox of MS access on Button Click Event

I have a sub in VBA where I am fetching data from database on button click event like below

Public Sub getNearestPfk(runNR As Integer)
Dim DB As dao.Database
Dim rs As dao.Recordset
Dim sqlString As String
Dim record As Object
Dim objPFK As Pfk

Dim i As Integer

i = 0


Set DB = CurrentDb
sqlString = " SELECT TOP 5 T_TEMP_DESTANCE_CAL.* FROM T_TEMP_DESTANCE_CAL WHERE RUNID = " & runNR & " ORDER by DISTANCE "
MsgBox (sqlStringGetNearestPFK)

Set rs = DB.OpenRecordset(sqlString)

Do Until rs.EOF

objPFK.ID = rs!ID
objPFK.fistName = rs!firstName
objPFK.lastName = rs!lastName
objPFK.distance = rs!distance
objPFK.duration = rs!duration
objPFK.address = rs!address

arrayObjPFK(i) = objPFK


rs.MoveNext
i = i + 1
Loop


rs.Close
Set rs = Nothing
Set DB = Nothing


End Sub

Now I have all the data Which I want to display

how should I display this data in listbox so on this button click a listbox will populate

Upvotes: 0

Views: 1681

Answers (1)

Y2Que
Y2Que

Reputation: 113

You can set the list box property called Row Source Type to Table/Query and then your Row Source property to the SQL statement.

If you want to update the list box, simply update the Row Source.

Upvotes: 1

Related Questions