Attach a subroutine to an unbound Text box? (1 Viewer)

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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?
 

theDBguy

I’m here to help
Local time
Today, 12:53
Joined
Oct 29, 2018
Messages
10,232
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?
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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?
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
Will spend some time trying to get a function properly written. Let you know if it gets done.
 

theDBguy

I’m here to help
Local time
Today, 12:53
Joined
Oct 29, 2018
Messages
10,232
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.
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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?
 

theDBguy

I’m here to help
Local time
Today, 12:53
Joined
Oct 29, 2018
Messages
10,232
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.
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
I see. Sorry I am so thick. I will rewrite the function with that in mind. Thanks again for your help.
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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.?
 

theDBguy

I’m here to help
Local time
Today, 12:53
Joined
Oct 29, 2018
Messages
10,232
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:

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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?
 

theDBguy

I’m here to help
Local time
Today, 12:53
Joined
Oct 29, 2018
Messages
10,232
View attachment 80521 View 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).
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:53
Joined
Sep 21, 2011
Messages
5,782
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.?
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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.
 

foxxrunning

Member
Local time
Today, 12:53
Joined
Oct 6, 2019
Messages
93
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]
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:53
Joined
Sep 21, 2011
Messages
5,782
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 (Users: 0, Guests: 1)

Top Bottom