Link to home
Start Free TrialLog in
Avatar of willwatters
willwatters

asked on

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

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?
Avatar of BWarmuskerken
BWarmuskerken

with adoRS
  if Recordcount > 0 then
    .movefirst
    do while not adoRS.EOF
      list1.additem cstr(.fields("Employee Number")
      .movenext
    loop
  end if
end with
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
in addition, why are you doing this with a Lizt Box, rather than a ComboBox.

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

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
Avatar of willwatters

ASKER

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.
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.
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
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?
make it

Next i
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
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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.
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.