Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dual loop!! Problem to increment after condition.

Status
Not open for further replies.

neiljabba

IS-IT--Management
May 22, 2003
86
0
0
GB
Hello all

I am using Word Forms to set up a form for users to fill in.(I know all about Info Path and web based options but am restricted by availability and access rights.)

Here goes the process:
The user completes a single checkbox on each row.

Once a single box( Named Scheck11, SCheck12, SCheck13 for row 1) in the row has been completed I need to move the focus for the sub down to the next row i.e. Scheck21, 22, 23 and so on through the form.

I have built in a check so that only one box can be checked and I think this is why Ive tied myself in knots for the loop. If its true it clears the box and then needs to move on it doesnt in spite of trying different loops and increment options.

Anyway heres one version of the code any ideas, I can think it but not write it:

Sub checkres()'checks the result of the ticked SCheck


For j = 1 To 2 ' for the row number in word table
For i = 1 To 3 ' for the checkbox number on the row

Let k = j 'sets k so we know which row its on to use when clearing


If FormFields("SCheck" & j & i).Result = True Then
FormFields("SCheck" & j & i).EntryMacro = "clearbox"
FormFields("SCheck" & j & i).EntryMacro = "clearbox"
FormFields("SCheck" & j & i).EntryMacro = "clearbox"



MsgBox "Please select only one box. Click OK and try again", vbInformation, "Entry error."

clearbox 'goes to macro to tidy up error

Else


End If

'clearbox


Next i
Next j

End Sub

Sub clearbox() 'counts through and clears all checkboxes on the row to resolve double entry problem

For x = 1 To 3 ' represents the checkbox on the row.

FormFields("SCheck" & k & x).Result = False ' k row value set from j in above sub checkres

Next x

checkres 'returns to results checking sub

End Sub

Many thanks in advance.

Neil
 
Radio buttons are designed for allowing only one choice out of many. You're complicating things for yourself by using sets of checkboxes rather than radio buttons.

Lee
 
Called Option buttons in VB. Put sets of related option buttons in a control array. If you need multiple sets of option buttons, put each set in a frame, and you can use them separately.

Bob
 
Cheers guys

So simple, not sure whether I mean the solution or me!!! :0


Many thanks again.

Neil
 
Hello again

Looks like I have another problem. Whilst I could alter the checkbox name by appending a variable to it I cant seem to do it with the optionbutton.

I've tried me.controls, that didnt work, but it has in the past on userforms. Is there any way I can do that?

Here's the code as it stands with j hopefully replacing the 1 at the end of the name SCheckH. Such as SCheckH & j.

Public Sub Done_Click()



For j = 1 To 3 ' boxes

If SCheckH1.Value = True Then MsgBox "Hello" Else MsgBox "No"

Next j

End Sub


Many thanks in advance.

Neil
 
Use the option buttons as an array. All the buttons in one area, where only one can be chosen, should have the same name. The one that is selected will contain the value you want to work with.

Lee
 
First thing is, when you make a control array, you'll notice that the click event has an added argument called index. That argument contains the index property of the option button that you clicked. (check the index properties of each of your option buttons, and you'll see that they are each different. If a control isn't part of an array, the index property will be blank.) Use the index property to determine which option button was pressed.
Code:
Sub myOptButton_Click(Index as integer)
select case index
   case 0
      'Code for first option button
   case 1
      'code for next option button
   case (...)
      '...
end select
End Sub
This is standard treatment for any control array. Now, if you want to actually see what the option button says, that's the caption property.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top