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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox selection dilema 1

Status
Not open for further replies.

stibbetts

Technical User
May 6, 2004
64
US
so, this program that i am writing is coming into the home stretch, my problem is that i cant get it to cycle through checkboxes. i have created checkboxes at runtime and names them, big supprise, checkbox1, checkbox2, checkbox3,...checkbox-N. i have a loop inside a loop that determines what each checkbox references, i cant get the checkboxes to cycle though, the loop gos something like this:

public sub parse()

do while repx < (integer variable)
repy = 0
do while repy < (integer variable)
if checkbox = true then ' this is where the checkbox's need to cycle
' do the required operations
repy=repy+1
loop
repx=repx+1
loop
msgbox "operation complete"
end sub

sorry if some syntax arent quite right but i hope you get the general idea. there is one checkbox for each combination of rex and repy meaning there are (repx * repy) boxes named in order 1 through N. anyone know how to cycle through those boxes?
 
What's your form called? 'Me' means 'this form', but if the code's running from somewhere else 'this form' might not make any sense. You can refer to the form explicitly by name, so if it's called 'frmCheckBoxes' your code would read like this:

Code:
If frmCheckBoxes.Controls("checkbox" & (repx * repy)).Value = True Then

Does that help?

Nelviticus
 
this just doesnt seem to be working, can anyone tell me anything about the checkbox array that i've heard mentioned? how is it created and how is it referenced?
 
What doesn't work, do you get an error message? If repx=3 and repy=2 then the line

Code:
If frmCheckBoxes.Controls("checkbox" & (repx * repy)).Value = True Then

means "if the value of the control named 'checkbox6' on the form named 'frmCheckBoxes' is 'True' then".

I suspect one of two things: a) autodesk inventor doesn't implement VBA properly, or b) you have a logic error in your code, so 'repx * repy' is sometimes giving values for check boxes that don't exist.

In the code in your first post, if 'integer variable' is 4 and repx starts at zero then the sequence of numbers you will generate (i.e. the product of repx and repy) will be 0, 0, 0, 0, 3, 6, 9. In order for it to work you'd therefore need to have at least four checkbox controls on your form with names of 'checkbox0', 'checkbox3', 'checkbox6' and 'checkbox9'. Put a 'Msgbox repx * repy' line in there instead of the 'If...' line to test this.

If your code logic is fine then I'm stumped.

Nelviticus
 
i have tested the code that names the checkboxes and it works beautifuly i get sequencial checkboxes starting at 1 and counting right through 64, i just get an error claiming that the checkbox doesnt exist when i call for it using("checkbox" & x) but if i where ot call checkbox5 it works. i think im going to cry :-(, or atleast get really frustrated and go home, it is a beautiful day
 
OK, try something a bit more basic - if you just have the line:

Code:
If frmCheckBoxes.Controls("checkbox1").Value = True Then

does that give you an error? Remember to replace 'frmCheckBoxes' with the name of your form and replace 'checkbox1' with the name of a checkbox that you know is there (apologies if I'm stating the obvious).

If this gives you an error then it sounds as if autodesk inventor doesn't implement VBA properly, in which case you'll have to try a more fiddly route. Below is a function that runs through all of the controls on the form until it finds one that matches the control you're after then returns its value, True or False. You call it like this:

Code:
If GetCheck("checkbox" & i) = True Then

where 'i' is the number of the checkbox you want. Here's the function:

Code:
Public Function GetCheck(strControl As String) As Boolean
    
    ' Looks for control named strControl and returns its boolean value
    
    Dim blnResult As Boolean    ' The result to return
    Dim blnFound As Boolean     ' Did we find the control?
    Dim objForm As UserForm     ' The userform to search
    Dim objControl As Control   ' The control we're looking at
    
    ' Replace 'UserForm1' with the name of your form
    Set objForm = UserForm1
    blnResult = False
    blnFound = False
    
    ' Loop through all the controls
    For Each objControl In objForm.Controls
        
        ' If the names match, set values and stop looking
        If objControl.Name = strControl Then
            
            blnResult = objControl.Value
            blnFound = True
            Exit For
            
        End If
        
    Next
    
    If blnFound Then
        
        GetCheck = blnResult
        
    Else
        
        MsgBox "Control '" & strControl & "' not found"
        GetCheck = False
        
    End If
    
    ' Tidy up
    Set objForm = Nothing
    Set objControl = Nothing
    
End Function

Note, this doesn't include any error checking code (bad Nelviticus!) and will give you an error if you try to get the value of a control that exists but doesn't have a 'Value' property.

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top