Error HRESULT E_FAIL when iterating through for loop only - vb.net

I'm seeing the good old "System.Runtime.InteropServices.COMException HResult=0x80004005 Message=Error HRESULT E_FAIL has been returned from a call to a COM component" error when attempting to find an item via a for loop as shown below:
For i = 1 to itemList.Count
oObject = itemList.Item(i)
Next
But not if I hardcode the index, this finds item 1 without issue:
oObject = itemList.Item(1)
Obviously I don't want to do that and need to search through all the objects in my "itemList" to find the one I'm looking for.
I'm being intentionally vague because the software I'm working in is Dassault 3D Experience but am writing macros for it through Visual Studio 2017. I'm not sure where to even start debugging this sort of issue so any suggestions would be appreciated. Thanks.
Edit: adding full code of what I'm trying to do here (find an object, display its name, also select it on screen to double check. I will later add a check to make sure the object found in each loop is really what I'm looking for). All variables have been declared before this section.
selactive = CATIA.ActiveEditor.Selection
selactive.Clear()
product1Service = CATIA.ActiveEditor.GetService("PLMProductService")
oRootOcc = product1Service.RootOccurrence
cVPMOccurrences = oRootOcc.Occurrences
For i = 1 to cVPMOccurrences.Count
oVPMOccurrence = cVPMOccurrences.Item(i)
selactive.Add(oVPMOccurrence)
MsgBox(oVPMOccurrence.Name)
Next
The line that fails is oVPMOccurrence = cVPMOccurrences.Item(i)

Can you do something like this with a For Each loop?
For each oVPMOccurrence as oRootOcc.Occurrence in cVPMOccurrences.Items
selactive.Add(oVPMOccurrence)
MsgBox(oVPMOccurrence.Name)
Next
Using a For Each means you don't have to worry at all about the index
Not sure what the type of oVPMOccurrence is as you haven't specified

Most indexes in .net are zero base. I don't know what itemList is but I suspect the index of the first item is 0.
For i = 0 to itemList.Count - 1
oObject = itemList.Item(i)
Next
Not sure why you want to overwrite the value of oObject on every iteration.

Related

Check if a property of an object has value or not

I couldn't find a solution for this on SO or other source. I apologize in advance if this is a tired question:
I have a macro that iterates through placeholders in shapes. Some of these placeholders have text, some don't. I'm trying to figure out if
.Shapes.Placeholder.TextFrame.TextRange has any value or not (In the 'watches' section of the VBA editor, this property has <The specified value is out of range.>). If it does, do something. Otherwise, continue the For Each statement. However, everything I tried has thrown out of bounds errors or something of that type.
I have tried:
If Not (placeholder.TextFrame.TextRange.Length = 0) Then: Immediately throws out of bounds error
Try/catch, which doesn't seem to exist in VBA (Gives me Sub or Function not registered)
If Not CStr(placeholder.TextFrame.TextRange) = "0" then
I'm quite out of my depth as I don't have experience with any Visual Basic iteration. I just need to, if that property is empty or null, skip over the placeholder.
EDIT: Following suggestions, I tried this:
For Each placeholder In sld.NotesPage.Shapes.Placeholders
If Not (placeholder.TextFrame Is Nothing) Then
If Not (placeholder.TextFrame.TextRange Is Nothing) Then
If Not (placeholder.TextFrame.TextRange.LanguageID Is Nothing) Then
placeholder.TextFrame.TextRange.LanguageID = lng
End If
End If
End If
Next
I still get an out of bounds error on placeholder.TextFrame.TextRange Is Nothing. Did I make a syntax error?
FINAL EDIT: Turns out there was an answer in SO after all. For this specific problem, it was enough to add On Error Resume Next right after theFor Each.

EPSDK.Recordset not looping until EOF (end of file)

I am currently half way through a project where I am migrating data from an ancient Adobe Workflow server using Visual Basic and COM.
I have hit a bit of a brick wall really as I am trying to perform a simple while loop that counts the number of records in a recordset, and I keep getting this error...
"An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll
Additional information: Unspecified error"
There is little to no documentation to help me online so I am hoping there is some sort of VB wizard/veteran that can point me in the right direction.
I have set the record as a global variable like so...
Dim record As New EPSDK.Recordset
I have then tried...
Dim recCount As Integer = 0
Do Until record.EOF
recCount += 1
Loop
This...
Dim recCount As Integer = 0
Do While Not record.EOF
recCount += 1
Loop
This...
Dim recCount As Integer = 0
Do
recCount += 1
Loop Until record.EOF
And lots of other variations, but still cannot seem to source the problem. There are no code errors, nothing comes up in the console, and I just keep getting that message back.
Can anyone spot what I am doing wrong? Thanks
Ok, I've looked up the documentation for EPSDK. For those who are unaware (as I was), it's an object collection from Adobe for manipulating COM data. It's basically the most popular functionality in ADO.
MoveFirst, as its name suggests, moves to the first record in a recordset. There doesn't appear to be any such method supported by the EPSDK Recordset object. Since you can use the Move method to do the same thing, it isn't needed. In either case, you don't need to use it to move to the end of the file.
What you're doing wrong is expecting that you can increment a variable called recCount that you made up and the recordset cursor will magically move along. Doesn't happen. As you say, the doc is insubstantial, but you probably need to use MoveNext. Here's a cheat sheet you can use to look up what's supported.
Also, you need to specify a connection, open it, point the recordset to the open connection, and open the recordset. I would suggest that you familiarize yourself with ADO (NOT ADO.Net! Not the same thing), upon which this is clearly based. There's much more documentation, and it should apply fairly well. Read up on Connections and Recordsets in particular.
Now, your loops do pretty much the same thing. While Not is the equivalent of Until. However, if you put the while/until condition after the Do statement, you won't enter the loop unless the condition is met. If you put it after the Loop statement, you will always run the loop at least once. In this case, you should put "Do Until myRecordset.EOF", because then if the recordset is empty, you won't go into the loop.

How can I rewrite this Visual Basic.net loop to make it work?

I have a For Each loop written in Visual Basic that's trying to modify a table it is iterating through. This is causing the ""Collection was modified; enumeration operation might not execute" exception.
What is frastrating about this is that I've tried to make copies of all the objects that I was using and and only removed from the copy but the exception still happened.
I know that this is due to my using a For Each loop instead of a For or While loop but I could not rewrite my code and make it work (I'm more familiar with C#). This is why I decided to ask for help here.
This is my code. How can I rewrite to be able to remove the Row that I wish to remove? Any help and code would be very appreciated!
Dim drAcademicRecord, drSchool As DataRow
For Each drSchool In dsR.Tables("School").Rows
If drSchool("OrganizationName") = "NA" Then
For Each drAcademicRecord In dsR.Tables("AcademicRecord").Rows
If drAcademicRecord("AcademicRecord_Id") = drSchool("AcademicRecord_Id") Then
dsR.Tables("AcademicRecord").Rows.Remove(drAcademicRecord) '<-- This is the line causing my exception
End If
Next
End If
Next
For i as integer = dsR.Tables("AcademicRecord").Rows.Count - 1 to 0 Step -1
If dsR.Tables("AcademicRecord").Rows(i)("AcademicRecord_Id") = drSchool("AcademicRecord_Id") Then
dsR.Tables("AcademicRecord").Rows.RemoveAt(i)
End If
Next
You don't need to use
dsR.Tables("AcademicRecord").Rows.Remove(drAcademicRecord)
Because you have already got drAcademicRecord, so you can directly delete that row by
drAcademicRecord.Delete
e.g.
For Each drSchool As DataRow In dsR.Tables("School").Select("OrganizationName='NA'")
For Each drAcademicRecord As DataRow In dsR.Tables("AcademicRecord").Select("AcademicRecord_Id=" & drSchool("AcademicRecord_Id"))
drAcademicRecord.Delete
Next
Next

Object reference not set to an instance of an object (Completely broken?) in vb.net

I know, I know, I could have used a for loop, dont tell me anything about that. Please, help!
Private Function LoadSaved() ''//Loads saved clippings if the user wants us to
Dim ZomgSavedClips As StringCollection
If IsDBNull(My.Settings.SavedClips) = False Then ''//If it is null this would return a rather ugly error. Dont want that do we?
ZomgSavedClips = My.Settings.SavedClips ''//ZomgSavedClips name was a joke, I just felt like it.
ZomgSavedClips.Add(" ") ''//This line ought to fix the error, but doesnt
i = 0
While i < ZomgSavedClips.Count ''//This is where the error occurs
ClipListings.Rows.Add(ZomgSavedClips(i))
i = i + 1 ''//First time I wrote this function I forgot this line. Crashed mah comp. Fail.
End While
End If
End Function
The line While i < ZomgSavedClips.Count is bugging, I know that the .count should return null but I even added a blank piece of text just to stop that. Whats up with this? Should I add actual text?
SavedClips is null no? If it is null it could pass the test IsDBNull beacuse the both are not the same
Obviously, My.Settings.SavedClips is still set to Nothing.
SavedClips is regular 'ole null (nothing in VB). Include a check for "My.Settings.SavedClips is nothing". If that evaluates to true then just leave the function.
I even added a blank piece of text just to stop that.
All you did was move where the error happens. You can't call .Add() on a null/Nothing object.
'''<summary>Loads saved clippings if the user wants us to</summary>'
Private Sub LoadSaved() ''//Loads saved clippings if the user wants us to
''//Load saved clips into memory
Dim ZomgSavedClips As StringCollection = My.Settings.SavedClips
If ZomgSavedClips Is Nothing Then ZomgSavedClips = New StringCollection()
''//Apply loaded clips to visible listings
Dim i As Integer
While i < ZomgSavedClips.Count ''
ClipListings.Rows.Add(ZomgSavedClips(i))
i += 1
End While
End Sub
Some notes on this code:
Don't use Function when you mean Sub
Since you'll be selling this code to others, you want to use xml comments at the top so that Visual Studio can give better intellisense helps.
IsDBNull() doesn't do what you think it does.
Yes, you should use a for loop, but since you already commented on that I left the while loop alone with the assumption that there's more code you didn't show us.

Openform action was canceled in MS-Access VBA code

I am supporting an application which was running for the past 3 years. It was developed completely in MS Access and written in VBA.
Suddenly the application is facing the mentioned error at the following lines:
DoCmd.OpenForm FormName:="frmNewPeerGroup", View:=acNormal, windowmode:=acWindowNormal, OpenArgs:=5
FrmNewPeerGroup code
Private Sub Form_Open(Cancel As Integer)
Dim lDept As Long, lDiv As Long
lType = OpenArgs 'Supplied by caller
lAssmtVer = 1 'Current
sName = ""
sDescription = ""
dtCreatedDate = Format(Now(), "dd/mm/yyyy")
sCreatedBy = UCase(userPerms.NTLoginName)
lSupervisorID = userPerms.userID
lTeam = 0
With cmbBxType
.RowSourceType = "Value List"
.RowSource = GetValueListDict(pgType)
.Value = lType
.Enabled = (OpenArgs = 1)
End With
With cmbBxVersion
.RowSourceType = "Value List"
.RowSource = GetValueListDict(pgAssmtType)
.Value = lAssmtVer
End With
mgLogoDesc.Visible = False
txtBxCreatedDate.Value = dtCreatedDate
txtBxCreatedBy.Value = sCreatedBy
If OpenArgs = 5 Then
lTeam = oActiveAssmt.TeamID
lDept = GetParentID(aTeams(), CInt(lTeam))
lDiv = GetParentID(aDepts(), CInt(lDept))
With cmbBxDivision
.RowSourceType = "Value List"
.RowSource = GetValueListArray(aDivs())
.Value = lDiv
.Enabled = False
End With
With cmbBxDepartment
.RowSourceType = "Value List"
.RowSource = GetValueListArray(aDepts())
.Value = lDept
.Enabled = False
End With
With cmbBxTeam
.RowSourceType = "Value List"
.RowSource = GetValueListArray(aTeams())
.Value = lTeam
.Enabled = False
End With
Else
With cmbBxDivision
.RowSourceType = "Value List"
.RowSource = GetValueListArray(aDivs())
.Enabled = False
End With
cmbBxDepartment.Enabled = False
cmbBxTeam.Enabled = False
End If
End Sub
Many instances of the DoCmd.OpenForm command are giving the error in a message box saying:
The expression On Click you entered as the event property setting
produced the following error: The OpenForm action was canceled.
- The expression may not result in the name of macro, the name of
a user-defined function, or [Event procedure].
- There may have been an error evaluating the function, event, or macro.
This is the error message I am receiving.
My problem is, the same code was running around 3 years, but suddenly some updates to Microsoft or Office might be giving trouble to this code.
Did anyone come across this error in the past weeks? Please let me know what else we can do to make this work again.
This thread is very old but I came across the same error and spent a few hours looking for an answer. I was able to find the cause after some time and thought of posting my answer as it may help someone in a similar situation. Creating a application using Access Forms is new to me, so the error message was not directly intuitive.
My forms were Master table data entry forms and configured to be Pop-up and Modal with Me.Form.Name sent as parameter in the DoCmd.OpenForm command using a button (OnClick event) placed next to the Combo controls on a transaction form to allow user to quickly add new records. This parameter value was picked up in the Form_Open(Cancel As Integer) event and used later to refresh the combo box (Forms!<formname>.Controls!<controlname>.Requery) once data was submitted to the master table using the pop-up form.
It appears that the Open event doesn't occur when you activate a form that's already open (ref: https://msdn.microsoft.com/en-us/library/office/aa211439(v=office.11).aspx). Each time I received the error, my data entry form was open in Design view in Access. So I closed the form in design mode, and repeated the steps. And Voila! no error!
Since I will have more than one forms open, I now need to test and try to use Form_Activate() as recommended in the above MSDN reference link.
I don't know if this qualifies as an answer, but the code in that OnOpen event is dependent on a lot of outside functions. Specifically, the code is assigning value lists for the RowSources of a bunch of combo boxes. The immediate red flag that occurs to me is that non-SQL Rowsources have a finite length, and in Access 97, that limit was 2048 characters (in Access 2003, it's 32,750 -- don't ask me why it's that number!).
So, the immediate thing I see is that perhaps what ever data drives the functions that create those value lists has begun to exceed 2048 characters in length.
If that's the actual answer, then you can write a callback function that will return the values in the arrays, and it won't have the limitation on the returned length. You'd set the RowsourceType to the name of your callback function and leave the Rowsource property blank.
An example of the callback function is found in the A97 help (though I can't find the same example in the A2K3 help). In A97 help, you get there by searching for RowsourceType, and then in the help window, click on the link in the sentence reading "You can also set the RowSourceType property with a ____user-defined function____."
To check this out, you just need to find out the length of the string returned from GetValueListArray() by each of the arrays referenced in the OnOpen event.
It also might be helpful to add an error handler to the OnOpen event, particularly given that there are so many outside dependencies in the code in that particular sub.
And last of all, let me say that it looks like horrible programming. Most of this ought to be settable with default properties, seems to me. I also question that kind of dependency on OpenArgs with such an undocumented input value. What does "5" mean? And what does "1" mean? Is that documented somewhere? It's just terrible, terrible code, in my opinion.
I'd likely do this with a standalone class module instead, because that will be self-documenting in terms of what does what. You'd set a particular named property to 5 and that would control what the form gets from the class module methods for populating the combo boxes. It would all be in one place, and you could use a meaningful property name to make it clear what the values 5 and 1 represent. It's particularly helpful to do this if you have the same kind of code in the OnOpen event of multiple forms. In that case, it's a no-brainer to move it out of the form modules, and the only question is whether you put it in a regular module or in a standalone class module (as I'm suggesting).
Anyway, perhaps none of this is on point, but it might give you some ideas.
Could it be the security settings is Access? All recent versions of Access has a security settings dialog where you can enable (or disable) macros in the application. I think you will get this error if macros are disabled.
Are you sure one of the required references (VBA IDE > Option > References) isn't missing?
If you're referencing Excel/Word or external objects, are you sure that the references to the type libraries are the right ones (if you're using specific versions instead of doing late binding)
Are you building the MDE on a 64 bit machine by any chance?
What is the code on the form frmNewPeerGroup? What version of Access are you using? If it is 2003, sp3 causes problems for which there is a hotfix. Have you tried decompile and / or compact and repair?
If you have an original mdb, check the references to make sure that none of them are marked MISSING. This is quite a likely reason for problem in that it has suddenly occurred.
To check the references, look at Tools->References on the menu for a code window.
If no references are missing, you could try stepping through the form code to get a more exact idea of where the error is occurring.