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

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.

Related

Variable in a form won't keep its value after being used in the call to another form

I have a form with a variable in it called "VigilTable." This variable gets its value from the calling string OpenArgs property.
Among other things, I use this variable in the call string when opening other forms.
But it only works the first call.
MsgBox VigilTable before the call will always show "Spring2022" or whatever on the first call but always comes up blank on succeeding calls (and I get "invalid use of NULL" when the called form attempts to extract the value from OpenArgs). The variable is dimmed as String in the General section of the form's VBA code.
So what's happening here? And can I fix it?
Thanks.
Ok, so you delcared a variable at the form level (code module) for that given form.
and we assume that say on form load, you set this varible to the OpenArgs of the form on form load.
So, say like this:
Option Compare Database
Option Explicit
Public MyTest As String
Private Sub Form_Load()
MyTest = Me.OpenArgs
End Sub
Well, I can't say having a variable helps all that much, since any and all code in that form can use me.OpenArgs.
but, do keep in mind the following:
ONLY VBA code in the form can freely use that variable. It is NOT global to the applcation, but only code in the given form.
However, other VBA code outside of the form can in fact use this variable. But ONLY as long as the form is open.
So, in the forms code, you can go;
MsgBox MyTest
But, for VBA outside of the form, then you can get use of the value like this:
Msgbox forms!cityTest.MyTest
However, do keep in mind that any un-handled error will (and does) blow out all global and local variables. So, maybe you have a un-handled error.
Of course if you compile (and deploy) a compiled accDB->accDE, then any errors does NOT re-set these local and global variables.
but, for the most part, that "value" should persist ONLY as long as the form is open, and if you close that form, then of course the values and variables for that form will go out of scope (not exist).
Now, you could consider moving the variable declare to a standard code module, and then it would be really global in nature, but for the most part, such code is not recommended, since it hard to debug, and such code is not very modular, or even easy to maintain over time.
So, this suggests that some error in VBA code is occurring, and when that does occur, then all such variables are re-set (but, the noted exception is if you compile down to an accDE - and any and all variables will thus persist - and even persist their values when VBA errors are encountered.
For a string variable, a more robust solution not influenced by any error, should be writing/reading in/from Registry. You can use the, let as say, variable (the string from Registry) from any workbook/application able to read Registry.
Declare some Public constants on top of a standard module (in the declarations area):
Public Const MyApp As String = "ExcelVar"
Public Const Sett As String = "Settings"
Public Const VigilTable As String = "VT"
Then, save the variable value from any module/form:
SaveSetting MyApp, Sett, VigilTable , "Spring2022" 'Save the string in Regisgtry
It can be read in the next way:
Dim myVal as String
myVal = GetSetting(MyApp, Sett, VigilTable , "No value") 'read the Registry
If myVal = "No value" Then MsgBox "Nothing recorded in Registry, yet": Exit Sub
Debug.print myVal
Actually, this proved not to be the the answer at all.
It was suggested that I declare my variables as constants in the Standard module but I declared them as variables. It appeared at first to work, at least through one entire session, then it ceased to work and I don't know why.
If I declare as constants instead, will I still be able to change them at-will? That matters because I re-use them with different values at different times.
I didn't do constants but declaring VigilName in the Standard module and deleting all other declarations of it fixed both problems.
While I was at it I declared several other variables that are as generally used and deleted all other declarations of them as well so that at least they'll be consistently used throughout (probably save me some troubleshooting later.
Thanks to all!

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?

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

Run Time Error 1004 When Inserting Rows/Columns

So I'm running into the lovely run time error 1004: Application defined or object defined error.
The bear of it is, half the time the code in VBA runs correctly with no problems, and the other half it falls on its face. The third line ("Mgmt.List.....") is run in a loop normally, but it has no problems messing up on me in this simple macro.
Sub whyyyy()
Dim Mgmt As Worksheet
Set Mgmt = Sheets("Mgmt MarginAnalysis")
Mgmt.ListObjects("Table4").ListRows.Add (3)
End Sub
The line
Mgmt.ListObjects("Table4").ListRows.Add (3)
is extra special, as when it's run in the loop, it will sometimes work the first time, but fail the second time. Or it will just fail outright.
Don't really know what makes it work and what doesn't. This just started happening, and the worksheet/table combination seems to only happen to tables I've just inserted recently (not by using VBA).
Are you sure, the Sheets("Mgmt Margin Analysis") has table named "Table4". If you are creating the table on runtime, chances are that, it will not be assigned the same name ("Table4") everytime it is created.
Please ensure you assign this name in your code, and do not let Excel to choose the name for you.

Object variable or With block variable not set

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)