473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using a Form or Subform object as a parameter within a function

16
From within a form module, I am calling a public function attempting to pass a Form or Subform object.
Unfortunately, I continue to get a “Type mismatch” error when calling the function.

Calling function within Form module:

Expand|Select|Wrap|Line Numbers
  1. Debug.Print TypeName(Me.Subform_groupNames)
  2. 'Prints out: “SubForm”
  3. ChangeHeight (Me.Subform_groupNames)
  4. 'Type mismatch #13”
  5.  
  6.  
Public Function declaration within separate module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function ChangeHeight(SubFormObj As Access.SubForm)
  3.  

I’ve tried various iterations of declaring the function:

'Public Function ChangeHeight(ByRef SubFormObj As Access.NavigationControl.SubForm)
Type mismatch when compiling
Public Function ChangeHeight(SubFormObj As Access.SubForm)
Public Function ChangeHeight(SubFormObj As SubForm)
Public Function ChangeHeight(SubFormObj As Object)
Gets into object but then can’t use its properties

I’ve also tried various iterations of calling the function:

ChangeHeight (Me!Subform_groupNames)
ChangeHeight (Me.Subform_groupNames)

Produces: "Type mismatch - #13"

I’ve tried this:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  ChangeHeight (Forms!Main_Form.Subform_groupNames)
  3.  
BUT this causes a memory leak in Access. When the Access application shuts down, an Access process remains resident as a running process.

I’ve changed the parameter to “Form” but that also throws the "Type mismatch - #13" error.
Apr 17 '14 #1
12 3697
NeoPa
32,556 Expert Mod 16PB
I find this very hard to credit. What you have in the way of code seems absolutely on the button.

Can you check the name of the SubForm object itself by typing the following in the Immediate Pane (Ctrl-G) :
Expand|Select|Wrap|Line Numbers
  1. ?Me.Subform_groupNames
Run it when your code is about to execute line #3 above as it won't recognise Me. unless the code's active.

This is definitely an interesting one.

NB. I assume you realise that a Form and a SubForm are completely different objects. A SubForm typically contains a Form and Me.SubFormRef refers to the SubForm whereas Me!SubFormRef refers to the contained Form. See Referring to Items on a Sub-Form for more details.
Apr 17 '14 #2
drumahh
16
Hi,

I've attached a very simple zip file with the source that replicates the issue. It has a MainForm containing a Subform.

I did attempt to print out the name of the subform.

I needed to add Me.Subform_groupNames.Name to your suggestion to print out the name of the Subform.
Attached Files
File Type: zip TestSubformPass.zip (29.4 KB, 146 views)
Apr 21 '14 #3
NeoPa
32,556 Expert Mod 16PB
My bad on the code. Can you post the result you saw?

I'm away now till after the weekend. Unusually, I will download your database when I get back and have a look. Generally, most experts (myself included) would not be interested in downloading anything that wasn't first requested, but as I say, this is an interesting one.

Be prepared for a delay responding though.
Apr 23 '14 #4
drumahh
16
Hi. Thanks for your reply. The error is:

Run-time error '13':
Type mismatch

I’ve attached a screen shot of the error as well.

I have a workaround in place but it is not elegant, so I don’t mind the delay. Thanks for looking at this in more depth.

Attached Images
File Type: jpg SubformParameterIssue.jpg (46.7 KB, 1109 views)
Apr 24 '14 #5
anvidc
28
What you are trying to achieve?
If you want to change height
me.Subform_groupNames.Height = 4000
Apr 24 '14 #6
drumahh
16
I wish to have a function that dynamically changes the height of a Subform, for any SubForm on any form.

This workaround works, BUT I’d rather pass the form object than calculate it by passing the name of the form and the name of the subform:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function ChangeHeight(MainFormName As String, SubFormName As String) As Boolean
  3. 'Public Function ChangeHeight(SubFormObj As SubForm)
  4.  
  5. '------------------------------------------------------------------------------------------------------------------
  6. '   This function will change the height of the Subform
  7. '   based on the height of the detail row and the number of records in the subform
  8. '   The section that the subform lives in has to have CanGrowproperty set
  9. '
  10. '   Also, passing the subform has to be the full Forms! qualifier:
  11. '   BUT *** THIS CAUSES A MEMORY LEAK - DON'T USE
  12. '   http://social.msdn.microsoft.com/Forums/office/en-US/79b6aa1c-a9bc-45e9-aeef-362446bf6219/referring-to-a-subform-in-vba-access-2007
  13. '   at the end I discovered by trial and error that, by passing the object:
  14. '   Forms.MyMainForm.MySubform
  15. '------------------------------------------------------------------------------------------------------------------
  16.  
  17.  
  18. On Error GoTo ERRORHANDLER
  19.  
  20.     Dim SubFormObj As SubForm
  21.     Dim OriginalDetailHeight As Long
  22.     Dim NumRecords As Long
  23.     Dim InitialHeight As Long
  24.  
  25.     '**************************************************
  26.     ' Workaround
  27.     '    I would like to pass a subform object but that keeps throwing
  28.     '    a type mismatch error
  29.     '    So passing the form and subform name
  30.     '**************************************************
  31.  
  32.     Dim frm As Form
  33.     Dim ctrl As Control
  34.  
  35.     ' Assumes form is open.
  36.     Set frm = Forms(MainFormName)
  37.  
  38.     For Each ctrl In frm.Controls
  39.         If ctrl.Name = SubFormName Then
  40.             Set SubFormObj = ctrl
  41.             Exit For
  42.         End If
  43.  
  44.      Next ctrl
  45.  
  46.     '**************************************************
  47.     ' End Workaround
  48.     '**************************************************
  49.  
  50.     OriginalDetailHeight = SubFormObj.Form.Detail.Height
  51.  
  52.     SubFormObj.Form.RecordsetClone.MoveLast
  53.     NumRecords = SubFormObj.Form.RecordsetClone.RecordCount
  54.     SubFormObj.Height = SubFormObj.Height + (NumRecords - 1) * OriginalDetailHeight
  55.  
  56.     InitialHeight = frm.InsideHeight
  57.     frm.InsideHeight = InitialHeight + SubFormObj.Height - OriginalDetailHeight
  58.     ChangeHeight = True
  59.  
  60.  
  61. EXITHANDLER:
  62.     Set ctrl = Nothing
  63.     Set SubFormObj = Nothing
  64.     Set frm = Nothing
  65.  
  66. Exit Function
  67.  
  68. ERRORHANDLER:
  69.  
  70.     ChangeHeight = False
  71.     MsgBox Err.Description & " (" & Err.Number & ")", vbInformation, "Unexpected Error"
  72.     GoTo EXITHANDLER
  73.  
  74.  
  75. End Function
  76.  
  77.  
  78.  
Apr 24 '14 #7
anvidc
28
Sound complicate.. any i try to help that.
subform in the form is a object
so to change height on the object in a form
you the to tell the function where is the object..
means that mainform instead so in your function you need 2 item the mainform and the object inside the form..

Do you understand...
Apr 24 '14 #8
drumahh
16
>>you the to tell the function where is the object..

Yes, that is the crux of the issue. I can't "tell" the function that this is my form object without getting a "Type mismatch" error.

I SHOULD be able to, but Microsoft Access is not letting me.

Thanks!
Apr 24 '14 #9
anvidc
28
Sorry my bad, miss understand

This should do:

Expand|Select|Wrap|Line Numbers
  1. Function ChangeH(SubForm as Control, TotalRecord As Integer)
  2.  
  3. Debug.Print Subform.Height
  4.  
  5. Subform.height = Subform.height + (TotalRecord-1) 
  6.  
  7. End Sub
  8.  
Call ChangH(me.form_Sub, 10)
Apr 25 '14 #10
drumahh
16
This will work.

I have reattached the zip file of the small sample with the working code.

Fundamentally, the form or subform object should be expected to be used, but this appears to be a better workaround than the one I had.
Attached Files
File Type: zip TestSubformPass.zip (31.6 KB, 102 views)
Apr 25 '14 #11
NeoPa
32,556 Expert Mod 16PB
What happens if you define the parameter as ByRef?

I had a recent problem myself when passing a form object and it complained of a DLL calling error. It had tried to work it out by itself as it usually does, but got it wrong somehow. Try specifying that explicitly and let us know if anything changes.
May 2 '14 #12
drumahh
16
I just ried ByRef and that produced the same error. I tried ByVal just to see and that failed as well.

Expand|Select|Wrap|Line Numbers
  1. Public Function ChangeHeight(ByRef SubFormObj As Access.SubForm)
  2.  
  3. Debug.Print SubFormObj.Height
  4.  
  5. End Function
  6.  
May 2 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Simone | last post by:
Hi All, I have a Find Record button setup in most of my forms in order to find a specific customer's details. I have just noticed today though that this search will only find the customer if it...
5
by: ego | last post by:
Hi all , I had created the following Form/SubForm structure : MainForm SubForm A (SubForm of MainForm) SubForm B (SubForm of SubForm A) SubForm C (SubForm of SubForm B) SubForm D ...
9
by: PC Datasheet | last post by:
I'm stuck on something that seems should be easy and I need some help. My main form has an option group with five options. My subform chooses from different lists depending on which option is...
10
by: Thelma Lubkin | last post by:
My form/subform combination that allows user to display 'ColorSet' records, move to other records via a selector, to add,delete, and edit them, and to manage the related 'Color' records from the...
10
by: Sebastian Santacroce | last post by:
Hi, If I want to pass a form (forms I have created) to a function what would I set the declaration as for example Dim p as existingForm OpenForm (p, existingForm)
3
by: ghat12 | last post by:
Hi, I have created a form containing 5 textfields/combo boxes and a command button to conduct searches for matching records. My results are currently displayed as a separate datasheet which is...
5
by: Ron | last post by:
Hi All, I've got a form called frmCust that has a subform on it called sfrmBalance. sfrmBalance's control source is a query called qryTrans4CustBal. sfrmBalance has one field on it called...
1
by: Man4ish | last post by:
I am creating a graph using boost library, I am making the network using create_Network function() i am adding the vertices to this graph by creating the object g of class Graph. I am trying to...
1
by: troy_lee | last post by:
I have a table (Table A). It has one field, a PK. It is in a 1:M with another table (Table B). I am having trouble with a form/subform setup to view the 1:M records. On the parent form, there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.