Attach a subroutine to an unbound Text box?

Will do that. Thanks again. I think I understand now.
 
So I erased my function and just placed your Test() function into the control source. The same thing happened. When I tried to print preview the form the first thing that happens (as was happening in my function) is the a msgbox pops up and asks for the parameters of Test(). Do you have any idea why this is happening?
 
So I erased my function and just placed your Test() function into the control source. The same thing happened. When I tried to print preview the form the first thing that happens (as was happening in my function) is the a msgbox pops up and asks for the parameters of Test(). Do you have any idea why this is happening?
Hi. You'll have to show us exactly what you did for us to tell you why it didn't work. For example, rather than using =test(), try using =Date(). Do you still get an error?
 
Wow, that is the first time it actually worked. Put Date() into the control source and it placed the date in the field as it is supposed to do. Does that suggest that the function as written was incorrect?
 
Will spend some time trying to get a function properly written. Let you know if it gets done.
 
Wow, that is the first time it actually worked. Put Date() into the control source and it placed the date in the field as it is supposed to do. Does that suggest that the function as written was incorrect?
No, it doesn't mean that. There are two factors, why it might not work. How the function was written, and how it was called. The Date() function does not need an argument, so you can just use =Date(), and you will get the current date back. If you wrote the function to accept arguments, then you'll have to pass it to the function call. For example, in the code I showed you earlier, I used it this way: =AddThemUp([box1],[box2]). That's because the function AddThemup() accepts/expects two arguments. That's why we need to "see" exactly how you called the function (what exactly did you put in the textbox) to tell you why it didn't work.
 
I wrote this in the control source box =SplitTotal() But I think I see that in the control source box I have to pass both arguments also in the parenthesis of the function, is that correct?
 
I wrote this in the control source box =SplitTotal() But I think I see that in the control source box I have to pass both arguments also in the parenthesis of the function, is that correct?
That's correct, assuming you wrote the function to accept two arguments.
 
I see. Sorry I am so thick. I will rewrite the function with that in mind. Thanks again for your help.
 
So I rewrote the Function today and changed the arguments to Code1 and Code2. Here is the code I wrote
Option Compare Database
Option Explicit

Function SplitTotal(Code1 As String, Code2 As String)

If Code1 = "AL" Or "AS" Then
TotalA = [SPLIT1] * [Fee]

If Code1 <> "AL" Or "AS" Then
TotalA = [Amount]

If Code2 = "AF" Then
TotalB = [Split2] * [Fee]

If Code2 <> "AF" Then
TotalB = Null

End If
End Function

Then I placed this into the control source for the TotalA text box on the report. =SplitTotal([CODE1],[CODE2])
When I ran the print preview I again got the msgbox asking for the parameter Code1 and Code2.
Not there yet. Any ideas.?
 
So I rewrote the Function today and changed the arguments to Code1 and Code2. Here is the code I wrote
Option Compare Database
Option Explicit

Function SplitTotal(Code1 As String, Code2 As String)

If Code1 = "AL" Or "AS" Then
TotalA = [SPLIT1] * [Fee]

If Code1 <> "AL" Or "AS" Then
TotalA = [Amount]

If Code2 = "AF" Then
TotalB = [Split2] * [Fee]

If Code2 <> "AF" Then
TotalB = Null

End If
End Function

Then I placed this into the control source for the TotalA text box on the report. =SplitTotal([CODE1],[CODE2])
When I ran the print preview I again got the msgbox asking for the parameter Code1 and Code2.
Not there yet. Any ideas.?
Hi. Please review my sample function again. I used box1 and box2 to call the function but defined the arguments as val1 and val2. So, depending on whether Code1 and Code2 are the fields in your table or not, you will have to apply what I showed you earlier. Also, your function is still not returning a value, because you haven't assigned the return value to the function. If you need help creating and using the function, consider posting a demo version of your db, so we can show you how to do it using your own field names.
 
Last edited:
1585935768089.png
1585935768089.png
This is a screen shot of the subreport section of the report. The only fields on it are the Service date, code1 and code 2, description1 and 2, and the two unbound textboxes on the right. In your addThemUp function the Val1 and val 2 are I think my two text boxes. Is that correct? Then in the control source your box1 and box2 refers to the two arguments in the function? Is this enough to see what I am trying to do, or do you need more?
 
View attachment 80521View attachment 80521
This is a screen shot of the subreport section of the report. The only fields on it are the Service date, code1 and code 2, description1 and 2, and the two unbound textboxes on the right. In your addThemUp function the Val1 and val 2 are I think my two text boxes. Is that correct? Then in the control source your box1 and box2 refers to the two arguments in the function? Is this enough to see what I am trying to do, or do you need more?
Hi. It would be nice to see an actual copy of your db. However, did you modify your function to return a value yet? I mentioned it in my last post (now bolded for emphasis).
 
PMFJI, but I think you would also need
Code:
If Code1 = "AL" Or Code1 = "AS" Then
instead of
Code:
If Code1 = "AL" Or "AS" Then

Same with the other if statements.?
 
PMFJI, but I think you would also need
Code:
If Code1 = "AL" Or Code1 = "AS" Then
instead of
Code:
If Code1 = "AL" Or "AS" Then

Same with the other if statements.?
I thought that as well when I wrote it. Will change it.
 
This is your example. I think the assignment is the Test statement.
Public Function Test() As String
Test = "It is now " & Now()
End Function

Why isn't my statement an "assignment statement". I don't understand.

TotalA = [SPLIT1] * [Fee]
 
PMFJI again

A function normally returns a value.

That value is 'assigned' to the name of the function, so in your case you would need

SplitTotal = TotalA or TotalB or whatever.
 

Users who are viewing this thread

Back
Top Bottom