View Full Version : Nested WITH statements


mredmond
10-08-2003, 10:51 AM
Can WITH statements be nested?

example

dim rst as Recordset
dim btn as CommandButton
dim i as integer

With rst
If .RecordCount > 0 Then
.MoveFirst
Do Until .EOF
for i = 1 to 4
With btn
.Name = "Button" & i
.Visible = true
End With
next
.MoveNext
Loop
End If
End With

I have a form with a Tabstrip object, an unbound subform object and a series of buttons. Based on the TAB selected in the tabstrip, a particular set of data is displayed in the subform area and the buttons take on certain values and attributes. It's all working great when I code it directly, but I am trying to clean up the code and streamline the process. I have placed all the button data into a table which I filter down to get the necessary records. I then want to loop through all the records (hence the first WITH and DO-LOOP) and then loop through all the buttons, setting the various attributes and values (hence the need for the 2nd WITH and the FOR-NEXT loop), but I am not sure if the WITH can be nested like this. How will ACCESS know which WITH it is working with (trying saying that 10 times real fast!)

I haven't actually tried it yet. The specific code is more complicated than what is shown and I didn't want to get into it unless it has a chance of working.

Thanks for the help!

chenn
10-08-2003, 01:02 PM
You can nest withs, however only the most current with is referenced when the code incounters a '.' (dot). Such as:

With rstRecordset
Do until .EOF 'references the recordset object
with objExcel
.Range("A1").select 'references the excel object
.MoveNext 'fails because excel doesn't have a move next command

end with
loop
end with

mredmond
10-08-2003, 01:06 PM
That makes sense. I'm going to try it and see what happens. Thanks for the heads up.