Newbie needs help writing a function

T

Teri

Hey all, I am trying to write a function for a Module. Here is my code:

Public Function fncModelNo(OptionKey As Variant)
Dim ModelNo As Variant

ModelNo = Left([OptionKey], InStr([OptionKey], ".") - 1)
Debug.Print ModelNo

End Function

When I try to run this I receive a Compile error: Argument not optional.
What help tells me is that I am probably missing an argument on part of my
code, unfortunately I seemed to not be able to find it.

If I put just the Left(**********) as an expresion on a query, then the code
works, but I am trying to place the code in a module so that I can use it in
other places.
 
F

fredg

Hey all, I am trying to write a function for a Module. Here is my code:

Public Function fncModelNo(OptionKey As Variant)
Dim ModelNo As Variant

ModelNo = Left([OptionKey], InStr([OptionKey], ".") - 1)
Debug.Print ModelNo

End Function

When I try to run this I receive a Compile error: Argument not optional.
What help tells me is that I am probably missing an argument on part of my
code, unfortunately I seemed to not be able to find it.

If I put just the Left(**********) as an expresion on a query, then the code
works, but I am trying to place the code in a module so that I can use it in
other places.


Why are you declaring Variants when they appear to be strings?
You can test to see what the value passed to the function actually is.

Public Function fncModelNo(OptionKey As String) as String
Debug.print OptionKey
Dim ModelNo As String

ModelNo = Left([OptionKey], InStr([OptionKey], ".") - 1)
Debug.Print ModelNo
fncModelNo = ModelNo
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top