Not able to read property at runtime - vb6

When I tried to read the "Left property" of a control, it is giving me the error,
"Left cannot be read at run time"
Here is my code,
for each ctrl in me.controls
if ctrl.left > 2490 then
'app logic
end if
next
what is wrong in this code. It worked without error in another computer.
Can anyone tell me what is wrong

You may have a design-time only placeable control on your form like a timer for example, that does not have a run-time left property. You could check the type of control to ensure only TextBox, Label, Button, etc. get checked, or just use an on error resume next:
Check for object type using TypeOf:
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is Timer Then
Else
If ctrl.Left > 2490 Then
'app logic
End If
End If
Next
Check for object type using TypeName:
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "Timer" Then
Else
If ctrl.Left > 2490 Then
'app logic
End If
End If
Next
Using On Error Resume Next:
Dim ctrl As Control
On Error Resume Next
for each ctrl in me.controls
if ctrl.left > 2490 then
'app logic
end if
Next

If you use the last method, it's important to handle errors inline reraising any unexpected error. Otherwise if you get any different error than the one you're expecting you could have a very tough time finding it. So:
Dim ctrl As Control
On Error Resume Next
for each ctrl in me.controls
if ctrl.left > 2490 then
Select Case Err.Number
Case 0 'No Error, ignore
Case 393 'The error you want to ignore
Err.Clear 'Reset for next iteration
Case Else
On Error Goto 0
Err.Raise Err.Number 'Reraise any unexpected errors
End Select
'app logic
end if
Next
On Error Goto 0

Related

VB6 Update Error Sheridan Grid

I'm making some changes to a program that was written by another developer, which uses an SSDB Grid.
I'm writing the code for the BeforeUpdate method.
On Error GoTo BeforeUpdate_Err
Dim ans%
ans% = MsgBox("These changes will be committed to the database. These changes cannot be undone. " & _
"Would you like to continue?", vbYesNo, "Confirm Changes")
If ans% = 7 Then
Grd_Collection.CancelUpdate
End If
Exit Sub
BeforeUpdate_Err:
MsgBox (Err.Description)
The only other code for the grid is the InitColumnProps method.
However, after hitting the Exit Sub line, I get an error message "Update Error".
I've searched the code for this being hard-coded but it isn't, so it's coming from the grid.
What is causing the error and how do I fix it?
Doesn't the BeforeUpdate method pass in an integer? (Cancel As Integer) or something?
Therefore, you should just be able to change your code (and tidy it up) to this:
On Error GoTo BeforeUpdate_Err
If MsgBox("These changes will be committed to the database. These changes cannot be undone. " & _
"Would you like to continue?", vbYesNo, "Confirm Changes") = vbNo Then
Cancel = 1
End If
Exit Sub
BeforeUpdate_Err:
MsgBox (Err.Description)

How do I perform print images on screen?

I am completely new to VB6, and I need to print images on a page. I am having print options in 2 different places on the same page.
What I have tried:
I tried to write a common method to perform the print operation:
Private Sub mnuPrintImage_Click()
On Error GoTo mnuPrintImage_Click_Error
gobjRLItemImage.PrintImage imgItem
Exit Sub
mnuPrintImage_Click_Error:
ShowError "frmItem.mnuPrintImage_Click"
End Sub
I tried to use Me.PrintForm, but when I do so, I am getting a popup to save the complete page. However, I need to print the image directly and don't want to save it locally.
Public Sub PrintImage()
On Error GoTo PrintImageError
me.printform
Exit Sub
PrintImageError:
Select Case Err.Number
Case 440:
' Cancel button pressed; do nothing
Case Else
Err.Raise Err.Number
End Select
End Sub

Wrong number of arguments or invalid property assignment error in hp UFT

Set ParentObject=Browser("Title:=Sign-In").Page("Title:=Sign-In Home Page")
PropertyRequired="name"
PropertyValue="Agree and Login"
Result="yes"
Function WebButton(ParentObject,PropertyRequired,PropertyValue,Result)
Call ReportResult(Result)
Dim hit
hit=0
If PropertyRequired="" then
PropertyRequired="name"
End If
If ParentObject.WebButton(PropertyRequired &":="&PropertyValue).exist then
ParentObject.WebButton(PropertyRequired &":="&PropertyValue).click
hit=1
End if
If hit>0 then
Reporter.reportevent micpass,"The button: "&PropertyValue,"Clicked sucessfully"
else
Reporter.reportevent micpass,"The button: "&PropertyValue,"not Clicked sucessfully"
End if
End Function
After I Call this function, then an error " Wrong number of arguments or invalid property assignments" displayed.
Call WebButton(ParentObject,PropertyRequired,PropertyValue,Result)
I just executed your piece of code .I dont find any issues .
Try using parenthesis when calling Reporter.reportevent so it's like this:
Reporter.reportevent (micpass, "The button: " & PropertyValue, "Clicked sucessfully")

QTP Link object does not support the Exist property

I have a weird problem with QTP 11. The following piece of code worked so far, but suddenly stopped working and throws Object does not support this property or method.: 'objPage.Link' for the line with 'if link exist'
Set objBrowser = Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1 & "")
Set objPage = objBrowser.Page("title:=.*")
If objPage.Link("class:=menu_link_tab_.*", "html id:=.*DesktopTab").Exist(3) Then
msgbox "ok"
End If
Can anyone tell me what is wrong and/or how to do it right?
EDIT: I solved this but still have no idea what happened. I just cut this part from QTP script and pasted it into Notepad and then copied it from Notepad to QTP. Everything works fine. I did not change anything... Any ideas what the hell happened are welcomed.
ANOTHER EDIT: The problem reappears randomly. With no changes to anything I can just run the test 10 times to have it fail randomly with the 'Object does not support this property or method' message
I have a startBrowser function where I set the objPage and objBrowser :
Function startBrowser(vURL)
Dim oDesc
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate vURL
Window("hwnd:=" & IE.HWND).Maximize
Set oDesc = Description.Create
oDesc( "micclass" ).Value = "Browser"
If Desktop.ChildObjects(oDesc).Count > 0 Then
Set objBrowser = Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1 & "")
End If
Set objPage = objBrowser.Page("title:=.*")
End Function
I have added lots of msgboxes with GetROProperty to verify whether the objects are ok. They seem to be.
Now my function to click the link looks like this :
Function clickMainMenuLink(vIdentifier)
Set objLink = objPage.Link("class:=menu_link_tab_.*", "html id:=.*" & vIdentifier, "index:=0")
If objLink.Exist(timeOut) Then
objLink.Click
Else
reporter.ReportEvent micFail, "Click the " & vIdentifier & " menu link", "Cannot find the " & vIdentifier & " menu link"
ExitTestIteration
End If
End Function
So at the moment my test just opens the browser and clicks a link as I try to debug the thing, but it still fails or passes randomly. The run error indicates line with 'Set objLink'. Any ideas for further debugging? I'm on QTP11 and IE8 if it matters.
From your error does not support this property or method: objPage.Link it would appear that the problem isn't with the .Exist part of line but the .Link part (you can verify this by separating the line into two lines and see which fails
set objLink = objPage.Link("class:=menu_link_tab_.*", "html id:=.*DesktopTab")
If objLink.Exist(3) Then
From your comment it seems that you're creating objPage in a different location from where you're using it, I suggest making sure that the object arrives OK.
Thanks Motti, your reasoning was right. The problem was with the objPage part. Despite the fact that I could read every property of objPage in a function QTP sometimes just did not see this objPage as a Page object. I guess it has something to do with not declaring type explicitly, but that's just a guess. As a dirty workaround I set up the objBrowser and objPage in every function now and it works 100%.

Getting Error as 'User defined type not defined' in the code

Greetings for the day,
Hi, I am a beginner using vb 6.0. I am using the following code and getting 'user defined type not defined'.the code is below.the line where i get error is highlighted.Kindly help.should i add some references or components?if so,what it would be. your timely and kindly help will be much more helpful for me
Public Sub LoadDocument()
Dim xDoc As MSXML2.DOMDocument
Set xDoc = New MSXML2.DOMDocument
xDoc.async = False
xDoc.validateOnParse = False
If xDoc.Load("C:\Users\284582\Desktop\XML1.xml") Then
DisplayNode xDoc.ChildNodes, 0
End If
End Sub
' Error on this line'
Public Sub DisplayNode(ByRef Nodes As MSXML.IXMLDOMNodeList, _
ByVal Indent As Integer)
Dim xNode As MSXML.IXMLDOMNode
Indent = Indent + 2
For Each xNode In Nodes
If xNode.NodeType = NODE_TEXT Then
Debug.Print Space$(Indent) & xNode.ParentNode.nodeName & _
":" & xNode.NodeValue
End If
If xNode.HasChildNodes Then
DisplayNode xNode.ChildNodes, Indent
End If
Next xNode
End sub
It's MSXML2.IXMLDOMNodeList, not MSXML.IXMLDOMNodeList.
The library may be missing from your references. Try this.
Manually adding MSXML2
1. Open MS Access.
2. Database Tools ribbon
3. Visual Basic ribbon item (icon)
4. Double-click on any module to open it.
5. Tools menu
6. References…
7. Find Microsoft XML, v6.0. is in the list
a. If in list but not checked, check it and click [OK].
b. If not in the list:
i. click [Browse…] and add "c:\windows\system32\msxml6.dll"
8. [OK] your way back to the Visual Basic window.
9. Close the Visual Basic Window. You should be good to go.
Programmatically adding MSXML2
Add the following sub and function. Run the sub. Edit the sub to include a path if necessary.
Check for broken references in the library
Adapted from Add references programatically
Sub CheckXmlLibrary()
' This refers to your VBA project.
Dim chkRef As Reference, RetVal As Integer ' A reference.
Dim foundWord As Boolean, foundExcel As Boolean, foundXml As Boolean
foundWord = False
foundExcel = False
foundXml = False
' Check through the selected references in the References dialog box.
For Each chkRef In References
' If the reference is broken, send the name to the Immediate Window.
If chkRef.IsBroken Then
Debug.Print chkRef.Name
End If
'copy and repeat the next 2 if statements as needed for additional libraries.
If InStr(UCase(chkRef.FullPath), UCase("msxml6.dll")) <> 0 Then
foundXml = True
End If
Next
If (foundXml = False) Then
'References.AddFromFile ("C:\Windows\System32\msxml6.dll") <-- For other than XML, modify this line and comment out line below.
RetVal = AddMsXmlLibrary
If RetVal = 0 Then MsgBox "Failed to load XML Library (msxml6.dll). XML upload/download will not work."
End If
End Sub
Add XML reference to the library
Developed by Chris Advena. Thanks to http://allenbrowne.com/ser-38.html for the insight.
Public Function AddMsXmlLibrary(Optional PathFileExtStr As String = "C:\Windows\System32\msxml6.dll") As Integer
On Error GoTo FoundError
AddMsXmlLibrary = 1
References.AddFromFile (PathFileExtStr)
AllDone:
Exit Function
FoundError:
On Error Resume Next
AddMsXmlLibrary = 0
On Error GoTo 0
End Function

Resources