Solved

Understanding the IsLoaded function

Posted on 2001-06-13
12
1,029 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now