prabha_friend
Prabhakaran Karuppaih
- Local time
- Today, 22:46
- Joined
- Mar 22, 2009
- Messages
- 1,008
Please see the attached png for sample records.
Many Regions, Many Towers, Many Countries and all..
Has to create a worksheet for Each Region-Tower and Paste the countries' records.
Private Sub Input_Click()
Dim Mainrset As Recordset
Dim Temp As Variant
Set Mainrset = CurrentDb.OpenRecordset("Query_Form")
Mainrset.MoveLast
Mainrset.MoveFirst
ReDim Temp(0)
Set Temp(0) = CurrentDb.OpenRecordset("SELECT Region_Name FROM Regions;")
Temp(0).MoveLast
Temp(0).MoveFirst
ReDim Preserve Temp(1)
Set Temp(1) = CurrentDb.OpenRecordset("SELECT Tower_Name FROM Towers;")
Temp(1).MoveLast
Temp(1).MoveFirst
Excel.Application.Visible = True
Workbooks.Add
While Not Temp(0).EOF
While Not Temp(1).EOF
Sheets(1).Name = Temp(0)!Region_Name & "-" & Temp(1)!Tower_Name
Mainrset.Filter = "Region_Name = '" & Temp(0)!Region_Name & "' AND Tower_Name='" & Temp(1)!Tower_Name & "'"
ReDim Preserve Temp(2)
Set Temp(2) = Mainrset.OpenRecordset
Temp(2).MoveLast
Temp(2).MoveFirst
Range("A1").CopyFromRecordset (Temp(2))
Temp(1).MoveNext
Wend
Temp(0).MoveNext
Wend
End Sub
The above code is not correct as some Tower/Process are not associated with some countries. Usually What I do is to loop through all the records and look for the changes in the field. Is that the only way? (Actually I have met this scenario many times but as I had gap in my career, currently struggling to find the right way).
Many Regions, Many Towers, Many Countries and all..
Has to create a worksheet for Each Region-Tower and Paste the countries' records.
Private Sub Input_Click()
Dim Mainrset As Recordset
Dim Temp As Variant
Set Mainrset = CurrentDb.OpenRecordset("Query_Form")
Mainrset.MoveLast
Mainrset.MoveFirst
ReDim Temp(0)
Set Temp(0) = CurrentDb.OpenRecordset("SELECT Region_Name FROM Regions;")
Temp(0).MoveLast
Temp(0).MoveFirst
ReDim Preserve Temp(1)
Set Temp(1) = CurrentDb.OpenRecordset("SELECT Tower_Name FROM Towers;")
Temp(1).MoveLast
Temp(1).MoveFirst
Excel.Application.Visible = True
Workbooks.Add
While Not Temp(0).EOF
While Not Temp(1).EOF
Sheets(1).Name = Temp(0)!Region_Name & "-" & Temp(1)!Tower_Name
Mainrset.Filter = "Region_Name = '" & Temp(0)!Region_Name & "' AND Tower_Name='" & Temp(1)!Tower_Name & "'"
ReDim Preserve Temp(2)
Set Temp(2) = Mainrset.OpenRecordset
Temp(2).MoveLast
Temp(2).MoveFirst
Range("A1").CopyFromRecordset (Temp(2))
Temp(1).MoveNext
Wend
Temp(0).MoveNext
Wend
End Sub
The above code is not correct as some Tower/Process are not associated with some countries. Usually What I do is to loop through all the records and look for the changes in the field. Is that the only way? (Actually I have met this scenario many times but as I had gap in my career, currently struggling to find the right way).