I'm facing a problem with QTP 11 (Quick Test Professional), the problem occurred when trying to call some function, QTP display a run time error ("General run error.") poopup message box.
i had tried many time to resolve the issue, but I dont understand what exactly causing the error.
when i Call a function from "Function library" the error displays. unless i took this function to an Action then the function will work.
have any one faced issue like this ?
Any help would be appreciated!
Sometimes I get the "General Run Error" error when I am attempting to call a function and pass the incorrect number of parameters in the function call.
Can you explain your last paragraph some more? When you run the function via FL, it works? You should not be running functions directly in your FL, but inside of your actions.
Here's an example:
Action
test_function("Hello", "42")
test_function(42, "Hello")
Function Library
Function test_function(sTextString, iIntNumber)
iNewIntNumber = iIntNumber + 1
MsgBox "String: " & sTextString & vbNewLine & "Int + 1: " & iNewIntNumber
End Function
In my function library, the second line (iNewIntNumber = iNewIntNumber + 1) may throw a "General Run Error" in the action due to an invalid cast on the variable.
Related
I have inherited a hideous Excel VBA application, written by a guy who apparently didn't like functions and comments. Opening the source code is like staring into an abyss of madness, with 9 levels of hell indentation awaiting to trap the unwary.
First, the setup.
In the process of debugging an intermittent issue, I have identified a particular line in the primary function as its location. Since the exception is not easy for me to fix, I gave this line, and only this line, an error handler that displays a custom message.
' Lots of stuff cut out
On Error GoTo TechReleaseBuildError
lb_Err1 = BuildDirectories(gj_TechBld.mt_TechRlsOutDir)
On Error GoTo 0
' Lots of stuff cut out
Exit Sub
TechReleaseBuildError:
MsgBox "Error building tech release. Pause the synchronization software and try again."
Stop
Resume
End Sub
The Stop and Resume are only there to help debugging, since the normal error messages that pop up don't have an option to debug the error.
Within the BuildDirectories function, I have identified one line that was causing the error that was being kicked up to primary function. I have placed error handlers in this function as well.
' Lots of stuff cut out
Exit Function
TEST:
Stop
Resume
SynchroRetry:
Dim CurrentError As ErrObject
CurrentError = Information.err
SynchroRetryCounter = SynchroRetryCounter + 1
Debug.Print "Synchro Error, retrying #" + SynchroRetryCounter
Stop
If SynchroRetryCounter > 5 Then
Stop
Information.err.Raise CurrentError.Number, CurrentError.Source, CurrentError.Description, CurrentError.HelpFile, CurrentError.HelpContext
Else
Application.Wait Now + #12:00:03 AM#
Resume
End If
End Function
At the very beginning of BuildDirectories I have placed an On Error GoTo TEST statement as a catch-all error handler so I can identify what lines they are coming from, and also used the SynchroRetry handler for the one line that I already know causes an issue, which I immediately reset back to the TEST handler. The SynchroRetry handler just waits for 3 seconds and retries, failing after the 5th retry.
' For some reason, this line (and only this line so far) causes problems when synchronization software is running.
' The error handler waits for a few seconds then tries again a few times.
Dim SynchroRetryCounter As Integer
SynchroRetryCounter = 0
On Error GoTo SynchroRetry
lj_FSO.CopyFile Source:=lt_RPOFilePath & lt_RPOFileName, Destination:=lt_VinRposDir & "\" & lt_RPOFileName, OverWriteFiles:=True ' Trouble line
On Error GoTo TEST
I have verified with Find that these are the only 3 On Error statements in the function (the only ones in the module, even). The top-level function also has no On Errors other than the two shown.
In brief summary, the top-level function has an error handler on only one line, and the function it encloses also has two error handlers inside it, one for one of its known troublesome lines and a general one that should be encompassing the entire function.
The problem is that some error is occurring that is causing my "Error building tech release." message to display, but does not seem to be triggering the TEST or SynchroRetry error handlers. I cannot imagine how this is possible. I'm relatively new to VBA's monstrosity of an error handling model, but I don't believe I did anything wrong. Did I miss some subtlety here? How is an exception able to bypass my handlers and bubble up the stack like this? I do not believe the assignment or passing of the function value could cause the problem, since their just a Boolean and String value. The problem is also intermittent, and only appears when we have file synchronization software active on the directory this program is copying files to.
So a couple hours after I posted this, I managed to figure it out.
The reason that the Debug button wasn't on the error boxes is because in Options the Error Trapping was set to "Break on Unhandled Error". Changing it to "Break in Class Module" brought the button back.
The reason the exceptions were bypassing my error handlers is because they were happening in my SynchroRetry error handler. In particular, I had to change the first few lines from this:
Dim CurrentError As ErrObject
CurrentError = Information.err
SynchroRetryCounter = SynchroRetryCounter + 1
Debug.Print "Synchro Error, retrying #" + SynchroRetryCounter
to this:
Dim CurrentError As New ErrObject
Set CurrentError = Information.err
SynchroRetryCounter = SynchroRetryCounter + 1
Debug.Print "Synchro Error, retrying #" + CStr(SynchroRetryCounter)
Note the additions of New, Set, and CStr.
Problem solved.
I have a problem with :
Function Create_Model(adress As range, name As String) As String
Dim Msg As String
On Error GoTo ErrorHandler
ActiveWorkbook.Names.add "toto", "=Interface!$I$19"
Create_Model=name
Exit Function
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Resume Next
End Function
Indeed, if I run this, I get :
"Error # 1004 was generated by VBAProject
Error Line: 0
Application-defined or object-defined error"
It seems that the problem come from the use of "Function" because if I try to execute this with a "Sub", it's working.
Someone could please explain me why I can't do this with a "Function" and how I could replicate this function otherwise?
P.S : If I compile using Debug->Compile VBAProject. I don't get any message.
P.S.2 : This function aims to be used in excel formula.
P.S.3 : Argument used are : adress = J18:L20 and name = "Test". And finnaly, I would like replace "toto" by name and "=Interface!$I$19" by adress.
Thanks for your help.
After the comments below, I got to thinking, and realized that my original answer (see below) is perhaps more based in (my own!) best practice than being technically correct.
The main difference between a Function and a Subroutine is that a Function can return a value, while a Subroutine cannot.
I have heard of other issues in using Function rather than Subroutine but can't find a list right now. You have apparently found one, though! These (perhaps) rumors of issues, is why I tend to limit actions to Subroutines, and returning values to Functions.
After doing some digging, I've found the following resources that may help with further explanation of the difference between the two.
From Chip Pearson's site: a page called Macros and Functions
From ExcelFunctions.net: a page called Excel VBA Tutorial Part 4 - VBA Functions & Subroutines
Looks like I learned something today.
==ORIGINAL ANSWER==
Short answer and super high-level
Functions are typically not the best place to perform actions (on a worksheet, cell, file, create objects, etc). They are really good at returning values, and that is what they are designed for.
Subs or procedures are built for taking action.
I have never seen this particular issue, but have run into many things that trying to do them in a Function causes problems.
I have a big macro which basically processes some columns and spits out results based on some cross-checking with an access 2003 database. It works absolutely fine - no hitches at all.
However, I recently had to make a modification to it. It was literally changing an '8' to a '9' in one line of the code. But next time I ran it, it threw the 1004: Method 'Range' of object '_Global' failed error. Excel 2003 is a funny thing - I once scratched my head for hours over this, trying to find offending lines of code that could be causing the error, but alas to no avail. I did something that I didn't expect to trigger anything:
Starting with the original macro (100% confirmed working), if I just open the code up, and then save it so the 'last updated' metadata will update to reflect the save, though absolutely nothing has changed, it will throw that error again on opening.
It's as if it's so fragile that saving the macro as is will break it. Any ideas?
Update: here's what I changed that initially brought about the issue
iOutputCols = 9 'this was changed to 9 from 8
ReDim Preserve sOutputCols(iOutputCols)
sOutputCols(0) = "Policy No"
sOutputCols(1) = "Client"
sOutputCols(2) = "Trans"
sOutputCols(3) = "Effective Date"
sOutputCols(4) = "ClosingRef"
sOutputCols(5) = "Gross"
sOutputCols(6) = "Comm"
sOutputCols(7) = "Net Due"
sOutputCols(8) = "Risk" 'this line was added
Making the change here, while originally causing the error, doesn't seem special - I did small changes like the above elsewhere in the code and in other modules, one time I even did something as testval = "test" and even that redundant line will produce the error. The most minimalistic way to cause it? Simply open it up, save it without changing anything, and on next use the error occurs.
The error occurs at this line, in a completely different code section which is part of a form:
If strErr <> "" Then MsgBox strErr, vbInformation + vbOKOnly, "Action Error"
Application.ScreenUpdating = True 'error occurs here, message box which shows me the error right above
End Sub
Update 2
Removing the error handling throws the error on this line
Case "> Send Next Period Reminder" 'error on line below
Call ReplaceText(wordApp, "[office_address]", Range("Address_" & Worksheets("UserParms").Range("F6").Value).Value) 'error this line
Call ReplaceText(wordApp, "[office_address2]", Range("Address2_" & Worksheets("UserParms").Range("F6").Value).Value)
'more of the same replacetexts below
For context, this is when an option is selected for "Send Next Period Reminder", which pulls a word .dot template from a static folder and populates it based on the data selected within the sheet (Hence the replace texts). This is in a different module and has never been touched before.
Try properly qualifying your Range method calls. You have lines like this:
Call ReplaceText(wordApp, "[office_address]", Range("Address_" & Worksheets("UserParms").Range("F6").Value).Value) 'error this line
Call ReplaceText(wordApp, "[office_address2]", Range("Address2_" & Worksheets("UserParms").Range("F6").Value).Value)
While it may not be obvious, there are cases, both environmental and code-based, where these unqualified uses of Range could fail. Change the references like Range("Address... to something like yourTargetWS.Range("Address...
I have looked through all of the VB6 Error Numbers to no avail, and it seems the command object itself takes care of the error messagebox in the background. What I need to know is if their is someway to change the MsgBox for the ADODB.CommandTimeout error itself. My first thoughts were to just catch the error (using error numbers) and then set a MsgBox that way. However, I can't find any documentation on any error numbers handling ADO Events. Any suggestions?
For whatever it's worth, the error code is 0x80040e31. You can Google for "error 80040e31 timeout expired". And, of course, you can check for that error status in your code.
Here is a list of ADO error codes:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms677004%28v=vs.85%29.aspx
Alright, I got smart and used this:
On Error GoTo ERRS
....
ERRS:
MsgBox Err.Number
This gave me the specific error number to catch (in this case: "-2147217871") and from there use a simple If statement to test if the error number = -2147217871 and if it does, display my MsgBox. Works like a charm (well, as much "charm" as VB6 and ADO has, I guess). I am still getting used to the legacy error handling of VB6. Thanks paulsm4 or your assistance!
i am trying to write a query using left function in access to take only the first 3 characters of a field.
Is there any alternative method for performing the same process without using left
Usage of left function shows a compile. error all of a sudden without any reason. If i copy the table and query to a new database it works fine for a while before the error comes again. This happens only on the usage of Left function.
you can use mid function:
Mid (Field, 1, 3)
The compile error is showing up because you have a missing reference. Open any module and check the References.
You can always try Mid
Mid([field1],1,3)
It sounds very like you have a problem with your references. Look for any references marked "MISSING". Also try to delete Visual Basic for Applications, it won't allow this, but it sometimes corrects the problem. Finally, check the details of Visual Basic for Applications and make sure that is available in the stated location. Any alternative to Left will be affected by this problem.
This problem is frequently associated with a missing reference that you would not think had anything to do with Left.
Run the following code and report back as to the results. Also tell us what version of Access you are running.
Sub ViewReferenceDetails()
Dim ref As Reference
For Each ref In Access.References
Debug.Print ref.Name & " - " & ref.Major & "." & ref.Minor & " - " & ref.FullPath
Next ref
End Sub