By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,705 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,705 IT Pros & Developers. It's quick & easy.

Populating a List Box

P: 1
Hi all,

I am trying to populate a list box ona form with data in a table. I am new to Access VBA and am not able to do it. Following is the snapshot of my code. Could somebody please look into it tell me where am I going wrong and wht shld I change. Any help would be sincerely appreciated.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim strSQL As String
  3.     Dim strcustomername As String
  4.     Dim rstLoans As DAO.Recordset
  5.     Dim dbase As DAO.Database
  7.  'strcustomername = txtorderno.Value
  8.     Set rstLoans = dbase.OpenRecordset("MASTERRECORD")
  9.      'Set rstLoans = dbase.OpenRecordset("MASTERRECORD", dbOpenCDO)
  10.     SecuritiesList.RowSource = ""
  12. 'strSQL = "SELECTtblorder.orderno,tblorder.ProductID1,tblorde  r. [Clothingtype1]"
  13. 'strSQL = strSQL & "FROM tblorder;"
  17.     Me.SecuritiesList.RowSource = strSQL
  18. End Sub
Aug 13 '08 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,545
Hi. There is no need at all to define recordsets if you want to set the recordsource of a listbox in code. The only statement that matters is the one setting your rowsource - none of the recordsource or database lines have any relevance to this at all and can be removed with no penalty. Doing so on the code you provided and removing the commented out lines leaves just:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim strSQL As String
  3.     strSQL = "SELECT * FROM [MASTERRECORD]"    
  4.     Me.SecuritiesList.RowSource = strSQL
  5. End Sub
As it is just one very simple select statement this should provide to the listbox all fields from your masterrecord table in the order they are defined for that table, if this is indeed what you want to show in your listbox. However, what your listbox shows to users depends on the number of columns you have defined for that listbox, and their widths. You will need to check what you have specified for that listbox to make sure you will actually see the list of fields concerned. You won't see much if you have defined just 1 column of width 0...

If you make further changes to the recordsource of a control you may need to include a requery call to effect those changes after setting the rowsource property.

Expand|Select|Wrap|Line Numbers
  1. Me.SecuritiesList.Requery
Aug 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.