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?
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?
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
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.OL EDB.4.0;Da ta 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.OL
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.OL EDB.4.0;Da ta 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
Dim adoRS as recordset
set adoConn = new Connection
set adoRS = New Recordset
adoConn.ConnectionString "Provider=Microsoft.Jet.OL
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
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.
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.
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.
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.RecordCoun t > 0 Then
Data1.Recordset.MoveLast
iRec = Data1.Recordset.RecordCoun t
Data1.Recordset.MoveFirst
For i = 0 To iRec
list1.AddItem CStr(Data1.Recordset.Field s("Employe e Number"))
Data1.Recordset.MoveNext
Next iRec
End If
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.RecordCoun
Data1.Recordset.MoveLast
iRec = Data1.Recordset.RecordCoun
Data1.Recordset.MoveFirst
For i = 0 To iRec
list1.AddItem CStr(Data1.Recordset.Field
Data1.Recordset.MoveNext
Next iRec
End If
ASKER
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.Reco rdCount > 0 Then
datEmployee.Recordset.Move Last
iRec = datEmployee.Recordset.Reco rdCount
datEmployee.Recordset.Move First
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset .Fields("E mployeeNum ber"))
datEmployee.Recordset.Move Next
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?
Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.Reco
datEmployee.Recordset.Move
iRec = datEmployee.Recordset.Reco
datEmployee.Recordset.Move
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset
datEmployee.Recordset.Move
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
Next i
ASKER
I got the following error message for the line "lstEmployeeNo.AddItem CStr(datEmployee.Recordset .Fields("E mployeeNum ber"))
", 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.Reco rdCount > 0 Then
datEmployee.Recordset.Move Last
iRec = datEmployee.Recordset.Reco rdCount
datEmployee.Recordset.Move First
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset .Fields("E mployeeNum ber"))
datEmployee.Recordset.Move Next
Next i
End If
", 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.Reco
datEmployee.Recordset.Move
iRec = datEmployee.Recordset.Reco
datEmployee.Recordset.Move
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset
datEmployee.Recordset.Move
Next i
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Reco rdCount
to:
iRec = datEmployee.Recordset.Reco rdCount - 1
-------------------------- ---------- ---------- ---
Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.Reco rdCount > 0 Then
datEmployee.Recordset.Move Last
iRec = datEmployee.Recordset.Reco rdCount - 1
datEmployee.Recordset.Move First
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset .Fields("E mployeeNum ber"))
datEmployee.Recordset.Move Next
Next i
End If
iRec = datEmployee.Recordset.Reco
to:
iRec = datEmployee.Recordset.Reco
--------------------------
Dim iRec As Integer
Dim i As Integer
datEmployee.RecordSource = "Select [EmployeeNumber] from EMPLOYEE"
datEmployee.Refresh
If datEmployee.Recordset.Reco
datEmployee.Recordset.Move
iRec = datEmployee.Recordset.Reco
datEmployee.Recordset.Move
For i = 0 To iRec
lstEmployeeNo.AddItem CStr(datEmployee.Recordset
datEmployee.Recordset.Move
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.
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.
if Recordcount > 0 then
.movefirst
do while not adoRS.EOF
list1.additem cstr(.fields("Employee Number")
.movenext
loop
end if
end with