Solved

Understanding the IsLoaded function

Posted on 2001-06-13
12
1,030 Views
Last Modified: 2007-12-19
Hello, stars,

Am trying to figure out how to pass objects to functions and not sure that I understand it.  Have a program that I want to check if the form is open before it attempts to open the target form.  Copied the IsLoaded from the Northwind database.  Left all the text the same.  In the activating procedure, I declared a memory variable with the name of strFormName (which is the same as in the IsLoaded public function) and set the value to my form name(frmAddNew).  Am getting a message that says this is a data type mismatch.  

Here is my current code on the activation:Private Sub cmdAddNew_Click()
  On Err GoTo Err_cmdAddNew_Click
 Dim strFormName As String
 strFormName = Forms!frmAddNew
 
   If IsLoaded(strFormName) Then
        MsgBox "Please try later when not in use!", vbOKOnly, "Already in use"
    Else
        DoCmd.OpenForm "frmAddNew", acNormal
    End If
   
Err_cmdAddNew_Click:
  Select Case Err.Number
    Case 2501
        Resume
        MsgBox Err.Description
    'Case Else
    '    MsgBox Err.Description
  End Select
     
End Sub

Follow-up question is that I can find no information in the VB Language Reference.  Does this indicate that this function will not be operable in the near future?  Should I discontinue using this?  

Thanks, again, for your expert guidance.

FlSteve

0
Comment
Question by:BegnrSteve
  • 7
  • 3
  • 2
12 Comments
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6186970
try:

Private Sub cmdAddNew_Click()
 On Err GoTo Err_cmdAddNew_Click
Dim strFormName As String
strFormName = "frmAddNew"

  If IsLoaded(strFormName) Then
       MsgBox "Please try later when not in use!", vbOKOnly, "Already in use"
   Else
       DoCmd.OpenForm "frmAddNew", acNormal
   End If
 
Err_cmdAddNew_Click:
 Select Case Err.Number
   Case 2501
       Resume
       MsgBox Err.Description
   'Case Else
   '    MsgBox Err.Description
 End Select
     
End Sub
0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6186982
the function doesn't take the actual form as a parameter, but a string containing the name of the form.
0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6186994
or you could use:

strFormName = Forms!frmAddNew.Name
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:BegnrSteve
ID: 6187001
Hello, cj,

Should have thought of what you did by enclosing the string value with "'s.  Basic rule of coding.  Should be shot for that one.  Thanks.

If you will be so kind as to explain your last post regarding the "but a string containing the name of the form".  Do I need to change something in the function?  Thanks, again, for your help.

FlSteve
0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6187020
I don't know why you would use my last example at all though because if a form is in the Forms collection at all, it means it is already loaded.  There is a collection called Documents that contains all of the forms and saved properties.  When you open a form, the document is opened and added to the Forms Collection.  Therefore you would get an error if you tried to use:
    strFormName = Forms!frmAddNew
if frmAddnew wasn't open.

Use the string like this:

strFormName = "frmAddNew"
0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6187049
Forms!frmAddNew - refers to a form object in the Forms Collection (contains all currently loaded forms)

Forms!frmAddNew.Name - is a property of the form.  It is a string containing the name of the form.

"frmAddNew" is just a string that IsLoaded uses to determine whether a form with a name property value matching that string is loaded or not.
0
 

Author Comment

by:BegnrSteve
ID: 6187234
Well, I just put the basic quotation marks around the form name and the compiler said, OK.  Will test that later.  Just wanted to be sure about if I passed the name of the variable to the function, if that was suitable.  It appears that if I set the variable with the object name (properly identified) :), then the value of the variable is being considered by the function.  In this case, the name of the form.  I guess this is what I am trying to figure out.  Still confused by the books on this issue.  Trying to learn.  

Thank you for stopping by and making all the great comments and help.  Hope you have a great day.  Accepting your responses as the answer.

FlSteve
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6188173
the IsLoaded function is not a part of VBA. so if you wanted to, as long as you copy the source code you could use it for as long as you want.

this is from the A97 help file:
"The IsLoaded function is a sample Function procedure included with the Northwind sample database, and not a built-in Visual Basic for Applications function. To use the IsLoaded function in your database, first open the Northwind sample database, then copy the function, and paste it in a module in your database."

the best way to find out if a form is open is to loop through the forms collection as cjswimmer has already stated (indirectly)

i'm not sure what the IsLoaded method that comes with Northwind looks like, but here's an IsLoaded function i just whipped up that will work by looping through the forms collection.

'begin copy
Public Function IsLoaded(FormName as string) as boolean

dim f as form
for each f in forms
    if f.name = formname then
        IsLoaded = True
        exit function
    end if
next f
end function
'end copy

please accept cjswimmer's answer, as i am only improving apon his idea.

dovholuk
0
 
LVL 6

Accepted Solution

by:
cjswimmer earned 100 total points
ID: 6190655
dovholuk, I believe that the IsLoaded function is much better than the Forms enumeration method.  Here is the code for it, you will see why it is better:

Function IsLoaded(ByVal strFormName As String) As Boolean
 ' Returns True if the specified form is open in Form view or Datasheet view.
   
    Const conObjStateClosed = 0
    Const conDesignView = 0
   
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
            IsLoaded = True
        End If
    End If
   
End Function

If you have a lot of open forms, the enumeration process is a waste of resources.  This method gets right to the point, plus it can be used for tables, queries, reports, macros and modules as well as forms.

0
 
LVL 6

Expert Comment

by:cjswimmer
ID: 6190666
just to note, I was referring to the GetObjectState method of the SysCmd function having the ability to test for those other objects, not the IsLoaded function.
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6190757
right on,

that SysCmd is a nice little nugget of info. bravo.

you've improved my improvement! lol!

now, how about changing your code a bit. i would "abstract" the method to be applicable to all object in your .mdb as such. i think you'll see the value in my suggestion too! :)

Function IsLoaded(ByVal strObjectName As String, ByVal ObjectType As Long) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.
   Const conObjStateClosed = 0
   
   If SysCmd(acSysCmdGetObjectState, ObjectType, strObjectName) <> conObjStateClosed Then
      IsLoaded = True
   End If
End Function

i would move the verification of the CurrentView to the calling code, rather than in the IsLoaded method.

any way you choose, this is a nice little method.

dovholuk
0
 

Author Comment

by:BegnrSteve
ID: 6190803
Well, what I did was to copy and paste the code from the Northwind database as you did here.  Still, I don't completely understand how it works.  But, I will continue playing with it, reading about it and learning what I can.  I really appreciate the discussion here and I will review the notes until I get a basic handle on it.  I have a real weakness of passing objects to functions and back.  But, I think that I can learn.  

Thanks so much for all your efforts.

FlSteve
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question