Use object as variable for function - function

I'm trying to create a function called wait that would use an object:
Function wait(browser As Object)
' loop until the page finishes loading
Do While browser.READYSTATE <> 4
Loop
End Function
I also have:
Function GetIE() As Object
On Error Resume Next
Set GetIE = CreateObject("InternetExplorer.Application")
End Function
and:
Dim appIE As Object
Set appIE = GetIE
sURL = "http://google.com"
wait (appIE)
But i'm getting "Run time error '424'; Object required.
Any idea?

Change this line
wait (appIE)
to
wait appIE
Explanation: In VBA, whenever you call a Function that has parameters, if you are not doing anything with the return value, then you have to call it without the parenthesis. In this case, since the code is not returning anything, it should be defined as a Sub and not a Function. The same thing applies to Sub routines that take parameters also, i.e. you have to call it without parenthesis with the parameters separated by comma.
Further Reading (via #doug) : Quick VBA Tip: Parentheses

Related

Close only Word.Application Objects that were created by my program

So I'm working on a VBA function in Access 2010 that creates a Word.Application object and returns it. Later on I want to be able to close this object, however not if its a Word.Application object that was not started by my program.
Public myGlobalWordApp as Object
Public newWordAppInstCreated as Boolean
Function GetWordAppInstance(Optional isVisible As Boolean = False) As Object
newWordAppInstCreated = False
On Error Resume Next
Set myGlobalWordApp = GetObject(, "Word.Application")
If myGlobalWordApp = Nothing Then
myGlobalWordApp = CreateObject("Word.Application")
myGlobalWordApp.Visible = isVisible
newWordAppInstCreated = True
End If
Set GetWordAppInstance = myGlobalWordApp
End Function
I want to make a CloseWordAppInstance() Sub that closes the myGlobalWordApp application object BUT ONLY if my VBA code was the one to open it. Sometimes I'll have another Word document up that I'm looking at and I don't want that window to be closed.
I've looked at the Word 2010 Application documentation and I saw that there is a .Parent method that I can call. I imagine that I could use this to see if I can determine if my Access Document/Module/Application created the Word.Application object but I dont know how to reference the "current object" or know how to do the comparison. Any help on that step would be appreciated.
My only "impropper" way of doing this would be checking the Boolean flags that I attempted to make, but that wont work if I make a second object.
Alternatively if anyone knows a better way to do this process that would be great!
You need to make sure that after you have created a word instance, you keep its reference. You can then use the same reference to close the word instance.
Private myGlobalWordApp As Object
Public Function GetWordAppInstance(Optional isVisible As Boolean = False) As Object
On Error GoTo ErrH
If myGlobalWordApp Is Nothing Then
'' create a new word instance if one doesn't exist
Set myGlobalWordApp = CreateObject("Word.Application")
myGlobalWordApp.Visible = isVisible
Set GetWordAppInstance = myGlobalWordApp
Else
'' otherwise return the instance we have
Set GetWordAppInstance = myGlobalWordApp
End If
Exit Function
ErrH:
MsgBox "Error creating word instance!", vbExclamation
End Function
Public Sub CloseWordInstance()
If Not myGlobalWordApp Is Nothing Then
'' close our word instance
myGlobalWordApp.Quit False
Set myGlobalWordApp = Nothing
End If
End Sub
You should never need to use the myGlobalWordApp variable directly. You can instead call the GetWordAppInstance to get the word instance and the CloseWordInstance to safely close it. This would ensure that you don't ever overwrite the myGlobalWordApp variable and lose reference to the word instance.
Public Sub Test()
Dim myWordInst As Object, wdDoc As Object
Set myWordInst = GetWordAppInstance(True)
Set wdDoc = myWordInst.Documents.Add
' do something with this doc here
CloseWordInstance
End Sub

VBA Acess Function - Return Outlook Folder/Inbox as object

Private OutlookApp, Nms As Object
Sub TestSub()
Dim Fold As Object
Set OutlookApp = GetObject(, "Outlook.Application")
Set Nms = OutlookApp.GetNamespace("MAPI")
Set Fold = outlookFolderpath("Test Folder")
For Each Email In Fold.Items ' This loop doesnt work
Debug.Print Email.Subject
Next
End Sub
Private Function outlookFolderpath(Inbox As String) As Object
Dim fold_name As String
Set OutlookFolder_Path = Nms.Folders(Inbox).Folders("Inbox")
For Each Email In OutlookFolder_Path.Items ' This Loop works
Debug.Print Email.Subject
Next
End Function
Hello,
I was hoping someone could help me with the above code. I'm trying to Set and inbox folder path from a function and using it within the sub.
It works fine from within the function but not when setting it in the sub?
Can anyone see where I am going wrong? I get a runtime error '91' - Object variable or With block variable not set
so I would gather that the function is not returning the object but I'm not sure why?
thanks
You need to return the object from the function, so
Set OutlookFolder_Path = Nms.Folders(Inbox).Folders("Inbox")
should be
Set outlookFolderpath = Nms.Folders(Inbox).Folders("Inbox")
If you declare Option Explicit at the top of your code you will be less likely to run into problems like this as all your variable should be declared.

Returning Multiple Values from a Custom Function

So, I've got a ValidateForm function that loops through a form to validate each control. I have a collection set up called ValData to capture different bits of info to be passed out of the function. This is working great.
However, I don't know how to access each item in ValData after the function returns. I can get one at a time like: ValidateForm().IsValid, but in order to get each item, I have to run the function again. I want to avoid this.
Is there a way to run the function once, but access the values of each item returned?
Depending upon your requirements (which are NOT clear in your question! ;-) ), you might consider using a Collection as the return from your function:
Private Function MyResultsFunction() As Collection
Dim output As Collection
Set output = New Collection
'Hydrate your collection with data by whatever means necessary:
With output
'Stupid example code:
.Add "Item1"
.Add "Item2"
.Add "Item3"
.Add "Item4"
End With
'Return a reference to the collection as the function output:
Set MyResultsFunction = output
End Function
As a simple, retarded test of the above:
Private Sub Form_Load()
'A variable to receive the results from your function:
Dim Results As Collection
'An iterator to loop through the results contained in the collection:
Dim CurrentItem As Variant
'For this example, a string to toss the results into to display in the
'MsgBox:
Dim output As String
'Use the local Collection defined above to access the reference returned by
'your function:
Set Results = MyResultsFunction
'Iterate through the collection and do something with the results
'per your project requirements:
For Each CurrentItem In Results
'This is stupid example code:
output = output & CurrentItem & vbCrLf
Next
'I am just displayng the results to show that it worked:
MsgBox output
'Clean up:
Set Results = Nothing
End Sub
Hope that heps!
Without seeing your code it's hard to say what exactly you are tyring to do, but here is one of several ways you can store results to query later.
Declare a public variable at the top of your module to represent the items from your ValData function. After you populate the array, you can access the items through a normal function.
You obviously could do more sophisticated things, especially if you use a collection object. You could even store a counter and create a GetNext() function. I hope this gives you a heads start.
Public Results(1 To 2) As String
Sub CreateTestArray()
Results(1) = "Foo"
Results(2) = "Bar"
End Sub
Function GetResult(ByVal i As Long) As String
If i <= UBound(Results) Then
GetResult = Results(i)
End If
End Function
Sub ProofOfConcept()
MsgBox GetResult(2) ' will show "Bar"
End Sub

Access: persist a COM reference across Program Reset?

Are there ways in Access VBA (2003) to cast a COM reference to an integer, and to call AddRef/Release? (which give the error "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic")
I'm using a third-party COM object which doesn't handle being instantiated twice in a single process (this is a known bug). I therefore thought of storing the reference as the caption of a control on a hidden form to protect it from Program Reset clearing all VB variables.
Edit: I think the cast to int can be done with the undocumented ObjPtr, and back again with the CopyMemory API, and AddRef/Release can be called implicitly. But is there a better way? Are add-ins protected from Program Reset?
Is the problem with surviving the code reset or is it that once the code is reset it can't be re-initialized?
For the first problem, wrap your top-level object in a function and use a STATIC variable internally to cache the reference. If the STATIC variable Is Nothing, re-initialize. Here's the function I use for caching a reference to the local database:
Public Function dbLocal(Optional bolInitialize As Boolean = True) +
As DAO.Database
' 2003/02/08 DWF added comments to explain it to myself!
' 2005/03/18 DWF changed to use Static variable instead
' uses GoTos instead of If/Then because:
' error of dbCurrent not being Nothing but dbCurrent being closed (3420)
' would then be jumping back into the middle of an If/Then statement
On Error GoTo errHandler
Static dbCurrent As DAO.Database
Dim strTest As String
If Not bolInitialize Then GoTo closeDB
retryDB:
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
' now that we know the db variable is not Nothing, test if it's Open
strTest = dbCurrent.Name
exitRoutine:
Set dbLocal = dbCurrent
Exit Function
closeDB:
If Not (dbCurrent Is Nothing) Then
Set dbCurrent = Nothing
End If
GoTo exitRoutine
errHandler:
Select Case err.Number
Case 3420 ' Object invalid or no longer set.
Set dbCurrent = Nothing
If bolInitialize Then
Resume retryDB
Else
Resume closeDB
End If
Case Else
MsgBox err.Number & ": " & err.Description, vbExclamation, "Error in dbLocal()"
Resume exitRoutine
End Select
End Function
Anywhere you'd either of these in code:
Dim db As DAO.Database
Set db = CurrentDB()
Set db = DBEngine(0)(0)
db.Execute "[SQL DML]", dbFailOnError
...you can replace the whole thing with:
dbLocal.Execute "[SQL DML]", dbFailOnError
...and you don't have to worry about initializing it when your app opens, or after a code reset -- it's self-healing because it checks the Static internal variable and re-initializes if needed.
The only caveat is that you need to make a call with the bolInitialize argument set to False when you shut down your app, as this cleans up the reference so there's no risk of your app hanging when it goes out of scope as the app closes.
For the other problem, I really doubt there's any solution within VBA, unless you can make an API call and kill the external process. But that's something of a longshot, I think.

Referencing global variable - MS Access

Im trying to pass the name of a global variable to a sub routine and would like to know how to reference it. For example I could do the below with a control:
Private Sub passCtrlName(ctlName as String)
Me.Controls(ctlName) = "Whatever"
End Sub
Edit:
For Example
Public imGlobVar As String
Public Sub passGlobVar(frm as Form, ctlName as String, globVar as String)
frm.Controls(ctlName) = globVar
End sub
And call it as
Private Sub imaButton_Click()
imGlobVar = "something"
Call passGlobVar(Me , txtBox1, imGlobVar)
End sub
2nd Edit:
It seems that I could most definitely be barking up the wrong tree here, so I will explain what I'm trying to achieve.
I have a form that has textboxes for the users (risk) address, with a checkbox at the top that lets the user select that this address is the same as the 'contact' details already on the system, and the textboxes are locked.
Populating the textboxes is fine and works. What I use the global variables for is to improve usability (albeit slightly).
The user can add new details, and if they hit the checkbox 'make same as contact' the details that they have entered are stored in the global variables, one for each control.
If the user has made a mistake by hitting the checkbox, they haven't lost these value, and by unchecking the box the entered values are returned.
I hoped to create a sub routine where I could pass the name of the global variable and control and calling this routine, as opposed to writing it out for each control.
I have a feeling that I could be using the wrong technique to achieve my goals. But in answer to my original question, it appears that you can not pass global variables to sub routines in the manner that I wished.
You do not need to pass global variables, you can simply refer to them by name. Note that global variables are reset if an unhandled error occurs.
In http://msdn.microsoft.com/en-us/library/dd897495(office.12).aspx you will find a section on Scope and Lifetime of Variables and Constants.
In a module:
Option Explicit
Public glbVarName As String
Const MyConstant=123
Sub InitVar
glbVarName="Something"
End Sub
Any other module, includeing a form's class module:
Sub SomeSub
MsgBox glbVarName
SomeVar=2+MyConstant
End Sub
If you're asking if you can dynamically reference global variables using a string containing the variable name the answer is no. You could use a single global array and pass the index, which would allow you to dynamically reference an element of the array.
[Edit]
In response to the clarification in the question: You could just save the value of each control to its Tag property when the user checks the checkbox. Then, if the user unchecks the checkbox, you can just loop over your controls and assign the value from the Tag back to the Value of the control.
You could store the values from your controls in a Dictionary object, using the control names as the dictionary keys. Then you can retrieve the value for each control based on the control's name.
Option Compare Database
Option Explicit
Const cstrMyControls As String = "Text0,Text2,Text4,Text6"
Dim objDict As Object
Private Sub chkToggle_Click()
If Me.chkToggle = True Then
Call SaveValues
Else
Call RestoreValues
End If
End Sub
Private Sub SaveValues()
Dim varControls As Variant
Dim i As Long
Set objDict = Nothing 'discard previous saved values '
Set objDict = CreateObject("Scripting.Dictionary")
varControls = Split(cstrMyControls, ",")
For i = 0 To UBound(varControls)
objDict.Add varControls(i), Me.Controls(varControls(i)).Value
Next i
End Sub
Private Sub RestoreValues()
Dim varControls As Variant
Dim i As Long
If objDict Is Nothing Then
'MsgBox "No values to restore." '
Else
varControls = objDict.keys()
For i = 0 To UBound(varControls)
Me.Controls(varControls(i)).Value = objDict(varControls(i))
Next i
End If
End Sub
I use additional field in table - name cancel - of course boolean - when i'm not sure if contents of fields will be valid I set it true. If this field will be true by the end - then I clean up (it may be all record or some fileds of course). Very easy.