Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-04
16
Medium Priority
?
138 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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
Course of the Month13 days, 1 hour left to enroll

579 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