?
Solved

Calculating a date within an IF clause; Experiencing Invlaid Use of Null error

Posted on 2013-12-08
4
Medium Priority
?
289 Views
Last Modified: 2013-12-08
I have the following VBA code used within a form to update a table.

After I select the type of record, "Advancement" or "Reappointment" in a list box named "lstEval" I have to enter the reporting period for the evaluation in an Unbound Text Box - "txtRptPD".

After the date is selected the two unbound txt boxes update with the month beginning "txtRptBeg" and month ending "txtEnd" of the evaluation period.

I am getting an error:

run Time Error # 94

Invalid use of null

I am guessing that the value of the "lstEval" box is  being evaluated as a null.



Below is the code for the lstEval after update and the txtRptPd after change:

Private Sub lstEval_AfterUpdate()
 Dim vType As String
     vType = Me.LstEval.Column(0)
 End Sub

Private Sub txtRptPD_Change()


 If vType = "Advancement" Then
     Me.txtRptBeg = DateSerial(Year(DateAdd("m", -7, Me.txtRptPD)), Month(DateAdd("m", -7, Me.txtRptPD)), 1)
     Me.txtEnd = DateAdd("d", -1, DateSerial(Year(Me.txtRptPD), Month(Me.txtRptPD), 1))
 Else
     Me.txtRptBeg = DateSerial(Year(DateAdd("m", -25, Me.txtRptPD)), Month(DateAdd("m", -25, Me.txtRptPD)), 1)
     Me.txtEnd = DateAdd("d", -1, DateSerial(Year(Me.txtRptPD), Month(Me.txtRptPD), 1))

End If

End Sub

Thanks for your help

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 39704928
What is the rowsource query for lstEval?

A quick way to determine what is causing the error is to use a messagebox (see the inline notes):

Private Sub lstEval_AfterUpdate()
 Dim vType As String
     msgBox vType &  ""  '<--- Does this display a value?
     vType = Me.LstEval.Column(0) 
 End Sub

Open in new window


You can *handle* nulls to avoid that error like this...

Private Sub lstEval_AfterUpdate()
 Dim vType As String
     vType = trim(Me.LstEval.Column(0) & "")
 End Sub

Open in new window


Private Sub txtRptPD_Change()

 dim dtStart as date
dim dtEnd as Date
dtStart = NZ(Me.txtRptPD, #1/1/1900#)
dtEnd = NZ(Me.txtRptPD, #1/1/2100#)
 If vType = "Advancement" Then
     Me.txtRptBeg = DateSerial(Year(DateAdd("m", -7, dtStart)), Month(DateAdd("m", -7, dtStart)), 1)
     Me.txtEnd = DateAdd("d", -1, DateSerial(Year(dtEnd), Month(dtEnd), 1))
 Else
     Me.txtRptBeg = DateSerial(Year(DateAdd("m", -25, dtStart)), Month(DateAdd("m", -25, dtStart)), 1)
     Me.txtEnd = DateAdd("d", -1, DateSerial(Year(dtEnd), Month(dtEnd), 1))

End If
End Sub

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 39704932
I will try this and get back with you.

Thanks

Glen
0
 

Author Closing Comment

by:GPSPOW
ID: 39704972
Your solution solved the null error.

I need to tweak the code to calculate the txtBegPD and txtEnd values.  

Thanks

Glen
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39704981
Glad to help :)
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question