Passing Arguements to a function

L

Lisa B.

Is there a way to only pass two(2) arguements to a function if the function
is waiting for five(5)

1. For example I have a function that adds a record to a table.
2. On a form the user inputs First Name, Last Name, type, Title, and Level
3. First Name and Last Name are required fields
4. I don't want Type, Title. or Level to be required (they should be
optional inputs)
5. the user presses a button that calls the following function

Public Function NewRecruit(first, last, Otype, Title, Level As
String)

6. the code behind the button looks like this

NewRecruit Me![txtFName], Me![txtLName], Me![txtType],
Me![txtTitle], Me![txtLevel]

7. an error is generated if the user only inputs the first and last name
**Invalid use of Null***

8. the code for the function is as follows

Set TheDB = CurrentDb
Set SourceRS = TheDB.OpenRecordset("tblRecruitment", dbOpenDynaset)


With SourceRS
.AddNew
!CommunityID = GLBL_Community
!OfficialType = Otype
!OfficialLevel = Level
!Title = Title
!FName = first
!LName = last
.Update
.Bookmark = .LastModified
End With
RecruitID = SourceRS![OfficialID]


SourceRS.Close
 
T

Tim Ferguson

Is there a way to only pass two(2) arguements to a function if the
function is waiting for five(5)

Look up help for the Optional keyword.

Look up help for ParamArray.

Look up help for Variant Arrays.

HTH


Tim F
 
F

Francesc Hervada-Sala

Lisa said:
5. the user presses a button that calls the following function

Public Function NewRecruit(first, last, Otype, Title, Level As String)
[...]
7. an error is generated if the user only inputs the first and last name
**Invalid use of Null***

You get the error because Level is expected to be a string, but a Null
value is not. I think you should define parameters as variant instead of
string:

Public Function NewRecruit(first As Variant, last As Variant, Otype As
Variant, Title As Variant, Level As Variant)

Hope it works!


Francesc
 
G

Garret

Not positive, but I think that the AddNew is failing because there a null
values in the 3 of 5 fields that are left blank. There is a thing called the
Nz( ) function that will replace Null values with zero length strings and
let the AddNew proceed. This will only work if you table is setup to allow
zero-length strings in the fields. I think this would work.

With SourceRS
.AddNew
!CommunityID = Nz(GLBL_Community,"")
!OfficialType = Nz(Otype,"")
!OfficialLevel = Nz(Level,"")
!Title = Nz(Title,"")
!FName = Nz(first,"")
!LName = Nz(last,"")


Lisa B. said:
Is there a way to only pass two(2) arguements to a function if the function
is waiting for five(5)

1. For example I have a function that adds a record to a table.
2. On a form the user inputs First Name, Last Name, type, Title, and Level
3. First Name and Last Name are required fields
4. I don't want Type, Title. or Level to be required (they should be
optional inputs)
5. the user presses a button that calls the following function

Public Function NewRecruit(first, last, Otype, Title, Level As
String)

6. the code behind the button looks like this

NewRecruit Me![txtFName], Me![txtLName], Me![txtType],
Me![txtTitle], Me![txtLevel]

7. an error is generated if the user only inputs the first and last name
**Invalid use of Null***

8. the code for the function is as follows

Set TheDB = CurrentDb
Set SourceRS = TheDB.OpenRecordset("tblRecruitment", dbOpenDynaset)


With SourceRS
.AddNew
!CommunityID = GLBL_Community
!OfficialType = Otype
!OfficialLevel = Level
!Title = Title
!FName = first
!LName = last
.Update
.Bookmark = .LastModified
End With
RecruitID = SourceRS![OfficialID]


SourceRS.Close
 

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