Link to home
Start Free TrialLog in
Avatar of Joe Howard
Joe HowardFlag for United States of America

asked on

How can I send a form as a parameter to a function

How can I send a form as a parameter to a function. for example:

Function Test(ByVal frm as Form)

    Set RS = frm.RecordsetClone

Open in new window

Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Call YourFunctionName(me)

mx
Call Test (me)


This will pass the Form object you are currently in.

mx
Avatar of Joe Howard

ASKER

I'm not looking to call the function from elsewhere, I want to provide a parameter for the function itself.
I got it.  Your Function can be where ever.  What I will posted will pass the Form object to the function.

mx
This is my function:
Function CountSelectedRecords(ByRef frm As Form) As Integer
    Dim i As Long
    Dim F As Form
    Dim RS As Recordset
    Dim intSelRec As Integer

On Error GoTo Err_Catch
    ' Get the form and its recordset.
'    Set F = frm
    Set RS = frm.RecordsetClone

    ' Move to the first record in the recordset.
    RS.MoveFirst

    ' Move to the first selected record.
    RS.Move F.SelTop - 1

    ' Enumerate the list of selected records
    For i = 1 To F.SelHeight
        intSelRec = intSelRec + 1
        RS.MoveNext
    Next i
    
    CountSelectedRecords = intSelRec
'    MsgBox CountSelectedRecords
    
    Exit Function
    
Err_Catch:
    CountSelectedRecords = 0
    
End Function

Open in new window

When I call it from the immediate window:
?CountSelectedRecords(SubDetails)

Open in new window

I get a compile error: ByRef argument type mismatch.
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guess this has changed  quite a bit from the test function ... :-(

mx
Thanks to both of you.

DatabaseMX: it works.

cactus_data: you're right!

How can I use it to display the result in a textbox, re-querying each time a record is selected/deselected (without using the timer)?
Yikes .... scope creep, lol ....
Gustav ... take over.  You're just up and I'm zzzzzzz ...
later.mx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get a runtime error 2448: You can't assign a value to this object.
Textbox txtSelected must be unbound.

/gustav
Right, I just figured that out. Tried to edit the post but you beat me to it.
This works 98%. You can fool by pressing the tab key:
In what case will it not work?
Well, the last 1% is when you select a record and move to a field on the same record.
You could cut that by adding some code to the OnEnter events of the text/combo/check boxes of the fields.

/gustav
BTW,

  [Form] works too (in place of Me)

  Just in case you run over that somewhere else...

Jim.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I hope the grading was fair.