Form Loaded (1 Viewer)

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
Hi, I have a Function below, I am calling this in query and having error when form is not loaded or closed and i want that if form is not loaded then the query to show all records or in simple words should ignore this criteria without any pop up error message


Function IsFormLoaded(strForm As String) As Boolean

Dim frm As Form

For Each frm In Forms
If frm.Name = strForm Then
IsFormLoaded = True
Exit Function
End If
Next

End Function


Calling with if condition in query ;

IIf(IsFormLoaded('searchins'),[Forms]![SearchIns]![dr2],")
 

Attachments

June7

AWF VIP
Local time
Today, 00:12
Joined
Mar 9, 2014
Messages
2,755
Instead of looping through Forms collection, test for specific form.

IsFormLoaded = CurrentProject.AllForms(strForm).IsLoaded

However, if form is not loaded query will trigger an input popup for the form control reference. This isn't going to work. Have function return a value other than true/false. Could even pass field or control name as another argument of function.
Code:
Function GetData(strForm As String, strFld As String) As String
If CurrentProject.AllForms(strForm).IsLoaded Then GetData = Forms(strForm).Fields(strFld)
End Function
Why is query even opened if the form is not open? What is calling the query? I avoid dynamic parameters in queries.
 
Last edited:

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
Instead of looping through Forms collection, test for specific form.

IsFormLoaded = CurrentProject.AllForms(strForm).IsLoaded

However, if form is not loaded query will trigger an input popup for the form control reference. This isn't going to work. Have function return a value other than true/false. Could even pass field or control name as another argument of function.
Code:
Function GetData(strForm As String, strFld As String) As String
If CurrentProject.AllForms(strForm).IsLoaded Then GetData = Forms(strForm).Fields(strFld)
End Function
Why is query even opened if the form is not open? What is calling the query? I avoid dynamic parameters in queries.

Thanks for the reply indeed!!
I will try it as per your instructions
 

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
Instead of looping through Forms collection, test for specific form.

IsFormLoaded = CurrentProject.AllForms(strForm).IsLoaded

However, if form is not loaded query will trigger an input popup for the form control reference. This isn't going to work. Have function return a value other than true/false. Could even pass field or control name as another argument of function.
Code:
Function GetData(strForm As String, strFld As String) As String
If CurrentProject.AllForms(strForm).IsLoaded Then GetData = Forms(strForm).Fields(strFld)
End Function
Why is query even opened if the form is not open? What is calling the query? I avoid dynamic parameters in queries.

I am getting this error as attached file.. or may be i am doing something wrong as i can only do copy paste in vba codes ... :D your help appriciated
 

Attachments

Gasman

Enthusiastic Amateur
Local time
Today, 09:12
Joined
Sep 21, 2011
Messages
5,076
Did you use Intellisense for that?, as mine does not show .Fields property for a form?

try the controls property

forms("Form1").Controls("Text3")
 

June7

AWF VIP
Local time
Today, 00:12
Joined
Mar 9, 2014
Messages
2,755
What do you mean you can only do copy paste?

What error message?

Did you modify query? Post query SQL statement.

Ooops, no I was relying on memory. However, this does work:

GetData = Forms(strForm).Recordset.Fields(strFld)
 
Last edited:

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
What do you mean you can only do copy paste?

What error message?

Did you modify query? Post query SQL statement.

Ooops, no I was relying on memory. However, this does work:

GetData = Forms(strForm).Recordset.Fields(strFld)


Copy paste menas have no expertise over vba coding !!! :D


I attached error image which is again now after your suggesstion

Please do me a favor that my form name is SearchIns and form field name is Dr2 please set all these in vba code you provided

I have also attached a sample DB i want if getdata see that form is not loaded then all records of that field must be displayed but if form loaded then only show the TRID PK number with match to form field
 

Attachments

Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 09:12
Joined
Sep 21, 2011
Messages
5,076
You either use the variable for the form name or the form name itself?
In your present form you would use strForm or "searchins"

No point in using "searchins" if you need to use other forms?

HTH
 

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
You either use the variable for the form name or the form name itself?
In your present form you would use strForm or "searchins"

No point in using "searchins" if you need to use other forms?

HTH
Gasman could you please do it in sample i attached, as i am not getting it :banghead:
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:12
Joined
Sep 21, 2011
Messages
5,076
Gasman could you please do it in sample i attached, as i am not getting it :banghead:
No, even if the site was working as normal and not having all these problems.

June7 has already given you the code

Code:
GetData = Forms(strForm).Recordset.Fields(strFld)
Why cannot you copy and paste that.? :confused:
 

SjCc

Registered User.
Local time
Today, 01:12
Joined
Oct 5, 2017
Messages
66
No, even if the site was working as normal and not having all these problems.

June7 has already given you the code

Code:
GetData = Forms(strForm).Recordset.Fields(strFld)
Why cannot you copy and paste that.? :confused:
Thanks dear

should i use it same or change form name with my form name and field name as well ? or should use like

GetData = Forms(strForm).Recordset.Fields(strFld)

or

GetData = Forms(searchins).Recordset.Fields(dr2)
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:12
Joined
Sep 21, 2011
Messages
5,076
No. Your initial post was using this function in a query, so you would pass the actual form and field names to that function.
If you hardcode the names then the function will only work for that form and field name, and if you were doing that, then you would surround the names with double quotes "

I would be using the variables, as June7 posted.

Code:
GetData = Forms(strForm).Recordset.Fields(strFld)
Probably along the lines of
Code:
IIf(Len(GetData("searchins","dr2"))> 0,GetData("searchins","dr2"),"")
depending on what you expect to return, in this case I am assuming a string?

HTH
 

June7

AWF VIP
Local time
Today, 00:12
Joined
Mar 9, 2014
Messages
2,755
As already requested, provide the SQL statement of query. This probably needs to be edited to work with revised function. Gasman shows an expression that would be used in query in place of the expression you originally posted. Although I don't think IIf() is needed. Just call the function:

GetData("searchins","dr2")
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom