Call Function

N

Nylex

I use a set routine in quite a few forms
I have written the function and Have tested it and it always get the correct
answer
When I return from the function I can not find the answer
This is how I call it

NewInvoice (LastInv)
Debug.Print InvDate, NextInv
Stop

Function NewInvoice(LastInv)
Dim nID As Integer

nID = Val(Mid(LastInv, 2))
NextID = "S" & Format(nID + 1, "00000")
NextInv = NextID

End Function

LastInv is a string and so is NextInv and NextID
 
S

Stefan Hoffmann

hi,
I use a set routine in quite a few forms
I have written the function and Have tested it and it always get the correct
answer
When I return from the function I can not find the answer
A function has one return value represented by its function name. So you
need to rewrite your function.
LastInv is a string and so is NextInv and NextID
Always declare your your variables using the correct type. Use

Option Explicit

to avoid typos.
So your module should look like this:

--
Option Compare Database
Option Explicit

Public Function NewInvoiceNumber(ALastInvoiceNumber As String _
) As String

On Local Error GoTo LocalError

Dim ID As Integer
Dim Result As String


ID = Val(Mid(ALastInvoiceNumber , 2))
Result = "S" & Format(ID + 1, "00000")

NewInvoiceNumber = Result

Exit Function

LocalError:
MsgBox Err.Description

End Function
--

Now you can use it:

MsgBox NewInvoiceNumber(LastInv)



mfG
--> stefan <--
 
F

fredg

I use a set routine in quite a few forms
I have written the function and Have tested it and it always get the correct
answer
When I return from the function I can not find the answer
This is how I call it

NewInvoice (LastInv)
Debug.Print InvDate, NextInv
Stop

Function NewInvoice(LastInv)
Dim nID As Integer

nID = Val(Mid(LastInv, 2))
NextID = "S" & Format(nID + 1, "00000")
NextInv = NextID

End Function

LastInv is a string and so is NextInv and NextID

What is NextInv?
You are calling a function named NewInvoice but you are setting (in
the final line before End Function) NextInv = NextID.
That line should be
NewInvoice = NextID

Try:
Function NewInvoice(LastInv)
Dim nID As Integer

nID = Val(Mid(LastInv, 2))
NextID = "S" & Format(nID + 1, "00000")
NewInvoice = NextID

End Function
 
D

Douglas J. Steele

First, your function is written incorrectly: you're not returning a value
from it, nor displaying one (which makes me wonder how you tested it and
knew it returned correct answers!). Also, I don't see that you've declared
variables NextID nor NextInv anywhere in the function.

Function NewInvoice(LastInv As String) As String
Dim nID As Integer

nID = Val(Mid(LastInv, 2))
NewInvoice = "S" & Format(nID + 1, "00000")

End Function

Now, to use it you'd have code like:

NextInve = NewInvoice (LastInv)
Debug.Print InvDate, NextInv
 
N

Nylex

Tks - that worked fine

The fields are in the original code before I called the function

In the function I put the debug.print statement and a stop line so I could
see what answer it was giving

Your solution cut a few liners of code of mine

Tks
 

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