Hi Bodisathva
I used your suggestion about arrays and it worked...
The result is not elegant, but as I said, it works. - Thanks
If you are interested this is what I'm trying to do...
I'm writing a maintenance program and part of it is to transfer data from an equipment hierarchy of seven tables to an equipment details table. I've already sorted out how to do it from individual tables. But to do it in bulk, I've used the following code....
Option Compare Database
Option Explicit
Option Base 1
Public Function EquipmentTransfer(tblName As String)
Dim db As Database
Dim strSQL As String
Dim iLevel As Integer
Dim iLevelSize(7) As Integer
Dim x As Integer
gsGetDefaults
Set db = CurrentDb
iLevel = Right(tblName, 1)
For x = 1 To 7
If x = 1 Then iLevelSize(x) = gintE1
If x = 2 Then iLevelSize(x) = gintE2
If x = 3 Then iLevelSize(x) = gintE3
If x = 4 Then iLevelSize(x) = gintE4
If x = 5 Then iLevelSize(x) = gintE5
If x = 6 Then iLevelSize(x) = gintE6
If x = 7 Then iLevelSize(x) = gintE7
Next x
' Create First Query - qry_TransferEquipment
strSQL = "SELECT Mid(Location,4,50) & " & "'" & gstrSeparator & "' & "
For x = 1 To iLevel
strSQL = strSQL & "RightPad(" & "E" & x & "No" & ", ' '," & iLevelSize(x) & ")" & " & '" & gstrSeparator & "' & "
Next x
' Remove the last comma
strSQL = Left(strSQL, Len(strSQL) - 8)
strSQL = strSQL & "AS MaintLink"
For x = 1 To iLevel
strSQL = strSQL & ", " & "E" & x & "No"
Next x
strSQL = strSQL & ", E" & x - 1 & "Desc, Location"
strSQL = strSQL & " FROM " & tblName
If Not pfCreateQuery(db, "qry_TransferEquipment", strSQL) Then
MsgBox "Error Creating Query"
Exit Function
End If
' Create the Second Query - qry_TransferEquipment2
strSQL = "SELECT Location"
For x = 1 To iLevel
strSQL = strSQL & ", " & "E" & x & "No"
Next x
strSQL = strSQL & ", E" & x - 1 & "Desc, Date() AS EnterDate, fOSUserName() AS UID, MaintLink "
strSQL = strSQL & "FROM qry_TransferEquipment LEFT JOIN tbl_EquipmentDetails ON qry_TransferEquipment.MaintLink = tbl_EquipmentDetails.eqMaintLink "
strSQL = strSQL & "WHERE tbl_EquipmentDetails.eqID Is Null"
If Not pfCreateQuery(db, "qry_TransferEquipment2", strSQL) Then
MsgBox "Error Creating Query"
Exit Function
Else
' Run the equipment transfer code
End If
End Function
As I say it's not elegant but it works.
Thanks again for your help
Peter