We help IT Professionals succeed at work.

Add items to listbox upon opening form.

stephenlecomptejr
on
I'm unable to display the values in a listbox upon the database opening up.  Please note code below and sample.  Please tell me how I can rearrange my syntax or recordsource to display the values correctly.

https://filedb.experts-exchange.com/incoming/ee-stuff/8155-sample_listbox.mdb 

I just want either someone to fix the code in the sample or tell me directly how it should properly otherwise.  Have to get out something very soon.
Option Compare Database
Option Explicit

Private Sub cmdAdd_Click()
  lstSection.AddItem Item:=New_Section_Value
End Sub

Private Sub Form_Load()
  
End Sub

Private Sub Form_Open(Cancel As Integer)

  Call Set_Combos
  Call Set_ListBoxes
  
End Sub

Private Sub Set_Combos()
  cboVolume.Value = Nz(cboVolume.Column(0, 0), 0)
End Sub

Private Sub Set_ListBoxes()
  Call Fill_Section_ListBox
End Sub

Private Sub Fill_Section_ListBox()
  
  Dim lVol As Long
  lstSection = ""
  lVol = Nz(cboVolume.Column(0, 0), 0)
  Dim sSQL As String
  sSQL = "SELECT [SectionName], [VolumeID] FROM [tblSections] WHERE [VolumeID] = " & lVol & " ORDER BY [SectionName] "
  Dim rs As DAO.Recordset
  Set rs = CurrentDb.OpenRecordset(sSQL)
  Do Until rs.EOF
    lstSection.AddItem Nz(rs.Fields(0), "")
    rs.MoveNext
  Loop
  rs.Close
  Set rs = Nothing
  Me.Refresh
  DoEvents
  
End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
change the Column Count of the listbox to 1
remove the entry on the column widths property,
 should be empty since there will only be a single column returned by  the line
       
      Nz(rs.Fields(0), "")
CERTIFIED EXPERT
Top Expert 2016

Commented:

Author

Commented:
Thanks caps for both comments.

I appreciate it!

Author

Commented:
As always above an A grade.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.