[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-05
20
Medium Priority
?
459 Views
Last Modified: 2012-09-07
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

0
Comment
Question by:MacroShadow
  • 8
  • 7
  • 4
  • +1
20 Comments
 
LVL 75
ID: 38370294
Call YourFunctionName(me)

mx
0
 
LVL 75
ID: 38370300
Call Test (me)


This will pass the Form object you are currently in.

mx
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38370917
I'm not looking to call the function from elsewhere, I want to provide a parameter for the function itself.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75
ID: 38370929
I got it.  Your Function can be where ever.  What I will posted will pass the Form object to the function.

mx
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38371060
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.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1464 total points
ID: 38371083
You cannot call it from the Immediate window.  You need to call it from within a Form.

Well ... you COULD ... call it from the  Immediate window like this:

?CountSelectedRecords(Forms!SomeFormName.Form)

mx
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 536 total points
ID: 38371090
Let me guess: SubDetails is the name of a subform control.
If so:

intRecords = CountSelectedRecords(SubDetails.Form)

By the way, I believe your function could be reduced to:
Function CountSelectedRecords(ByRef frm As Form) As Integer
  
    CountSelectedRecords = frm.SelHeight

End Function 

Open in new window

/gustav
0
 
LVL 75
ID: 38371107
Guess this has changed  quite a bit from the test function ... :-(

mx
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38371123
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)?
0
 
LVL 75
ID: 38371131
Yikes .... scope creep, lol ....
Gustav ... take over.  You're just up and I'm zzzzzzz ...
later.mx
0
 
LVL 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 536 total points
ID: 38371350
This works 98%. You can fool by pressing tha tab key:
Private Sub Form_Current()

    Me!txtSelected.Value = Me.SelHeight
    
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    
    Me!txtSelected.Value = Me.SelHeight

End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Me.NewRecord = True Then
        Me!txtSelected.Value = 0
    Else
        Me!txtSelected.Value = Me.SelHeight
    End If
    
End Sub

Open in new window

/gustav
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38371544
I get a runtime error 2448: You can't assign a value to this object.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 38371567
Textbox txtSelected must be unbound.

/gustav
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38371576
Right, I just figured that out. Tried to edit the post but you beat me to it.
0
 
LVL 28

Author Comment

by:MacroShadow
ID: 38371578
This works 98%. You can fool by pressing the tab key:
In what case will it not work?
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 38371598
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
0
 
LVL 58
ID: 38371703
BTW,

  [Form] works too (in place of Me)

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

Jim.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1464 total points
ID: 38372861
Just for kicks, I thought I would note that ... the original Q was answered in the first post :-)
Just sayin '......

mx
0
 
LVL 28

Author Closing Comment

by:MacroShadow
ID: 38376329
I hope the grading was fair.
0
 
LVL 75
ID: 38377277
no problem.  

mx
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question