1. From a data control (not preferred or recommended)
Place a data control on your form and set the databasename and recordsource properties to access your database.
Next, place a combo box on your form and set the datasource property to the data control (from a drop down list).
Next, choose the field you want displayed in the combo box by setting the datafield property.
2. From a recordset in code (recommended)
place a combo box on your form and then copy and paste the following code into the general declarations section of your form:
Private Sub Fill_Combo(cboBox As ComboBox, ByVal sTable As String, ByVal sDisplayField As String)
Dim db As Database
Dim rs As Recordset
Set db = Workspaces(0).OpenDatabase("c:\aa\newdata.mdb", False, False)
Set rs = db.OpenRecordset("SELECT DISTINCT " & sDisplayField & " FROM " & sTable & " ORDER BY " & sDisplayField, dbOpenSnapshot)
cboBox.Clear
Do Until rs.EOF
cboBox.AddItem rs(sDisplayField)
rs.MoveNext
Loop
rs.Close
Set db = Nothing
In the sub fillCombo, the first argument should be
cboBox as ComboBox
You are just specifying that the type of the thing being passed is a combo box, and then in the call to the sub you are passing the correct combo box to the routine.
If you make the following change it should work.
Simon
PS. check that in the openrecordset call, you have a space after distinct, inside the quotes, otherwise you will get other errors. [sig][/sig]
I found that I was getting that error because one of the necessary *.tlb files were not selected as a reference in my project. The progam is now accessing the database.
I was watching this discussion as I have a simillar problem and ran into the same error. In my application I am populating a spreadsheet, though. Could you tell me what references you were missing? Maybe that was my problem too.
msado20.tlb (Microsoft ActiveX Data Objects 2.0 Library)
msderun.dll (Microsoft Data Environment Instance 1.0)
If these are not the ones you need, perhaps you should skim through the sample programs(that come with VB) for an application that is similar to yours. Open it in VB and compare the references that it uses with your references.
Cooleen,
Am working on something very similar to the problem that you had last year. If you could help me out with just one line of code
Set dv = Workspaces(0)
It highlights the Workspaces and comes up with the error message
Compiler error:
Sub or function not defined
Using VB 6.0 have looked through the Object library with no reference equalling workspaces
Did you have any problems with this line or do you know the answer.
Kind regards
Henry
Hi there Cooleen,
Have just come back from Freaking out in India for the last two weeks. Can't find the copy of the VB form that I had altered based on this thread.
If you still have a copy of something fitting this thread could you send it to me
Henrymaher@yahoo.co.uk
Thanx
Henry
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.