Can someone help me?

  • Thread starter Thread starter Ted
  • Start date Start date
T

Ted

Hi all,

i'm trying to use the For Each...Next statement. the problem is the code is
in one
form and i'm looking at another. i can run the code if i hardcode the form
name but
the form will change depending on what the user chooses as a State on the
first form.

for example:

For Each ctrl In Forms![frmNJForms].Controls
If TypeOf ctrl Is CheckBox Then
If ctrl.Value = True Then
varFormName = Mid(ctrl.Name, 4, 2) & " " & Mid(ctrl.Name, 6, 4)
End If

objWord.selection.typetext varFormName
End If
Next

i don't want to hardcode Forms![frmNJForms] because it might be
Forms![frmMNForms] or Forms![frmNYForms] etc...

i tried:

Dim varStateForm as object

varStateForm = "Forms![frm" & txtInsuredState & "Forms]"
For Each ctrl In varStateForm.Controls
..
..
..
but i get "Object variable or With block variable not set"

How can i set the the frm&txtInsuredState&Forms as the object?

Thanks in Advance
Ted
 
Try this (it's aircode and not tested):

Sub WorkWithForm(strFormName as String)
Dim ctl As Control
For Each ctrl In Forms(strFormName).Controls
'rest of your code here

If you call this sub with the form name as the parameter, it should work
with the form you want:
Call WorkWithForm("frmNJForms")
 
Back
Top