Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define Variable 1

Status
Not open for further replies.

CindiN

Instructor
Jan 30, 2001
98
US
I have a code, that is giving me a "Compile Error: Variable not defined". And it's highlighting "itm" in the code. I'm not familiar with VB, can someone tell me what I need to change?

Function NameList() As String
NameList = "("
For Each Itm In lstNames.ItemsSelected
NameList = ListNames & Chr(34) & lstNames.ItemData(Itm) & Chr(34) & ", "
Next
NameList = Left(NameList, Len(NameList) - 2) & ")"
If Len(NameList) = 2 Then
NameList = ""
Else
NameList = "Employee_Name In " & NameList
End If
End Function

Thanks for your help.
CindiN
 

You have to tell VB what kind of variable Itm is. In this case I believe it has to be a Variant, and the declaration

Dim Itm As Variant

needs to appear at the beginning of the function.

Hope this helps

The Missinglinq "It's got to be the going,
not the getting there that's good!"
-Harry Chapin
 
Thank You Missinglinq,

Thanks for the info, however, I just keep getting more compile errors, so there must be more wrong with it.

CindiN
 
You don't say what the other compile errors are. Apart from syntax ones, the common difficult problem is missing Reference libraries.
 
DrSimon, Thanks for the response. I was just about ready to give up on this, so I didn't get specific. I'm afraid I've gone into a zone I'm not familiar with....VB. The code written above was done by someone else. Here's the code now with some changes:

Function NameList() As String
Dim Itm As Variant
Dim ListNames As Variant
NameList = "("
For Each Itm In lstNames.ItemsSelected
NameList = ListNames & Chr(34) & lstNames.ItemData(Itm) & Chr(34) & ", "
Next
NameList = Left(NameList, Len(NameList) - 2) & ")"
If Len(NameList) = 2 Then
NameList = ""
Else
NameList = "Employee_Name In( " & NameList
End If
End Function

Private Sub Command2_Click()
DoCmd.OpenReport "FinalWorksheetRPT", acViewPreview, , NameList

End Sub

I'm told from another post that the statement...

NameList = ListNames & Chr(34) & lstNames.ItemData(Itm) & Chr(34) & ", "

is incorrect. The problem now is that when multiple names are selected from the list box, it only provides the report on the LAST name that was selected.

Can you help me with this? Please keep in mind I'm not too familiar with VB.
Thank you so much,
CindiN
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top