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! - Function FillS2A(fld As Control, ID As Variant, _
-
row As Variant, col As Variant, code As Variant) _
-
As Variant
-
Static survDB As Database
-
Static rsData As Recordset
-
Static rsCourses As Recordset
-
Static strSearch As String
-
Static strField As String, strStaff As String
-
Static cntCourse As Integer, cntData As Integer, Counter As Integer
-
Static S2A_Totals() As Integer
-
Static DisplayData() As Variant
-
Dim ReturnVal As Variant
-
-
Set survDB = CurrentDb()
-
-
Set rsData = survDB.OpenRecordset("tblReturnData", dbOpenDynaset)
-
rsData.MoveLast
-
rsData.MoveFirst
-
-
Set rsCourses = survDB.OpenRecordset("tblCourses", dbOpenDynaset)
-
rsCourses.MoveLast
-
rsCourses.MoveFirst
-
-
strField = "S2A_PD_Desc"
-
strStaff = "S2A_PD_Staff"
-
ReDim S2A_Totals(rsCourses.RecordCount)
-
-
Select Case code
-
Case acLBInitialize
-
ReDim DisplayData(rsCourses.RecordCount, 1)
-
For cntCourse = 0 To rsCourses.RecordCount - 1
-
rsCourses.AbsolutePosition = cntCourse
-
For cntData = 0 To rsData.RecordCount - 1
-
rsData.AbsolutePosition = cntData
-
For Counter = 1 To 4
-
If rsData.Fields(strField & Format(Counter, "@")) = rsCourses!course Then
-
S2A_Totals(cntCourse) = S2A_Totals(cntCourse) + rsData.Fields(strStaff & Format(Counter, "@"))
-
DisplayData(cntCourse, 0) = rsData.Fields(strField & Format(Counter, "@"))
-
DisplayData(cntCourse, 1) = S2A_Totals(cntCourse)
-
End If
-
Next Counter
-
Next cntData
-
Next cntCourse
-
-
ReturnVal = True
-
-
Case acLBOpen
-
ReturnVal = Timer
-
-
Case acLBGetRowCount
-
ReturnVal = rsCourses.RecordCount
-
-
Case acLBGetColumnCount
-
ReturnVal = 2
-
-
Case acLBGetColumnWidth
-
' -1 uses default, other values in twips (567 per cm)
-
ReturnVal = -1
-
-
Case acLBGetValue
-
returnvalue = DisplayData(row, col)
-
-
End Select
-
-
FillS2A = ReturnVal
-
-
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
5 2742
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
- For future reference, please indent your code. In its present state, it is very difficult to read and even harder to decipher.
- 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.
- 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!
- 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:
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. - Now that I have thoroughly confused you, change Line #61 to:
- ReturnVal = DisplayData(row, col)
- Let me know how you make out.
- For future reference, please indent your code. In its present state, it is very difficult to read and even harder to decipher.
- 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.
- Let's see if we can eliminate the most obvious problem first.
- 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:
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. - Now that I have thoroughly confused you, change Line #61 to:
- ReturnVal = DisplayData(row, col)
- Let me know how you make out.
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.
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.
- Use [ and ] instead of braces { } when using your Code Tags, the rest is correct.
- 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |