Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Null parameter in SUB
Sami Shamma 
             
3 years ago
Hello friends

I have the following sub:

DetailsPrivate Sub PrintRoster( _
    rsDayOfWeekID As Integer, _
    rsAllBlocks As Boolean, _
    rsSpanish As Boolean, _
    rsReligionCode As String, _
    rsUseservice As Boolean, _
    rsServiceName As String, _
    rsServiceMessage As String, _
    rsTimeSlot As Date, _
    rsLocation As String, _
    Optional rsServiceNote As String, _
    Optional rsSelectedBlocks As String, _
    Optional rsAreYouSure As Boolean = False)

If I call it with rsServiceNote as Null, I get run-time error '94' Invalid use of Null

This is the code that generates the error:

    PrintRoster _
        DayOfWeekID, _
        AllBlocks, _
        Spanish, _
        ReligionCode, _
        Useservice, _
        ServiceName, _
        ServiceMessage, _
        TimeSlot, _
        Location, _
        ServiceNote, _
        SelectedBlocks, _
        True

The code works fine when the ServiceNote has value in it.

Help! Please
Kevin Robertson  @Reply  
          
3 years ago
For your optional parameters state the value to return if the parameter is omitted.
For example:

Optional rsServiceNote As String = ""
Sami Shamma OP  @Reply  
             
3 years ago
Hi Kevin

I did what you said :

DetailsPrivate Sub PrintRoster( _
    rsDayOfWeekID As Integer, _
    rsAllBlocks As Boolean, _
    rsSpanish As Boolean, _
    rsReligionCode As String, _
    rsUseservice As Boolean, _
    rsServiceName As String, _
    rsServiceMessage As String, _
    rsTimeSlot As Date, _
    rsLocation As String, _
    Optional rsServiceNote As String = "", _
    Optional rsSelectedBlocks As String = "", _
    Optional rsAreYouSure As Boolean = False)


But I am still getting the same error.
I tried to deal with it before calling the sub but it does not seem to set the value:

Private Sub SelectBtn_Click()
    If ServiceNote = Null Then
        ServiceNote = " "
    End If
    MsgBox "-" & ServiceNote & "-"
    
    If IsNull(SelectedBlocks) Then
        SelectedBlocks = " "
    End If
    PrintRoster ..........
Alex Hedley  @Reply  
           
3 years ago
rs usually means recordset, what is it denoting here?
Sami Shamma OP  @Reply  
             
3 years ago
Hi Alex

I call "PrintRoster" from 2 places, see code below, one of them is a recordset Loop to print all the reports.
You are correct the "rs" is not appropriate in the parameters. I will re name as soon as I get this to work.

many thanks

DetailsPublic Sub PrintALLBtn_Click()

    Me.Requery
    If MsgBox("you are about to print ALL the selected rosters. Are you sure?", _
      vbYesNoCancel + vbExclamation, "Warning") <> vbYes Then
    Exit Sub
    End If
    
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM ServiceQ WHERE " & Filter)
    While Not rs.EOF
        
        If rs!PrintAll Then
'MsgBox "Print = " & rs!PrintAll & " -- " & rs!ServiceMessage
            PrintRoster rs!DayOfWeekID, _
            rs!AllBlocks, _
            rs!Spanish, _
            rs!ReligionCode, _
            rs!Useservice, _
            rs!ServiceName, _
            rs!ServiceMessage, _
            rs!TimeSlot, _
            rs!Location, _
            rs!ServiceNote, _
            rs!SelectedBlocks, _
            False
            rs.MoveNext
        Else
            rs.MoveNext
        End If
    Wend
    rs.Close
    Set rs = Nothing

End Sub

Private Sub SelectBtn_Click()
    If ServiceNote = Null Then
        ServiceNote = " "
    End If
    MsgBox "-" & ServiceNote & "-"
    
    If IsNull(SelectedBlocks) Then
        SelectedBlocks = " "
    End If
    PrintRoster _
        DayOfWeekID, _
        AllBlocks, _
        Spanish, _
        ReligionCode, _
        Useservice, _
        ServiceName, _
        ServiceMessage, _
        TimeSlot, _
        Location, _
        ServiceNote, _
        SelectedBlocks, _
        True
    
End Sub
Kevin Yip  @Reply  
     
3 years ago
A string variable cannot be null, and that's why you get the "cannot be null" error.  To declare an optional parameter that can be null and defaults to null, use the Variant data type, and write:

     Optional v As Variant = Null
Sami Shamma OP  @Reply  
             
3 years ago
Thank you Kevin, that worked.

Many thanks

Tell Richard to add you to the tip jar.
Kevin Yip  @Reply  
     
3 years ago
Thanks.  My efforts here are completely voluntary, so no need to tip.  I'm retired, and used Access in my old job; and I just want to give back to the Access community in whatever minor ways.  I post only here on this site.

Other moderators actually know all of Richard"s courses and post a lot more often than I do.  So they deserve the tips more than I do.
Sami Shamma OP  @Reply  
             
3 years ago
Thanks again Kevin
You have helped me a lot in the past.

I have been away from writing code for over 30 years. I just started learning VBA a few months ago. Richard courses is the reason I can write these applications for my work.

you guys came to my rescue many times.


This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 12:35:26 PM. PLT: 0s