?
Solved

Understanding the IsLoaded function

Posted on 2001-06-13
12
Medium Priority
?
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

777 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