Avatar of Anton Fernando
Anton Fernando

asked on 

MS Access VBA form /subform control question

Hello,
if you have code like :
Function ShowControls()
Dim frmCust as Form  
Dim i as Integer 
Set frmCust = Forms("Customer")  
For i = 0 To frmCust.Count - 1    
Debug.Print frmCust(i).ControlName  
Next i 
End Function

Open in new window

And you want to do the same kind of exercise on a subform, how would you do it ?
I am getting errors in the Set frmCust=Forms("Customer") as it not able to find it whereas if the subform is treated like a stand alone form it works great.
I have tried Forms!Mainform!Customer and forms!MainForm!SubformControlName! . It seems a minefield trying to get the referencing right. Any assistance would be great as I need to access subform elements via their index number whilst it part of a mainform/subform set up.    
Microsoft AccessVBA

Avatar of undefined
Last Comment
Anton Fernando
Avatar of Jonathan Kelly
Jonathan Kelly
Flag of Ireland image

Function ShowControls()
Dim frmCust as Form  
Dim i as Integer Set frmCust = Forms!Customer!CustomerSubform.Form  
For i = 0 To frmCust.Count - 1    
Debug.Print frmCust(i).ControlName  
Next i 
End Function

Open in new window


check this
Public Function getControls()
Dim frm As Form
Dim ctl As control
DoCmd.OpenForm "Customer", acNormal, , , , acHidden
Set frm = Forms("Customer")


For Each ctl In frm.Controls
Debug.Print ctl.Name
Next
Set frm = Nothing
End Function

Open in new window



Avatar of Anton Fernando
Anton Fernando

ASKER

Thank you for both your replies.

John Tsioumpris. I will look at this as a possible solution,(fall back) -  though I would still like a more direct access to the subform without the need to open the subform separately.

Jonathan Kelly. I tried using your code and still get the error message Run time error 2465 that the subform could be found.
I played with what you wrote and found something that worked.

I had to use the control name for the subform as defined in the mainform, ie When I click on the holder for the subform (in the mainform), I needed to use its name rather then the actual name of the subform.
So the actual code
where Mainform is FrmDepotIssuesDBSv3
           Subform  is frmSubdbs3
          The holder name for the subform is Daily Depot Appointments (as defined in the main form)

Function ShowControls()
Dim frmDepotEntry As Form
Dim i As Integer
Set frmDepotEntry = Forms!FrmDepotIssuesDBSv3![Daily Depot Appointments].Form
For i = 0 To frmDepotEntry.Count - 1
Debug.Print i, frmDepotEntry(i).ControlName
Next i
End Function

As a follow on question, is there a way of getting the index value without going the "For i = 0 To frmDepotEntry.Count - 1 " process ?
Some kind of function like :

FormIndex(ControlName) where it returns the form control index straight ? Or will I need to build a function to do that??

Thank you both for all your help and guidance. 

 



 
Here is another modified solution without"opening"
Public Function getControls()
Dim frm As Form_Customer
Set frm = New Form_Customer
Dim ctl As control
For Each ctl In frm.Controls
Debug.Print ctl.Name
Next
Set frm = Nothing


End Function

Open in new window

In order for this to work your form should carry code behind....if you are uncertain on this just right click the form in question (Customer) --> Design View --> Design tab --> look for the button to enter VBE (on Access 2007 for example is called "View Code")
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Anton Fernando
Anton Fernando

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo