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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each control variable ...... 1

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I have some code that runs ok in Access 2010, but coughs in Access 2007

Code:
Dim TmpKeywords() As String
Dim tblKeywords As String
Dim i As Integer
Dim sql As String

tblKeywords = "one,two,three"
TmpKeywords = Split(tblKeywords, ",")

For Each i In TmpKeywords
       sql = sql & " OR Documents Like '%" & i & "%'"
Next i

It halts on the i, giving a notice:
For each control variable on arrays must be varient

I had to import the Access 2010 into a 2007 database because the control events did not work?, looked at all references.
seemed same. Maybe a backward compatability problem? Any ideas appreciated
 
Code:
 Dim TmpKeywords() As String
 Dim tblKeywords As String
 Dim i As Integer
 Dim sql As String

 tblKeywords = "one,two,three"
 TmpKeywords = Split(tblKeywords, ",")

For i = lbound(TmpKeywords) to ubound(tmpKeywords)
   sql = sql & " OR Documents Like '%" & tempkeywords(i) & "%'"
Next i

That code most definately never worked as written.
 
Many thanks MajP, that works, just had to change tempKeywords (tmpkeywords)

It did work in Access 2010, must have been lucky. Anyway, many thanks
 
You simply had to change the type of i:
Code:
Dim TmpKeywords() As String
Dim tblKeywords As String
Dim i As [!]Variant[/!]
Dim sql As String

tblKeywords = "one,two,three"
TmpKeywords = Split(tblKeywords, ",")

For Each i In TmpKeywords
       sql = sql & " OR Documents Like '%" & i & "%'"
Next i

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. I thought that was the case and had tried it, but the same error was generated. All worked in 2010 but 2007 did not like it?
 
This may have been a typo, but I doubt you got lucky on this line
sql = sql & " OR Documents Like '%" & i & "%'"
Has to be
sql = sql & " OR Documents Like '%" & tempkeywords(i) & "%'
 
MajP, For Each iterates the valuse, not the indices.
 
OOps, sorry for the typo (valuse instead of values)
 
Domino2, FYI: the code I've posted 15 Jan 13 13:30 works as expected in my ac2007 db.
 
Strange why I had problems. I know I had the pain trying to find why events were not working (just because of trusted locations. However all working now thanks. If I can I will revert it back to see what happens now events working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top