Solved

ms access dateadd runtime error mismatch

Posted on 2015-02-17
13
233 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 50

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 50

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 38

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 50

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 50

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 50

Accepted Solution

by:
Gustav Brock earned 500 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 50

Expert Comment

by:Gustav Brock
ID: 40616369
You are welcome!

/gustav
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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