473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Populating the headers in a listbox

Hi, I have a list box being populated from an external database. There are 6 columns in the list box. My code goes out to a database runs a query and populates a list box. I have the ColumnsHeads property set to true, it shows the column head boxes but it won't populate them. My code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub UserForm_Initialize()
  2.  
  3. Dim sdate As Date
  4. Dim fdate As Date
  5.  
  6. fdate = ActiveProject.CurrentDate
  7. fdate = Format(fdate, "Ddddd")
  8.  
  9. txtend.Value = fdate
  10. txtstart.SetFocus
  11.  
  12. End Sub
Expand|Select|Wrap|Line Numbers
  1. Public Function test(sdate, fdate) As String
  2.  
  3. Dim cmd As ADODB.Connection
  4. Dim rs As ADODB.Recordset
  5. Dim qry As ADODB.Recordset
  6. Dim prm As ADODB.Parameter
  7. Dim prm1 As ADODB.Parameter
  8. Dim strconn As String
  9.   Set cmd = New ADODB.Connection
  10.  
  11. strconn = "Provider= microsoft.ace.oledb.12.0;" & "Data Source = Z:\TRS Commercial\TRS Prod\data bases\parts status1.accdb"
  12. cmd.Open strconn
  13.  
  14. Dim k As New ADODB.Command
  15. Set qry = New ADODB.Recordset
  16. Set rs = New ADODB.Recordset
  17.  
  18. cmd.CursorLocation = adUseClient
  19.  
  20. Dim tskdat() As String
  21. Dim i As Integer
  22. Dim objfield As ADODB.Field
  23. Dim icol As Integer
  24. Dim ifld As Integer
  25. Dim n As Integer
  26. Dim m As Integer
  27.  
  28. 'opening and running the query
  29. Set k.ActiveConnection = cmd
  30. k.CommandType = adCmdStoredProc
  31.  
  32. k.CommandTimeout = 8000
  33. k.CommandText = "updatetest"
  34.  
  35. Set prm = k.CreateParameter
  36. prm.Type = adDate
  37. k.Parameters.Append prm
  38. k.Parameters(0).Value = sdate
  39.  
  40. Set prm1 = k.CreateParameter
  41. prm1.Type = adDate
  42. k.Parameters.Append prm1
  43. k.Parameters(1).Value = fdate
  44.  
  45. Erase tskdat
  46.  
  47. qry.Open k
  48.  
  49. If Not qry.EOF Then qry.MoveLast
  50. n = qry.RecordCount - 1
  51. m = qry.Fields.Count - 1
  52. ReDim tskdat(n, m)
  53. qry.MoveFirst
  54.  
  55. ' populating the list box
  56.     i = 0
  57.  
  58.   Do Until qry.EOF
  59.   'objfield.Name
  60.  
  61.     For icol = 0 To m
  62.         tskdat(i, icol) = qry.Fields(icol)
  63.     Next
  64.     i = i + 1
  65.     qry.MoveNext
  66.  
  67. Loop
  68.  lbdata.ColumnHeads = True
  69.  lbdata.List() = tskdat
  70.  
  71. cmd.Close
  72. Set qry = Nothing
  73. Set k = Nothing
  74. Set cmd = Nothing
  75. Set rs = Nothing
  76.  
  77. End Function
Can anybody please help me to get these coulmn heads to populate?

Thank you.
Jul 29 '14 #1
6 7825
zmbd
5,501 Expert Mod 4TB
You have to use the "Row Source" property of the listbox control.
You cannot do this using the "additem" method.

Two examples
1) Table or query
Set the row source to point to the table name, query name, or enter the SQL in the RowSource property directly.
Set your number of columns, formating etc...
Set the column headings to yes.
The first row will now have the field names as the header.
Use a query and alias the field names as the rowsource to alter the names displayed.

2) Data list
Once again you will use the rowsource property.
Easy example.
Insert a listbox and set up for two columns, both shown, and the headers property is set to true, then in the row source property enter: Header1, Header2; row1cell1, row1cell2; row2cell1, row2cell2; row3cell1, row3cell2; You will see that the the first entries are in the header row and the remaining in the subsequent row.

This has been vetted on my test forms in
ACC2003 WinXPEnterprise (had several like this)
ACC2010(32bit)Win7Enterprise(64bit)
Jul 29 '14 #2
Tried it, the list box is on a form in a microsoft project file, and it won't let me set the record source property. I get an "Invalid row source property" error.
Jul 30 '14 #3
zmbd
5,501 Expert Mod 4TB
You didn't state you were using Microsoft Project. Which would have been most helpfull as this forum deals primarily with Access.

For MSP, you will not have a recordsource because it's not a database nor worksheet; therefor, there is no known way to populate the list box header row. If you spend a few days with the search engines you'll find that this is a very common frustration with VBA programmers since circa Win95 release.

There may be some API call that will work; however, that is a tad out of my knowledge base.
Jul 30 '14 #4
zmbd
5,501 Expert Mod 4TB
After reading a littlebit on MS-Project I have an additional question as the Rowsource Property should be available...

Are you attempting to use the .additem method for the listbox?

If so, that will NOT work. You can use one method or the other but not both.

You also need to properly set the rowsource type to either list or table/query.
Jul 30 '14 #5
If you look at my code, i'm using a ADODB connection to open a access database and run a query. The parameters are sent to the query, I then take the data the query provides and populate a list box in project. I think i can use the objfield.name property, but I don't know how to go about it.
Jul 30 '14 #6
zmbd
5,501 Expert Mod 4TB
Unfortunately I do not have an installation of MS-Project available; thus no way for me trouble shoot directly.

Yes I do see that how you've opened the database link.
I'm also assuming that tskdat is your listbox.

However, I think we're not using the same names for things; therefore, so that I have an idea as to how the UI for MSP is presenting the listbox control to you would you please do the following:

- In a new form (so that we do not damage any of your current work (°,°) )
- Insert a new listbox control, open the properties for the control:
-- Data tab: Please list for me the first four field names. For example:
[Data]![Control Source] = null
[Data]![Row Source]= entry1, entry2;
[Data]![Row Source Type]= Table/Query
Etc…
-- Format tab: Please list for me the first four and their settings as above; [Format]![Visible]=true, Etc…

It would be most helpful, if you would list those same properties and settings for the cotrol in your production form.
Jul 30 '14 #7

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

Similar topics

3
by: Broder | last post by:
Hi there, I am currently running into a somewhat weired problem and hope that this NG is able to help ;-) I have a table in a MSSQL Server in which there is one Column that stores a date. The...
1
by: cloverme | last post by:
Hi, I need help populating a listbox from a database on a webform. I created a an access database with a table, fields and data. Then I created a WebForm in vb.net and added a DropDownList...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
1
by: Ekoku | last post by:
Hi any1 know how to populate a listbox from a textfile using split plz 10x alot Below a code extract Private Sub LoadAlbum() With ofdAlbum .DefaultExt = "txt" .InitialDirectory =...
8
by: jack-b | last post by:
Hi, I have a list box which displays countries names and a second listbox which displays their cites (based on the selection made in ListBox 1) If the user selects USA i want to display cities...
5
JustJim
by: JustJim | last post by:
Well this has been fun, but I now elect to "phone a friend" I have an Access application in which the operator can enter/choose the name of a training course and enter the number of teachers from...
2
by: DanAusitn | last post by:
Hi forum, I have been trying to figure this out for a while now, i thought it would be quite simple but has proved difficult as i am a learner. basicly i want to populate a list or combo box with...
1
by: dkohel | last post by:
I have 2 list boxes on my form. I am trying to populate listbox B with the selection from listbox A. I have set multi-select in both boxes to Extended... The user will select the items from...
13
tuxalot
by: tuxalot | last post by:
I have a listbox with a row source using a union query: SELECT .ReportID, .ReportName, .ReportCaption, .ReportCriteriaFlags, .ReportRemote FROM ORDER BY .ReportCaption; The union query sql: TABLE...
2
by: mcalex | last post by:
Hi, I'm having trouble trying to populate a list box with data from a parameterized query. If I set the rowsource property to the query name, when the form opens I get the parameter values dialog,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.