I am using MS Access 2016, Windows 10, USAF Laptop running SDC.
I am having an issue with debugging some code. When I hit a line in Break mode I get a Run-time Error '13':Type Mismatch, When I run the exact same code outside of break mode, the code functions properly.
Here is the basic code that is causing me problems:
Private Sub btn_ReqKey_Click() 'This is not all of the code only those lines that are necessary
Dim trgt_CACID as String
Dim Temp_KeyCode as String
trgt_CACID = Me.CACID.Value 'Pulls value from short text field of "1234567890"
'Here is the issue
Temp_KeyCode = UnlockCode(trgt_CACID) 'Run-time Error '13': Type mismacth
More code
End Sub
Public Function UnlockCode(ByVal CACID as String) As String
lots of code that generates a 25 digit Alpha code
End Function
When I click on the button the code fails at "Temp_KeyCode = UnlockCode(trgt_CACID)" Run-tine Error '13': type mismatch.
However, if I reset the code execution and run this in the immediate window without pressing the button.
trgt_CACID = "1234567890"
?UnlockCode(trgt_CACID)
Returns the proper code without throwing an error. So the function UnlockCode() works properly, just not at execution.
Additionally, When in break mode from pressing the button and I attempt the same line of code in the immediate window, it returns the same run-time error '13': Type mismatch.
Furthermore, I exit Break mode, reload the variable and execute the same line of code it works again.
So I know something in the execution environment is being affected, I cannot figure out what that is. This is the first time I have seen a disparity with how the immediate window functions as compare to code execution.
Side Note/Quirk, in the immediate when I enter the function, the helper pops up to guide me for the variables required. When I type the same in the module code, the helper pop ups do not display. So weird. Right? Further evidence something is different between the two environments.
Any help or assistance would be greatly appreciated.
Thanks in advance.
KO
Based on the discussions with Erik A and Gustav, I changed the Function name that was having a conflict with a table field name I was using. Once I changed everything to be dissimilar: table Field Name, Form Textbox name, and Function being called. Everything worked fine. So apparently the context I was calling from already had something using the name of the function and that field name was being picked up and sent to a function with the same name as the field. This created a subtle conflict that explains the disparity between execution in the context of table and form and when being Manually called in the Immediate Window AND why it worked in the Immediate window, but not during execution. Thank you Erik A and Gustav for their assistance.
Try running it with a dummy function:
Public Function UnlockCode(ByVal CACID As Variant) As String
UnlockCode = String(25, "0")
End Function
If that runs, debug your secret function.
Related
I have a function that works fine when called within VBA regardless of what arguments I pass values to or do not pass values to.
The function also works when called from the control source of a text box on a continuous form so long as I pass values to all arguments.
The error occurs when changing the control source of the text box so that a value is not passed to the first optional argument. Specifically when trying to click out of either of those fields. So in other words it won't let me save the control source change as it thinks the syntax is wrong.The error occurs whether I change the control source using the property sheet or by typing within the text box.
I have tried specifying ByVal for the arguments and not using default values for the optional arguments but that did not work. I verified that the function is public and that there are no misspellings or data type mismatches. It definitely has something to do with the syntax when not passing a value to the first optional argument. (the two commas next to each other)
Control source that works:
=GetProfile(Nz([FieldName],0),"",Forms("FormName"))
Control source that causes error:
=GetProfile(Nz([FieldName],0),,Forms("FormName"))
The error is:
"The expression you entered contains invalid syntax. You may have entered a comma without a preceding value or identifier."
The function is listed below and it is within a module, not a class module. I simplified the guts of the function for brevity:
Public Function GetProfile(varPerson as Variant, Optional strLName as string = "", Optional frm as Access.Form = Nothing) as string
GetProfile = "Test"
End Function
I am pretty sure this is a bug. Can someone confirm or point out my mistake? If it is a bug, is there a work around?
MS Access (2007 - 2016) in Office 365
I'm trying/failing to capture a value passed into Access from the command line using the Command() function in a Macro. THis is the macro that I created with the wizard...
If Command()="Update_Burndown_Metrics" Then
RunSQL
SQL Statement insert .... blah, blah
End If
No error when I save the macro, but when I run...
The expression you entered has a function name that Microsoft Access can't find
If I replace the...
If Command()="Update_Burndown_Metrics"
with
If 1=1
It runs fine. IOW, it's not the SQL. It's the "Command() function that it can't find.
I got the idea to use Command() from Opening Microsoft Access with parameters . Doesn't seem to work for me. But that coding approach is also confirmed here... http://www.utteraccess.com/wiki/Command-Line_Switches . So I think it's something else.
Eventually, I would like to pass the Update_Burndown_Metrics arg using /cmd on the command line.
Why can't it find Command() as a valid function ? Is it a scoping thing? Do I have to give Command() context somehow, maybe with some sort of prefix ?
I can't seem to reproduce the issue that you are describing, though, I am using an earlier version of MS Access, and so there may be some differences in the behaviour of this function.
The Command function should be globally accessible, even outside of VBA (it can be referenced by the ControlSource property of a text box, for example), and so this isn't an issue of scope.
I do observe from the Office 365 documentation, that the Command function is being invoked without the use of parentheses in the sample VBA Sub provided; therefore it may be worth you trying your code without including such parentheses, e.g.:
If Command = "Update_Burndown_Metrics" Then
MsgBox "Test succeeded."
Else
MsgBox "Test failed."
End If
The workaround I created is to create a function in vba that gets and returns the command...
Public Function GetCommand() As String
GetCommand = Command()
End Function
Then...
If GetCommand() = "Update_Burndown_Metrics" Then ...
strong textNeed help to identify the error which is occurring while I'm referencing a Checkbox value inside a if statement in Form's OnLoad event Code. Basically checkbox name is "chkEditFinalized" and is placed in diff Form named "Settings" Under 3rd tab "ActionTriggers". Below is the event code.
Private Sub Form_Load()
Dim YorN As Boolean
If ([Forms]![Settings]![chkEditFinalized].Value = True) Then
'Codes to execute
Else
'Codes to execute
End If
End Sub
On the error message window it says "RunTime Error 2450, Application cannot find the referenced from settings"
Can somebody tell me what i have done wrong?
Edit----: As I know that my intention is not possible to implement(as explained by Wolfgang Kais), Can somebody advice me on how to make a global variable and make it accessible from all forms which I've tried and failed(Error:" Variable not defined" While running a sub function even the variable was declared in a Separate module). I know I may sound like a stupid because I'm no expert in this. If I can do this it will fulfill my requirement.
Expecting your valuable advice.
Thanks..
I would like Access to "automatically" play a song whenever a combo box is updated. I want that song to change depending on the contents of the combo box. Unfortunately the below code returns:
Constant expression required
Here's the code I'm working with:
Private Sub cboCustomerID_AfterUpdate()
txtSongFile = Me.cboCustomerID.Column(2)
Me.Refresh
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
Me![WindowsMediaPlayer1].openPlayer (conMEDIA_FILE_TO_OPEN)
End Sub
Any help would be greatly appreciated!! Thank you.
The error you mentioned is a compile error, not a run time error. It is caused by this line:
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
The reason is that the VBA rule for what you can use on the right side of the = sign is quite restrictive. This is what Access' online help says:
"Literal, other constant, or any combination that includes all arithmetic or logical operators except Is."
But you should be able to use a variable instead:
Dim strMEDIA_FILE_TO_OPEN As String
strMEDIA_FILE_TO_OPEN = Me.txtSongFile
I want to register a function kind of CalculateHours(work_hour, rest_hour) to Excel VBA, in an Addin like Works.xla
I tried using Application.MacroOptions to register the function. The following code is in a Class file, the function is in another module file. They would load when we open Excel and the Addin.
Code:
Private Function AddFunctions()
With MyFunction
Application.MacroOptions .Name, _
.Description, , , , , .Category, , , .HelpFilePath
End With
End Function
Expectation:
I wanna get the argument help, function description in Excel function wizard as other built-in functions. With the help button link to my help file.
Result:
Error number: 1004 Application-defined or object-defined error
or
Method "MacroOptions" of object "_Application" failed
Is there anything (or everything) wrong?
I have kind of 10 functions and need to add them automatically to Excel function wizard every time load the Addin.
07/12/2016 well after dragging my function around following pieces of advise from some posts and doing a number of other pointless things, I found this error happens if Application.MacroOptions Description:=FuncDesc exceeds 255 characters. So essentially don't get too verbose with the description of your user defined function, or just add a
If Len(FuncDesc) > 255 then
Scary warning message about Run-time error '1004'
End if
Another possible issue (and solution) that was affecting me... The function code needs to be written in a module - if it it written in the ThisWorkbook page, Excel won't be able to find the code.
For ease of future readers, here is a compilation of the multiple answers (i.e. list of potential things to check)...
The function code needs to be written in a module (not ThisWorkbook)
Make sure the function description does not exceed 255 characters
If defined in another workbook, try including the workbook name - e.g. Macro:="'PERSONAL.xlsb'!Macro/UDF_Name"
If defined in an Excel add-in, call ThisWorkbook.Activate before Application.MacroOptions
Hope this helps, please one-up the respective solution poster if their answer helped you 👍🏼
I understand that question is old, but will post my solution to this error, as it may be common and exception message is not informative.
I fixed it with passing a macro/UDF-holder workbook name into "Macro" parameter, like "'Workbook.xls(x/m/b)'!Macro/UDF_Name":
' Adding a macro from Personal.xlsb
Application.MacroOptions _
Macro:="'PERSONAL.xlsb'!Macro/UDF_Name", _
Description:="Description", _
ArgumentDescriptions:=ArgumentsDescription()
Another observation: if the Application.MacroOptions in being run from VBA add-in with ThisWorkbook.IsAddin = True e. g. from Workbook_AddinInstall or Workbook_AddinUninstall, and there is no other workbook opened, Excel throws error 1004. Problem can be solved if one calls ThisWorkbook.Activate before calling Application.MacroOptions.