Nested WITH statements (1 Viewer)

mredmond

Registered User.
Local time
Yesterday, 21:45
Joined
Oct 1, 2003
Messages
32
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!
 
Last edited:

chenn

Registered User.
Local time
Today, 03:45
Joined
Apr 19, 2002
Messages
69
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

Registered User.
Local time
Yesterday, 21:45
Joined
Oct 1, 2003
Messages
32
That makes sense. I'm going to try it and see what happens. Thanks for the heads up.
 

Users who are viewing this thread

Top Bottom