Solved

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

Posted on 2013-12-08
4
285 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 500 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 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