Object variable or With block variable not set - vb.net

I am trying to figure out why all of the sudden i get an error when trying to run my program. The following code below is where the error is:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> Partial Class frmPM
The error i get is highlighted on the frmPM:
An error occurred creating the form. See Exception.InnerException for details.
The error is: Object variable or With block variable not set.
I've made sure there was a End Class at the end of all that but that doesn't seem to be the problem.
What else could cause this as it worked just fine for one run then not on down the road?
Thanks!
David

Ah, it seems as though it was creating a bad object at run time in a sub it was calling when it first started. Though its odd that it would have that type of error but after i commented that out, it seemed to work just fine. :o)

Related

MS Access Run Time Error 2465 Can't find the field 'employeeNum' referred to in your expression

Just finished tweaking a query, then started testing the change and then this issue popped up.
In a Module called GLOBALS I declare a number of public variables. One of which is this:
Public employeeNum As Long ' PO entering/modifying data
In form frmSearch I assign this variable. For some reason I am getting a run time that I catch in my Form_Load() procedure.
Here is a pic of when I stop at the breakpoint and hoover over the global variable employeeNum.
Any thoughts on this strange error?
Attempts to fix include the following:
Ran Compact and Repair Database a couple of times and no luck.
Ran /decompile to clean things up. No luck.
Compiled Database to validate no issues and no errors detected.
Changed Public to Global when declaring employeeNum. No luck.
Tried to debug.print employeeNum early in the procedure but I still would get the runtime error on the debug.print line.
Thanks
This problem is caused by two variables being named the same. A global variable and a local variable (employeeNum). The various replies gave me the clue I needed and I used the Object Browser to validate the problem. When I tried to find the object, the Object Browser stated it was hidden which is annoying but I’ll go through all the embedded code, etc. and figure out where it is just so I know.
I renamed the global variable employeeNum to employeeNumber throughout the application and everything works fine.

Using progressbar in VBA

I am trying to update some code that I didn't originally write.
I'm getting an error right out of the gate with ProgressBar, but I can't seem to identify what the problem is.
My code is:
Public Function CharStyles() As Dictionary
Dim CharacterProgress As ProgressBar
Set CharacterProgress = New ProgressBar
' some more code after this...
End Function
Hovering over 'New ProgressBar' - I see the error "ProgressBar =Object variable or With variable not set", but I can seem to find any information on using ProgessBar in VBA to get any understanding of what that error means.
Can anyone help me understand what the issue is here? Presumably this code worked before, so I'm not sure what I'm missing that's causing it to break.
Thank you.

Explicit "WorkSheet.Range" Fails With “Method or Data Member Not Found”

I have the following code:
Dim newWorksheet As Worksheet
Set newWorksheet = ThisWorkbook.Sheets.add()
Set pivotCache = ThisWorkbook.PivotCaches.create(SourceType:=xlExternal, SourceData:=cnSet)
pivotCache.CreatePivotTable newWorksheet.Range("A1"), newWorksheet.Name
Works great 99% of the time, however today a customer sent me a version of the file that was not working, when I open the file and attempt to compile the code I get the following issue with the last line of code:
Method or Data Member Not Found
In fact you can even see that the property is missing in the autocomplete:
It appears as though it's recognizing the "Worksheet" object as a "chart":
Strange, so I covert the "newWorksheet" over to a variant, compile, works great. Now I change it back, compile, and... it works, issue is gone, code runs great. Very strange, you can even see it in the autocomplete:
(Now it's object type "Worksheet")
I don't understand what would cause this or how to prevent it in the future, the issue only occurs with this one file. The user was not doing anything exceptional with it as far as I can tell. This issue is documented but typically is related to other problems. I don't see any explicit reference to this occurring on a per file basis.
Does anyone have any insight on this one, this is one of those "only in VBA" type of issue?

Second CopyPicture in VBA fails with "Automation error"

So I am copying and pasting a bunch of data to another spreadsheet. I thought this last bit was going to be easy since I've done this a bunch of times in this script.
Except it fails. Here's part of my code:
ProdCK.Worksheets("CK week").Range("A11:AY28").CopyPicture Appearance:=xlScreen
Worksheets("Prod.CK").Paste Destination:=Worksheets("Prod.CK").Range("A1")
ProdCK.Worksheets("CK week").Range("I131:BO148").CopyPicture Appearance:=xlScreen
Worksheets("Prod.CK").Paste Destination:=Worksheets("Prod.CK").Range("A22")
The first one works fine, but then the second one crashes on the CopyPicture operation. I've checked that a range is actually present by first storing that in a range, checking if the data is there in the "watch" screen and then calling CopyPicture on that, but nonetheless is still gives a
424 error Object required
Can anyone shine a light on this for me?
UPDATE:
The error is actually
-2147417851 Automation error The server threw an exception
My error handling code had a bug which turned the automation error into the Object required error. What is also interesting is that the error does not happen when I use a visible Excel.Application in this Sub. The only difference between my visible and invisible settings are Visible = True and ScreenUpdating = True.
I haven't been able to figure out why but currently I just set App.Visible = True before these two statements and set it back to App.Visible = False afterwards. This gets things working.
I really dislike it though.

Colleagues get error 5 in Excel-file with VBA, but same file works fine on my computer?

We have an Excel-file with a large amount of VBA behind it. The Excel-file works just fine on my computer, but so far 3 of my colleagues (non-IT'ers) have gotten this error:
Runtime error 5:
Invalid procedure call or argument
The error is located on this line, and I don't see why it is throwing an error there because it's just a simple Set (and it works perfectly fine on my computer):
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
We all have the exact same Excel-file. I even sent them my version of the file, in which everything is running fine and no errors happen, but even when they open my version of the file they still get the above error on the above line!
What exactly could cause this? We all have Office 2013 and updates are installed automatically. The problem started about 2 weeks ago with one colleague and since this week I heard from two other colleagues that they have the same problem. One even said it suddenly worked again after he moved to a different desk (which I doubt would have an influence) but shortly after, it started getting the error again.
I have absolutely no idea why they get the error, or what might cause it. Seeing as we now all have the same version and they still get the error, I am thinking it might have something to do with Excel itself but that's just my idea.
Does this sound familiar to anyone here? Or does anyone know what might cause this, and how it can be fixed?
Edit: a while ago I checked on my colleagues their computers to see if the CommandBar was present, and it was. Even then the error still happened.
I suggest you to use a function that checks if there is a CommandBar in your Application like this:
Function IsCommandBarValid(cbName As String) As Boolean
Dim i As Long
IsCommandBarValid = True
For i = 1 To Application.CommandBars.Count
If (Application.CommandBars(i).Name = cbName) Then
Exit Function
End If
Next i
IsCommandBarValid = False
End Function
Now, You can use it to see that your user have that CommandBar in his or her Application, then make it like this:
If (Not IsCommandBarValid("Attributions")) Then
Call Application.CommandBars.Add(Name:="Attributions")
End If
' And after this add your code
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
Invalid procedure call or argument (Error 5)
Some part of the call can't be completed. This error has the following causes and solutions:
An argument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
Check the ranges permitted for arguments.
This error can also occur if an attempt is made to call a procedure that isn't valid on the current platform. For example, some procedures may only be valid for Microsoft Windows, or for the Macintosh, and so on.
Check platform-specific information about the procedure.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
MSDN Source Article