473,385 Members | 1,622 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,385 software developers and data experts.

Populating a listbox from a function

JustJim
407 Expert 256MB
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 a particular school who have attended that course.

They can do this in four sets of combo box (for course name) and text box (number attended) and then move to the next record (another school's data) and do it all again.

By looping through the schools data table and the course title table, I can add up how many teachers in the whole system have attended each course. Easy!

Now I want to display that data and I thought that a listbox on a form would suit. I wrote the following function to be used as the listbox's Row Source Type and when the form is opened, the function is called several times and returns data for acLBInitialize, acLBOpen etc. The problem is that Access never asks the function for acLBGetValue so my poor lonely listbox stays empty!

Expand|Select|Wrap|Line Numbers
  1. Function FillS2A(fld As Control, ID As Variant, _
  2. row As Variant, col As Variant, code As Variant) _
  3. As Variant
  4. Static survDB As Database
  5. Static rsData As Recordset
  6. Static rsCourses As Recordset
  7. Static strSearch As String
  8. Static strField As String, strStaff As String
  9. Static cntCourse As Integer, cntData As Integer, Counter As Integer
  10. Static S2A_Totals() As Integer
  11. Static DisplayData() As Variant
  12. Dim ReturnVal As Variant
  13.  
  14. Set survDB = CurrentDb()
  15.  
  16. Set rsData = survDB.OpenRecordset("tblReturnData", dbOpenDynaset)
  17. rsData.MoveLast
  18. rsData.MoveFirst
  19.  
  20. Set rsCourses = survDB.OpenRecordset("tblCourses", dbOpenDynaset)
  21. rsCourses.MoveLast
  22. rsCourses.MoveFirst
  23.  
  24. strField = "S2A_PD_Desc"
  25. strStaff = "S2A_PD_Staff"
  26. ReDim S2A_Totals(rsCourses.RecordCount)
  27.  
  28. Select Case code
  29. Case acLBInitialize
  30. ReDim DisplayData(rsCourses.RecordCount, 1)
  31. For cntCourse = 0 To rsCourses.RecordCount - 1
  32. rsCourses.AbsolutePosition = cntCourse
  33. For cntData = 0 To rsData.RecordCount - 1
  34. rsData.AbsolutePosition = cntData
  35. For Counter = 1 To 4
  36. If rsData.Fields(strField & Format(Counter, "@")) = rsCourses!course Then
  37. S2A_Totals(cntCourse) = S2A_Totals(cntCourse) + rsData.Fields(strStaff & Format(Counter, "@"))
  38. DisplayData(cntCourse, 0) = rsData.Fields(strField & Format(Counter, "@"))
  39. DisplayData(cntCourse, 1) = S2A_Totals(cntCourse)
  40. End If
  41. Next Counter
  42. Next cntData
  43. Next cntCourse
  44.  
  45. ReturnVal = True
  46.  
  47. Case acLBOpen
  48. ReturnVal = Timer
  49.  
  50. Case acLBGetRowCount
  51. ReturnVal = rsCourses.RecordCount
  52.  
  53. Case acLBGetColumnCount
  54. ReturnVal = 2
  55.  
  56. Case acLBGetColumnWidth
  57. ' -1 uses default, other values in twips (567 per cm)
  58. ReturnVal = -1
  59.  
  60. Case acLBGetValue
  61. returnvalue = DisplayData(row, col)
  62.  
  63. End Select
  64.  
  65. FillS2A = ReturnVal
  66.  
  67. End Function
Edit: Hmm don't think I did the code tag thing properly
Any hints, pointers or straight-out help will be appreciated.

Jim
Jun 1 '07 #1
5 2742
MMcCarthy
14,534 Expert Mod 8TB
Firstly you don't need to include the =vb in the closing tag

Can you post the code you are using to pass this return to the Row Source of the list box
Jun 1 '07 #2
ADezii
8,834 Expert 8TB
  1. For future reference, please indent your code. In its present state, it is very difficult to read and even harder to decipher.
  2. You've selected the most efficient mechanism for populating a List Box, (a list filling Callback Function), but unfortunately the most com[plicated. Access allows you to supply a Function that tells it about the List or Combo Box you want displayed and has virtually no limitations. In order for Access to communicate with your Function, it does however, require a very specific interface.
  3. Let's see if we can eliminate the most obvious problem first.
    The problem is that Access never asks the function for acLBGetValue so my poor lonely listbox stays empty!
  4. Access does request, and receives the acLBGetValue but the problem seems to be that you are assigning this value to the wrong Variable. In Line #12 your Declaration was:
    Expand|Select|Wrap|Line Numbers
    1. Dim ReturnVal As Variant
    but in Line #61, you are attempting to assign a value to the Variable returnvalue which was never even Declared and in Line #65, you are setting your Function equal to ReturnVal NOT returnvalue.
    Expand|Select|Wrap|Line Numbers
    1. FillS2A = returnVal
  5. Now that I have thoroughly confused you, change Line #61 to:
    Expand|Select|Wrap|Line Numbers
    1. ReturnVal = DisplayData(row, col)
  6. Let me know how you make out.
Jun 1 '07 #3
ADezii
8,834 Expert 8TB
  1. For future reference, please indent your code. In its present state, it is very difficult to read and even harder to decipher.
  2. You've selected the most efficient mechanism for populating a List Box, (a list filling Callback Function), but unfortunately the most complicated. Access allows you to supply a Function that tells it about the List or Combo Box you want displayed and has virtually no limitations. In order for Access to communicate with your Function, it does however, require a very specific interface.
  3. Let's see if we can eliminate the most obvious problem first.
  4. Access does request, and receives the acLBGetValue but the problem seems to be that you are assigning this value to the wrong Variable. In Line #12 your Declaration was:
    Expand|Select|Wrap|Line Numbers
    1. Dim ReturnVal As Variant
    but in Line #61, you are attempting to assign a value to the Variable returnvalue which was never even Declared and in Line #65, you are setting your Function equal to ReturnVal NOT returnvalue.
    Expand|Select|Wrap|Line Numbers
    1. FillS2A = returnVal
  5. Now that I have thoroughly confused you, change Line #61 to:
    Expand|Select|Wrap|Line Numbers
    1. ReturnVal = DisplayData(row, col)
  6. Let me know how you make out.
Jun 1 '07 #4
JustJim
407 Expert 256MB
Boy do I feel silly!! returnvalue instead of ReturnVal???? I should have noticed that the parser didn't capitalise the R and the V for me. Thanks for your help, sorry for the late reply but I was away for the weekend.

I do indent my code by the way, otherwise I couldn't read it either! That was my first go with the new {Code=vb} thingy and I guess I didn't get it right. So you don't need it on the closing {/Code} marker, is that right?


Thanks again for the prompt and helpful advice.
Jun 3 '07 #5
ADezii
8,834 Expert 8TB
Boy do I feel silly!! returnvalue instead of ReturnVal???? I should have noticed that the parser didn't capitalise the R and the V for me. Thanks for your help, sorry for the late reply but I was away for the weekend.

I do indent my code by the way, otherwise I couldn't read it either! That was my first go with the new {Code=vb} thingy and I guess I didn't get it right. So you don't need it on the closing {/Code} marker, is that right?


Thanks again for the prompt and helpful advice.
  1. Use [ and ] instead of braces { } when using your Code Tags, the rest is correct.
  2. Glad to help. Just a little advice if you are interested - this situation could have been completely eliminated before and also in the future by using Option Explicit which forces you to explicitly declare all Variables. You can easily set this Option via: in the VBA Environment ==> Tools ==> Options ==> Editor Tab ==> Require Variable Declaration. If you had this Option set previously, it would have recognized returnval as an Undeclared Variable. Not using Option Explicit, allows you to 'implicitly' declare a Variable (Variant) as soon as you assign a value or refer to it.
Jun 4 '07 #6

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...
0
by: Bill Brinkworth | last post by:
I want the user to type in part of a word, and I want to return all words from an Access word table that contains specified letters. If they were to type "fe??", it should return all words in the...
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
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...
6
by: P K | last post by:
I have a listbox which I am populating on the client (it contains a list of dates selected from calender). The listbox is a server control. When I get to the server after postback by selecting an...
1
by: CraigMuckleston | last post by:
I have the following php code that retrieves a list of items. I want to populate a listbox with the XMLHttpRequestObject, but I am getting 1 long string instead of each item on a line. How can I...
2
by: NvrBst | last post by:
I populate a ListBox with a LogFile that has about (~1000 lines). The ListBox's datasource is a BindingList<string>. Whenever I add the elements, with the datasource set, it takes about 2 mins. ...
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...
0
by: Phijo | last post by:
Hello, I'm a Java developer but brand new to ASP (i have done some of the tutorials) so I have some ASP programming background however I cannot find the cause or the fix to this problem. I am...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.