?
Solved

Putting a database field values in a list of a list box

Posted on 2003-03-04
16
Medium Priority
?
136 Views
Last Modified: 2010-05-01
How do I put the values of a field (in a database) as a list on A list box, for that field?

For example in a customer form, which has an employee number field as a list box. How do I put all the values of the employee number (stored in an employee table database field, employee number) in the list of the list box of the employee number in the customer form?
0
Comment
Question by:willwatters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
16 Comments
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8065794
with adoRS
  if Recordcount > 0 then
    .movefirst
    do while not adoRS.EOF
      list1.additem cstr(.fields("Employee Number")
      .movenext
    loop
  end if
end with
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8065798
are you using a Data Control on your form, to connect to the Table in the Database, or are your going to d the data connection entirely in code?

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8065814
in addition, why are you doing this with a Lizt Box, rather than a ComboBox.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8065829
Oops, add this to the top

Dim adoConn as Connection
Dim adoRS as recordset

set adoConn = new Connection
set adoRS = New Recordset

adoConn.ConnectionString "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";Persist Security Info=False"

adoConn.Open

adoRS.Open "Select [Employee Number] from Employee", adoConn , adOpenKeyset, adLockOptimistic

0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8065850
Dim adoConn as Connection
Dim adoRS as recordset

set adoConn = new Connection
set adoRS = New Recordset

adoConn.ConnectionString "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";Persist Security Info=False"

adoConn.Open

adoRS.Open "Select [Employee Number] from Employee", adoConn , adOpenKeyset, adLockOptimistic

with adoRS
 if Recordcount > 0 then
   .movefirst
   do while not adoRS.EOF
     list1.additem cstr(.fields("Employee Number"))
     .movenext
   loop
 end if
end with

adoRS.close
adoConn.close

set adoRS = nothing
set adoConn = nothing
0
 

Author Comment

by:willwatters
ID: 8103665
The following code did not work as I got the following error message:

Compile error: invalid use of new key word.

The problem lies with the code below:

Set adoConn = New Connection

can you resolve this or do you know a different way round this. I'm using a data control on my form.
0
 

Author Comment

by:willwatters
ID: 8103760
The following code did not work as I got the following error message:

Compile error: invalid use of new key word.

The problem lies with the code below:

Set adoConn = New Connection

can you resolve this or do you know a different way round this. I'm using a data control on my form.
0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8103762
I don't normally use data controls.  If you add a Reference to the Microsoft ActiveX Data Objects 2.x, the code will work.

Or here is code for the Data Control

Dim iRec As Integer
Dim i As Integer
Data1.RecordSource = "Select [Employee Number] from Employee"
Data1.Refresh
If Data1.Recordset.RecordCount > 0 Then
  Data1.Recordset.MoveLast
  iRec = Data1.Recordset.RecordCount
  Data1.Recordset.MoveFirst
  For i = 0 To iRec
    list1.AddItem CStr(Data1.Recordset.Fields("Employee Number"))
    Data1.Recordset.MoveNext
  Next iRec
End If
0
 

Author Comment

by:willwatters
ID: 8106772
I tried the above code which was modified for my application (shown below).

Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.RecordCount > 0 Then
 datEmployee.Recordset.MoveLast
 iRec = datEmployee.Recordset.RecordCount
 datEmployee.Recordset.MoveFirst
 For i = 0 To iRec
   lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
   datEmployee.Recordset.MoveNext
 Next iRec
End If

I got the following error:
Compile error:
Invalid next control variable reference

for the "Next iRec" part of the code. Do you know how to resolve this.  If not I could add a Reference to the Microsoft ActiveX Data Objects 2.x for the other code. But how do I do this?
0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8107115
make it

Next i
0
 

Author Comment

by:willwatters
ID: 8119221
I got the following error message for the line "lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
", using the code below:

Got  error message:
Run time error ‘3021’:
No current record

Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.RecordCount > 0 Then
datEmployee.Recordset.MoveLast
iRec = datEmployee.Recordset.RecordCount
datEmployee.Recordset.MoveFirst
For i = 0 To iRec
     lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
     datEmployee.Recordset.MoveNext
Next i
End If
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 80 total points
ID: 8119269
change this block :

For i = 0 To iRec
  lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
  datEmployee.Recordset.MoveNext
Next iRec

to

DO while Not datEmployees.Recordset.EOF
  lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
  datEmployee.Recordset.MoveNext
Loop

That way you do not need to get the recordcount value, you simple loop through the recordset until you get to the End.

This could also be written as:

DO Until datEmployees.Recordset.EOF
  lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
  datEmployee.Recordset.MoveNext
Loop

The two versions are 100% equivalent.

Arthur Wood
0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8119455
Yes, but its much much slower to do it that way.  Every time the loop increments it has to check to see if its at the end of the database.  If your looking for speed, and in VB we all are, you're better off checking once, and then using the value in a loop.
0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8122104
Change this line from:
iRec = datEmployee.Recordset.RecordCount

to:
iRec = datEmployee.Recordset.RecordCount - 1

-------------------------------------------------

Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.RecordCount > 0 Then
datEmployee.Recordset.MoveLast
iRec = datEmployee.Recordset.RecordCount - 1
datEmployee.Recordset.MoveFirst
For i = 0 To iRec
    lstEmployeeNo.AddItem CStr(datEmployee.Recordset.Fields("EmployeeNumber"))
    datEmployee.Recordset.MoveNext
Next i
End If
0
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8255230
All I have to say is that the solution you accepted will be slower.  You are checking the record set each and every time the loop starts to see if you are at the end of the record set.  You are much better off getting the record count and using it for the loop.

I have to say I'm suprised my answer was not the one accepted.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8258673
the EOF check is almoast trivial, and ANY performance loss will be negligible unless you are talking about 10,000s of records, and that many should NEVER be added to a listbox, in any case.  For 100 records, if there is a 'delay', it would be in the milliseconds, at worst.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question