Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-12-08
4
Medium Priority
?
292 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
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

810 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