Custom inputbox/search form (1 Viewer)

Mr. SLP

Registered User.
Local time
Today, 06:00
Joined
Dec 23, 2017
Messages
56
I have a form that is used to search for Teacher_ID I want to be able to open the form from any other form and then use the selected Teacher_ID to update the one on the sending form. On occasion the sending form will be a sub form which is what’s really throwing me off. Any advice?


Sent from my iPhone using Tapatalk
 

isladogs

MVP / VIP
Local time
Today, 11:00
Joined
Jan 14, 2017
Messages
18,186
On occasion the sending form will be a sub form which is what’s really throwing me off.

What's the issue with that?
 

Mr. SLP

Registered User.
Local time
Today, 06:00
Joined
Dec 23, 2017
Messages
56
What's the issue with that?



I have been able to use screen.activecontrol.parent.name to get main form name and screen.activecontrol.parent.parent.name to get sub form name. However I am having a hard time putting them into a variable and then using the variable to reference the appropriate control on the correct form/subform.

I feel like I may be going about this all wrong so feel free to point that out.


Sent from my iPhone using Tapatalk
 

isladogs

MVP / VIP
Local time
Today, 11:00
Joined
Jan 14, 2017
Messages
18,186
Well you are certainly confusing me....
You can certainly refer to the main form from the subform using Parent. notation
e.g. If IsNull(Parent.txtSearch) Then

However, AFAIK Parent.Parent is meaningless
To refer to a control on the subform from the main form use e.g.
Me.SubformName.Form.txtTeacherID

However this all seems over complicated
When you select a TeacherID save it as a public variable e.g. strTeacherID
Then reference that in your other forms

I often create a function so I can also reference it in query filter criteria

Code:
Public Function GetTeacherID()
   GetTeacherID=strTeacherID
End Function

Another similar approach is to use a tempvar instead of a public variable
 
Last edited:

June7

AWF VIP
Local time
Today, 03:00
Joined
Mar 9, 2014
Messages
5,425
Coding a calling form to be versatile enough to be used as independent form as well as subform is difficult. Might have to use global variable or TempVars to pass data.

Also, the search form should probably be opened with acDialog. This suspends code in the calling procedure until the called form is closed. So the search form sets global variable or TempVars variable and the calling form refers to variable to pull its value.

...And Colin got there first - great minds...?
 

Mr. SLP

Registered User.
Local time
Today, 06:00
Joined
Dec 23, 2017
Messages
56
Code:
Public Function GetTeacherID()

   GetTeacherID=strTeacherID


So here’s what I tried and I still can’t get it.

Form1
Code:
private sub Searchtch_click()
Docmd.openform “search_for_staff”

Me.test = GetTeacherID()

Form “search_for_staff”
CODE]public sub AssignStaff_click()
Dim strTeacherID As Long
strTeacherID = me.TchList
Docmd.close


[/CODE]

Obviously I am not getting the teacherID into the variable for GetTeacherID() to work. What am I doing wrong?




Sent from my iPhone using Tapatalk
 

Mr. SLP

Registered User.
Local time
Today, 06:00
Joined
Dec 23, 2017
Messages
56
Never mind. I had a typo in the code and I wasn’t running the form as acDialog
Got it working.


Sent from my iPhone using Tapatalk
 

isladogs

MVP / VIP
Local time
Today, 11:00
Joined
Jan 14, 2017
Messages
18,186
Remove the line Dim strTeacherID As String from your form
You MUST declare strTeacherID as a public variable in a standard module so it is available across the entire application

Code:
Public strTeacherID As String

Public Function GetTeacherID()
   GetTeacherID=strTeacherID
End Function

If I understand you correctly the badly named form “search_for_staff” is where you select the teacher using a listbox(?) TchList
First I recommend renaming the form as e.g. frmStaffSearch
Then use code in the listbox after update event

Code:
Private Sub TchList_AfterUpdate()

   strTeacherID = me.TchList
   Docmd.close   '<==DO YOU REALLY WANT THIS HERE?
End Sub

From now on strTeacherID is available everywhere e.g. in another form, you could have a textbox populated in the form load event

Code:
Private Sub Form_Load()

   Me.txtTeacherID = GetTeacherID
End Sub
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 11:00
Joined
Jan 14, 2017
Messages
18,186
Its a crusty piece of code that's been well sliced :D
I'll correct it!
 

Users who are viewing this thread

Top Bottom