|
-
Oct 5th, 2000, 01:55 PM
#1
Thread Starter
Frenzied Member
Hi,
I'm trying to create a little function which will check to see if the user has selected an option in an option array.
The idea is that I can call the function such as:
Code:
If Not (ChkSel) Then
...
The ChkSel function would then loop through the option array to determine if any value is true therefore setting ChkSel to True.
The code would be:
Code:
Private Function ChkSel()
Dim i as Integer
ChkSel = False
For i = 0 to optSelect.Count - 1
If optSelect(i).Value = True Then _
ChkSel = True
Next i
End Function
The above works just great. However, what I'm trying to accomplish now is to make it so that the above code can take a parameter in order to check various option arrays, not just one. So I thought I could do the following:
Code:
Private Function ChkSel(Control as String)
Dim i as Integer
For i = 0 to Control.Count - 1
If Control(i).Value = True Then _
ChkSel = True
Next i
End Function
The idea would be to call ChkSel and indicate which option array I want it to check such as: ChkSel(optSelect)
But, the above doesn't seem to work as it gives me some errors.. I must be missing something...
Any help would be appreciated.. (By the way, don't suggest using flags in the click event of the array because I don't want to do that for various reason.. I just want to make the above code work..)
Thanks,
Dan
-
Oct 5th, 2000, 01:57 PM
#2
Guru
Almost there! 
Replace this line:
Code:
Private Function ChkSel(Control As String)
With this:
Code:
Private Function ChkSel(Control As Object)
[Edited by Yonatan on 10-05-2000 at 03:00 PM]
-
Oct 5th, 2000, 02:07 PM
#3
Thread Starter
Frenzied Member
Thanks! Worked like a charm.. I knew it had to be something really stupid, and it was...
-
Oct 5th, 2000, 02:22 PM
#4
Thread Starter
Frenzied Member
Hi,
Expanding further on the above code, how would I use it in a Module?
How would the module know which form the control was on that I'm trying to check?
Thanks,
Dan
-
Oct 5th, 2000, 02:30 PM
#5
Frenzied Member
the function should take in a form as well so add (frmForm as Form)
to the parameter
and when you pass in
pass ME as form name or the proper name of the form
and make sure you make the function PUBLIC not private..
-
Oct 5th, 2000, 02:35 PM
#6
Fanatic Member
This is accomplished in much the same way:
Code:
Public Function ChkSel(Control as Object, pForm as Form) As Boolean
Dim i as Integer
For i = 0 to pForm.Control.Count - 1
If pForm.Control(i).Value = True Then _
ChkSel = True
Next i
End Function
You call the function thusly:
If Not(ChkSel(Control,Me)) Then ...
Hope this helps you out.
-
Oct 5th, 2000, 03:04 PM
#7
Thread Starter
Frenzied Member
Thanks, but I get the following error:
"Object doesn't support this property or method."
and it highlights:
For i = 0 To pForm.Control.Count - 1
when I click on DeBug...
Any ideas?
Here is the exact code I used:
Code:
Public Function ChkSel(Control As Object, pForm As Form)
Dim i As Integer
ChkSel = False
'-- check if user selected option
For i = 0 To pForm.Control.Count - 1
If (pForm.Control(i)) Then _
ChkSel = True
Next i
End Function
And I called it via:
Code:
If Not (ChkSel(optFbOptionsEach, Me)) Then
...
Thanks,
Dan
-
Oct 5th, 2000, 03:36 PM
#8
Guru
You can't pass a Form object to a function in a module. But, you can just use another Object instead! 
Replace this line:
Code:
Public Function ChkSel(Control As Object, pForm As Form)
With this line:
Code:
Public Function ChkSel(Control As Object, pForm As Object)
-
Oct 5th, 2000, 04:00 PM
#9
Thread Starter
Frenzied Member
Thanks, but that doesn't seem to work either.. I tried calling both the actual name of the form and Me but it gives me the same error..
Any ideas? I was hoping that we could make this work so that I could use it in my standard module library to use in other projects.. I didn't want to have to explicity call out the form name within the Function. I was hoping to just pass the name as an argument but we can't seem to get it to work..
Thanks,
Dan
-
Oct 5th, 2000, 04:04 PM
#10
Frenzied Member
what yonatan said should work
whats the error your getting?
did you forget to make it public?
if you post the error it will help us help you
thanks
-
Oct 5th, 2000, 04:18 PM
#11
Guru
Are you sure you're passing a valid Form object as the pForm parameter, and a valid control array as the Control parameter?
Because if that's not the problem, then VB is freaking out.
Oh well, it freaks out 24/7 anyway.
-
Oct 5th, 2000, 04:36 PM
#12
Thread Starter
Frenzied Member
Okay, here's the deal.. I started a new project just to eliminate any possibilities..
Here is the code I placed in Module1:
Code:
Public Function ChkSel(Control, pForm As Object) As Boolean
Dim i As Integer
For i = 0 To pForm.Control.Count - 1
If pForm.Control(i).Value = True Then _
ChkSel = True
Next i
End Function
I called it from a command button on Form1 with:
Code:
Private Sub Command1_Click()
MsgBox ChkSel(Option1, Me)
End Sub
The error I get is as follows:
"Run-time error '438'
Object doesn't support this property or method"
And when I hit Debug, it highlights:
Code:
For i = 0 To pForm.Control.Count - 1
Any help would be appreciated..
Dan
-
Oct 5th, 2000, 04:45 PM
#13
Guru
Is Option1 an array?
Huh? Is it? Isn't it? See? 
Change its Index to some value (any value).
-
Oct 5th, 2000, 04:48 PM
#14
Addicted Member
did you try
MsgBox ChkSel(Option1, frmMain)
that might be it some times i think it interfers with d=stuff but i dont know much of vb just soem to make teh stuff i need to make so i dont know just trying to help
-
Oct 5th, 2000, 07:29 PM
#15
Thread Starter
Frenzied Member
Sorry, I should've been more specific.. Yes, I placed 1 option control on the form and then copied it and pasted it 2 times which created an array of 3 option controls..
And yes I did also try, calling the form by it's name as well as "Me".. But nothing seems to have worked.. Have you actually tried it? I understand in theory that it should work but there is obviously something getting in the middle of the code to make it error out.. I'm curious if you're able to make it work..
Let me know..
Dan
-
Oct 5th, 2000, 07:53 PM
#16
Hyperactive Member
Here you go...
Seems that a few people are confused about controls arrays,
controls and what to pass in general...
Have a try with this lot. I hope you will agree to use the
sample code ChkSel3 or ChkSel4 as the other ones are not
recommended if genercity is what you want.
Cheers
Code:
Option Explicit
Public Function ChkSel(pForm As Object, Control As String) As Boolean
'example passing a form and a controlarray's name
Dim i As Integer
' very bad design because it requires that each
' control in the array is sequential and starts at 0
' try it on an array where the first control has an index of 10
For i = 0 To pForm.Controls(Control).Count - 1
ChkSel = pForm.Controls(Control)(i).Value
If ChkSel Then Exit Function
Next i
End Function
Public Function ChkSel2(ControlArray As Object) As Boolean
' example passing a controlArray using Array style handling
' you don't need to worry about the form since it's a control array
Dim c As Integer
' very bad design because it requires that each
' control in the array is sequential and starts at 0
' try it on an array where the first control has an index of 10
For c = 0 To ControlArray.Count - 1
ChkSel2 = ControlArray(c).Value
If ChkSel2 Then Exit Function
Next c
End Function
Public Function ChkSel3(ControlArray As Object) As Boolean
' example passing a controlArray using Collection style handling
' you don't need to worry about the form since it's a control array
Dim myControl As Control
For Each myControl In ControlArray
ChkSel3 = myControl.Value
If ChkSel3 Then Exit Function
Next
End Function
Public Function ChkSel4(ControlArray As Object, ByRef theIndex As Integer) As Boolean
' example passing a controlArray using Collection style handling
' you don't need to worry about the form since it's a control array
Dim myControl As Control
' in this example we also set the value of the theIndex variable to the
' actual one that was selected. In a way, we are returning two answers
' to the caller which can be handy.
For Each myControl In ControlArray
ChkSel4 = myControl.Value
If ChkSel4 Then
theIndex = myControl.Index
Exit Function
End If
Next
End Function
-
Oct 5th, 2000, 08:02 PM
#17
Hyperactive Member
Paul Lewis,
Sory to tell you that it will never works in general because not every control within a form will have property Value. And that what is triggersd "Invalid use of property mentioned before".
Second of all ......
Why does anyone need a function in this situation at all?
-
Oct 6th, 2000, 12:23 AM
#18
Hyperactive Member
LG, I was responding to the original post as quoted below:
I'm trying to create a little function which will check to
see if the user has selected an option in an option array.
So I think I am safe because as far as I know, Option
Buttons always have the .Value property.
All I was demonstrating is that the method being used to
address the individual elements of the control array was
not safe. It was a given fact that the control array was
going to be of Option buttons 
Nice to know you took a look at the samples though 
Cheers
-
Oct 6th, 2000, 06:37 AM
#19
transcendental analytic
What? I thought everybody knew you could pass a control array´!
And why the h*ck:
pForm.Control(i)
??
you put form item as an object you passed to a function? WHOAA!
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 6th, 2000, 02:24 PM
#20
Hyperactive Member
Nothing wrong with the idea...
Just I think some of the early advice in the thread led
dbassettt74 down the wrong path.
My 4 examples were attempting to demonstrate the difference
between a bad way of doing it and a good way.
In my own programming, I have passed a form around - most
usually when classes have a "Canvas" member. This way my
class can draw itself (or print or whatever it happens to
be) on the form generically.
Regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|