run a function based on which form calls it

Bmelville1

Registered User.
Local time
Today, 07:10
Joined
Jun 18, 2012
Messages
10
Hello,
I have a db that tracks issues at work. and I have created an email function is there a way that I can call it from 2 different forms and then add If Then statements to change parts based on which form calls it? I tried setting a variable called NewOld to say if it was a new issue or an update issue. because they use different reports as the email body. I get an error that says "object required" and my Set NewOLD is highlighted.

Code:
Option Compare Database
Option Explicit
Dim NewOld As Integer
Dim TmpID As String



'This Function is for emailing newly created work orders out to the relevant individuals
Public Function FuncNewTechEmail()
    
     Set NewOld = 1
     TmpID = [Forms]![NewIssue]![ID]
   FuncEmailTech
   End If
   
End Function

'This Function is for emailing Updated work orders out to the relevant individuals
Public Function FuncOldTechEmail()
    'Dim TmpID As String
    'Dim NewOld As Integer
     TmpID = [Forms]![Issues2]![ID]
     'Set NewOld = 0
     
   FuncEmailTech
   
End Function

'This Function is for emailing Technicians
Public Function FuncEmailTech()

My FuncEmailTech works I then put if then statements like below in it

'Create Subject line of email based on old or new issue
If NewOld = 1 Then
StrSub = [Forms]![NewIssue]![Summary]
EmailSub = "An Issue Log has been opened: " & StrSub
Else
StrSub = [Forms]![Issues2]![Summary]
EmailSub = "An Issue Log has been Updated: " & StrSub
End If

I think I am not declaring the right type of variable but I don't know what to do should I use a boolean or string instead of an integer?
Thanks for your help
Brian
 
you would have to pass an argument to the function to identify the calling form, i think.

maybe a numeric code, but me.name should pass the form name as a string,
 
Thank you for the response but,
I'm having trouble using the me.name I get an erro that says invalid use of the me object.
here is my code
Code:
Public Function FuncNewTechEmail()
    Dim FrmName As String
    FrmName = Me.Name
    Debug.Print FrmName
    
     'Set NewOld = 1
     TmpID = [Forms]![NewIssue]![ID]
   FuncEmailTech
   End If
   
End Function

As for passing an argument to the function I do not know how to do this. I am trying to do as much of my coding in VBA and not macros as I am new to this any help or pointing in the direction for using functions with arguments would be greatly appreciated.
Thanks again,
Brian
 
Thank you for the response but,
I'm having trouble using the me.name I get an erro that says invalid use of the me object.
here is my code
Code:
Public Function FuncNewTechEmail()
    Dim FrmName As String
    FrmName = Me.Name
    Debug.Print FrmName
 
     'Set NewOld = 1
     TmpID = [Forms]![NewIssue]![ID]
   FuncEmailTech
   End If
 
End Function

As for passing an argument to the function I do not know how to do this. I am trying to do as much of my coding in VBA and not macros as I am new to this any help or pointing in the direction for using functions with arguments would be greatly appreciated.
Thanks again,
Brian

Brian,
declare the function with the argument as follows

Code:
Public Function FuncNewTechEmail(NewOld as Integer)
Dim FrmName As String
FrmName = Me.Name
Debug.Print FrmName
 
If NewOld = 1
   TmpID = [Forms]![NewIssue]![ID]
   FuncEmailTech
Else 
   ...... 'code based on NewOld other than 1  
 
End
 
 
End Function


To call this function from anywhere change the argument as you need, eg :

Code:
FuncNewTechEmail(1)    ....or 
FuncNewTechEmail(2)    ....or
FuncNewTechEmail(3)    ....etc

Hope this works for you. BTW, you should be using subs if you do not have functions returning a result.

Best,
Jiri
 
BTW, you should be using subs if you do not have functions returning a result.

Best,
Jiri
I don't. I pretty much use functions all the time except when using form and report events.
 

Users who are viewing this thread

Back
Top Bottom