List Box display problem

G

Guest

I am trying to display the records related to a particular value in the drop
down box back into a list box. I used some code provided by Lee Robinson
which works for one of my forms, but this version does not work. I think I
have isolated the problem to the if then statement, but not sure how to test
it further to see what is going on. Here is the code:
-----------------------------------------------------
Private Sub dispapps()

Dim DB As Database
Dim rs As Recordset
Dim strsql As String

strsql = "SELECT tblBUS_SPEC_APPS.autBUS_SPEC_POS_ID,
tblBUS_SPEC_APPS.txtAPP_ID " & _
"FROM tblBUS_SPEC_APPS WHERE
tblBUS_SPEC_APPS.autBUS_SPEC_POS_ID=" & Me.Combo0

'MsgBox strsql

Set DB = CurrentDb
Set rs = DB.OpenRecordset(strsql)


'MsgBox rs.RecordCount
rs.MoveFirst
'MsgBox Me.List2.ListCount

'MsgBox rs.Fields("txtAPP_ID")


Do Until rs.EOF
For i = 0 To Me.List2.ListCount - 1
MsgBox "txtappid = " & rs.Fields("txtapp_id")
MsgBox "i = " & i
MsgBox "itemdata = " & Me.List2.ItemData(i)
If Me.List2.ItemData(i) = rs.Fields("txtAPP_ID").Value Then
'**problem line
MsgBox "yep, they are equal"
Me.List2.Selected(i) = True
End If
Next
rs.MoveNext
Loop

Me.Refresh
End Sub
------------------------------------------------
As you can see, I put in a lot of message boxes to display what each value
was at the time. When the Do Loop and If Then statements are cycling, I can
see where the txtAPP_id and the ItemData(i) are the same values, but I never
see the msgbox "Yep, they are equal" so the '** problem line does not appear
to be evaluating correctly. I did check and each is a value, How can I test
this better to figure out the problem, or can someone see the problem?
 
S

strive4peace

is txtAPP_ID a long integer? you are probably getting an
error because the data types are different.

try this:

If cLng(nz(Me.List2.ItemData(i),0)) = nz(rs!txtAPP_ID) then

cLng converts to long integer
nz converts null to a value (0 for numbers, empty string for
text if second argument is not specified)

don't forget to release your object variables before the End Sub

rs.close
set rs = nothing
set db = nothing

Warm Regards,
Crystal
MVP Microsoft Access

remote programming and training
strive4peace2006 at yahoo.com
*
Have an awesome day ;)
 
G

Guest

That works quite well, thank you, now I have to make sure I take out all the
message boxes, they get annoying when not necessary.

Thanks.
 
S

strive4peace

you're welcome ;) happy to help

Warm Regards,
Crystal
MVP Microsoft Access

remote programming and training
strive4peace2006 at yahoo.com
*
Have an awesome day ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top