[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ms access dateadd runtime error mismatch

Posted on 2015-02-17
13
Medium Priority
?
249 Views
Last Modified: 2016-02-10
I have a form in MS Access with the following fields :

- StartDateG      (Data Type : Date/Time)
- EndDateG      (Data Type : Date/Time)
- NumberOFYears      (Data Type : Number - Long Integer)

I tried the below in VBA but always return (Run-time error "13" : Type mismatch)

EndDateG = DateAdd("yyyy", [NumberOfYears], [StartDateG])  ,
EndDateG.value  = DateAdd("yyyy", [NumberOfYears], [StartDateG])
Me.EndDateG.value  = DateAdd("yyyy", [NumberOfYears], [StartDateG])

However, when I used it in query it give the results without error.

Any help please ?

Thanks
0
Comment
Question by:TAB-000
[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
13 Comments
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40614157
This typically happens if Null values are met. Try with:

Me!EndDateG.Value  = IIf(IsNull([NumberOfYears]+[StartDateG]), Null, DateAdd("yyyy", Nz([NumberOfYears], 0), Nz([StartDateG], Date())))

/gustav
0
 

Author Comment

by:TAB-000
ID: 40614199
Dear Gustav,

Still producing the same error.

TAB
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40614234
Then it is something else.

Try inserting this code line to debug:

Debug.Print "EDG:", Me!EndDateG.Value, "NOY:", [NumberOfYears], "SDG:", [StartDateG]
Me!EndDateG.Value  = DateAdd("yyyy", [NumberOfYears], [StartDateG])

or remove the DateAdd code line.

/gustav
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:PatHartman
ID: 40615324
Me.EndDateG = DateAdd("yyyy", Me.[NumberOfYears], Me.[StartDateG])

Using the Me. qualifier will give you intellisense so you can see what the actual values are.   Or you can print them as Gus suggests.

The IIf() works differently in code than it does in SQL.  In SQL, the evaluation stops as soon as a condition is met.  So, you can test a field for 0 to avoid a divide by 0 error.  This won't work in VBA since all parts of the expression are evaluated even the parts in the false path.  That was a long winded explanation why you shouldn't use the IIf() in code.  Use the If-Then-Else or a Case statement.

If IsDate(Me.StartDateG) AND IsNumeric(Me.NumberOfYears) Then
    Me.EndDateG = DateAdd("yyyy", Me.[NumberOfYears], Me.[StartDateG])    
Else
    Msgbox "error message .....",vbOKOnly
    Exit Sub
End if
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40615373
Or just test for Null as the two fields have the correct data type. Then use Nz for the second part as shown.

/gustav
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40615463
In addition to the above comments from Custav and Pat, which are both valid, just add "default values" to the fields in question.  In this case, you won't have to worry about Nulls at all.

Edit the table (Design view) that has the fields and set the "Default Value" property:

  StartDateG - set default value to Date() or Now()
  EndDateG   - set default value to Date() or Now()
  NumberOFYears  - set defautl value to 0
0
 

Author Comment

by:TAB-000
ID: 40616053
Dear All,

With all solution mentioned above, still producing the same error. It seems that VBA don't like DateAdd.
Reference to Gustav, her is the results of debug print :
EDG:          Null          NOY:           1            SDG:          01/01/2015

Furthermore, I have tried the following code and it accept the value , as below debug
EDG:          01/01/2016    NOY:           1            SDG:          01/01/2015


Also I have deleted the fields in questions (i.e. StartDateG, EndDateG and NumberOfYears) and create another field with different name, still the same issue.

In addition, I have started all over again and created another .accdb  from scratch and the DateAdd is not accepted.

I did and do appreciate your usual cooperation to solve this issue.

Best regards.
0
 

Author Comment

by:TAB-000
ID: 40616057
--Addition Line Add Below--

Dear All,

With all solution mentioned above, still producing the same error. It seems that VBA don't like DateAdd.
Reference to Gustav, her is the results of debug print :
EDG:          Null          NOY:           1            SDG:          01/01/2015

Furthermore, I have tried this code (Me.EndDGreg.Value = #1/1/2016#) and it accept the value , debug as follows:
EDG:          01/01/2016    NOY:           1            SDG:          01/01/2015


Also I have deleted the fields in questions (i.e. StartDateG, EndDateG and NumberOfYears) and create another field with different name, still the same issue.

In addition, I have started all over again and created another .accdb  from scratch and the DateAdd is not accepted.

I did and do appreciate your usual cooperation to solve this issue.

Best regards.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40616168
Something else must be going on. Could you provide your full code for the function with this line - or upload your sample database?

/gustav
0
 

Author Comment

by:TAB-000
ID: 40616292
Dear Gustav,

Attached is sample file which illustrates the problem.

Thanks
Rental.accdb
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 40616328
That "something else" is that you have a field and a control (textbox) named DateAdd which gets first priority.

So, either rename the control or prefix DateAdd with VBA to make it unambiguous:

Me!EndDGreg.Value = VBA.DateAdd("yyyy", Me!NumberOfYears.Value, Me!StartDGreg.Value)

/gustav
0
 

Author Comment

by:TAB-000
ID: 40616368
Brilliant Gustav.

I thank you very much for the help and enlighten.

Best regards
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40616369
You are welcome!

/gustav
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

649 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