Pass form valued argument to module

NeuronRider

Registered User.
Local time
Today, 07:31
Joined
Mar 8, 2005
Messages
14
I'm guessing this is a pretty dumb question, but I'm getting a data type mismatch when trying to pass my argument from a form to my module subroutine.

Here's the subroutine (which is likely not very efficient, nor complete):
Code:
Public Sub AssignQA(objForm As Form)
    Dim intI, intP, x, n As Integer
    Dim QOrder(18) As Integer
    Dim varGrade As Variant
    
'1. Randomly assign picture order
    intI = 1
    x = RandomNumbers(2, , 2)
    For n = LBound(x) To UBound(x)
        If x(n) = 1 Then
            objForm.Controls("tab_Pic" & intI).Caption = "Summer Picture"
        ElseIf x(n) = 2 Then
                objForm.Controls("tab_Pic" & intI).Caption = "Winter Picture"
        intI = intI + 1
        End If
    Next n

RandomNumbers is a function that just returns an array of random values, with the two arguments used above defining the max value, and the number of array elements.

Now, the function is called in the On Activate event within my form, frm_AssessmentTask, as follows:

Code:
Private Sub Form_Activate()
    DoCmd.Maximize
    AssignQA (Forms!frm_AssessmentTask)
End Sub

I've also tried referencing the argument as Forms("frm_AssessmentTask"), and as just frm_AssessmentTask (resulting in a "requires object error" indicating to me that I'm going the wrong way).

Fairly new territory for me. Any suggestions would be appreciated, as my frustration is mounting with this subroutine.

-NR
 
NR,

You can call it like: AssignQA (Me.Form)

That will obviously pass the Current form to AssignQA.

If the form is not open, you'll need to declare a form object and assign
the desired form to it when YOU open it.

Wayne
 
I had a sticky thing like this before too, that I think Wayne helped me out on :)


Public Sub AssignQA(objForm As Form) to
Public Sub AssignQA(objForm As Object)


As a workaround you could change the parameter to a String (objForm As String) and call Forms(objForm) from within the sub

Modest
 
Last edited:
Thanks for the suggestions. Both seemed like valid methods to me, but the first still resulted in type mismatch, while the later resulted in an error later in the routine associated with trying to reference the object as a form. Apparently that type doesn't support the function where I set the Caption value.

The work around suggested does seem to be working, however. I would like to be able to pass the form name as a form object, so any other thoughts are appreciated, but for now at least, the code is functional.

Thanks,

-NR
 
Well I can tell you your answer, and I can tell you what should work.

Any of these should work:
Public Sub AssignQA(objForm As Form)
Public Sub AssignQA(objForm As Variant)
Public Sub AssignQA(objForm As Object)

I would use Form, just because that's more descriptive as what kind of variable it is. But if that doesn't work, I'd try object.


But the most important part is how you call your function. Make sure you don't include your parentheses. Correct syntax is:
AssignQA Forms!frm_AssessmentTask

or if your form name was called "Assessment Task" (with spaces):
AssignQA Forms![Assessment Task]


hope this helps,
modest
 
There we go :D I knew i must have been addressing it wrong when i started. I was even able to pass Me.Form.

Much appreciated :D

Now I need to go through my (slightly modified) For loop to figure out why both of my tabs get the same caption value...

Thanks much for the assistance.

-NR
 
G’day NR

Apart from the answers given you may be also interested in why…

No point in typing it again.

As you have found out, Me.Form works and so does simply ‘Me’.

In, ‘perhaps the greater scheme of things’, it is best not to refer directly to things when you can avoid it. That simply means try to avoid, when you can, hard coding anything that can be constructed at runtime. It not only reduces the need to change your code in the future but also, in a rather peculiar way, helps to reinforce the idea of not storing calculated values in code or in a database.

Hope that makes some sense but fire away if not.

Regards,
Chris.
 
Could you please explain your answer in detail if you will and have the time.

Which for loop and why and how does the for loop conflict with the impossibility of attempting to pass objects by value?

I, still, am attempting to learn.

Kind regards,
Chris.
 
Dim intI, intP, x, n As Integer

Dim intI As Integer, intP As Integer, x() As Integer, n As Integer

Also if you changed your if statement in the loop to a select case statement, your code would be a little less confusing.


Code:
    For n = LBound(x) To UBound(x)
        Select case x(n)
            case 1
                objForm.Controls("tab_Pic" & intI).Caption = "Summer Picture"
            case 2
                objForm.Controls("tab_Pic" & intI).Caption = "Winter Picture"
                intI = intI + 1
        End Select
    Next n


And here is a good way to get a random number.
Code:
Dim intNmbr As Integer
Randomize                       [COLOR=Green]' Resets the random number, randomizing the seed.[/COLOR]
intNmbr = Int((3 * Rnd) + 1)    [COLOR=Green]' Generate random value between 1 and 3.[/COLOR]


I'm not sure what your RandomNumbers returns, but your logic is:

1st pass if x(n) = 1 then tab_Pic1 = Summer Picture
2nd pass if x(n) = 2 then tab_Pic1 = Winter Picture
3rd pass if x(n) = 2 then tab_Pic2 = Winter Picture
...any more passes and you'll be calling on tab_Pic3 (which doesn't exist)...

With this order they'll both have "Winter Picture" as a caption. Do you need help to avoid this?


-modest
 
Last edited:
couldn't agree with you more, modest. I made some modifications to it yesterday that resulted in it actually working right, but I like you method better. I was already leaning toward using a select...case structure intead.

As for the variable diming. I've had problems before with assigning that random function to an array. the function returns a variant, which is assigned directly from an array generated during the function. When I've tried assigning arrSomeArrayName = RandomNumbers (arguements), I get an error to the effect of "can't assign array".

Might be doing that wrong too.

ChrisO, the for loop isn't causing problems in passing the form name as an arg. Rather, once I got that correctly passed, thanks to the folks here, I wasn't getting the values I had expected. I would get both control's captions set to the same value, though that value would cycle between my two possibles. It's just messed up logic on my part.
 
modest said:
Dim intI, intP, x, n As Integer

Dim intI As Integer, intP As Integer, x() As Integer, n As Integer

One more Q :D

Kind of curious why one would want to Dim the vars like this. As in, not using the shorthand for multiple vars of the same type.
 
Alright, I boiled it down to the following (allowing for some changes to variable names)

Code:
Public Sub AssignPic(frmName As Form)
    Dim intI, x, n As Integer
    
    x = RandomNumbers(2, , 2)
    intI = 1
    For n = LBound(x) To UBound(x)
        Select Case x(n)
            Case 1
                frmName.Controls("tab_Pic" & intI).Caption = "Summer Picture"
            Case 2
                frmName.Controls("tab_Pic" & intI).Caption = "Winter Picture"
        End Select
        intI = intI + 1
    Next n
        
End Sub

Seems to be working fine. Just for the record, the idea here is that there are two different pictures, which must be presented in a random order. So, the order is randomized within the array (really, i'm not sure what type this qualifies as any longer, since it's passed a variant value), and the captions for the two tabs, which are statically placed on my main form, are then changed to the representative value for the random assignment.

Make sense?

Thanks for the help guys;
I fear this one is getting off the original topic. :D
 

Users who are viewing this thread

Back
Top Bottom