Form Loaded (1 Viewer)

SjCc

Registered User.
Local time
Today, 11:28
Joined
Oct 5, 2017
Messages
67
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

  • Untitled.png
    Untitled.png
    38.3 KB · Views: 166
  • Untitled2.png
    Untitled2.png
    15.7 KB · Views: 160

June7

AWF VIP
Local time
Today, 10:28
Joined
Mar 9, 2014
Messages
5,423
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, 11:28
Joined
Oct 5, 2017
Messages
67
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, 11:28
Joined
Oct 5, 2017
Messages
67
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

  • Untitled.png
    Untitled.png
    7.9 KB · Views: 106

Gasman

Enthusiastic Amateur
Local time
Today, 18:28
Joined
Sep 21, 2011
Messages
14,046
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, 10:28
Joined
Mar 9, 2014
Messages
5,423
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, 11:28
Joined
Oct 5, 2017
Messages
67
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

  • Untitled.png
    Untitled.png
    43.6 KB · Views: 166
  • Test.accdb
    452 KB · Views: 182
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 18:28
Joined
Sep 21, 2011
Messages
14,046
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, 11:28
Joined
Oct 5, 2017
Messages
67
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, 18:28
Joined
Sep 21, 2011
Messages
14,046
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, 11:28
Joined
Oct 5, 2017
Messages
67
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, 18:28
Joined
Sep 21, 2011
Messages
14,046
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, 10:28
Joined
Mar 9, 2014
Messages
5,423
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

Top Bottom