Solved

ms access dateadd runtime error mismatch

Posted on 2015-02-17
13
206 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
13 Comments
 
LVL 49

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 49

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
 
LVL 34

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 49

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 49

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 49

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 49

Expert Comment

by:Gustav Brock
ID: 40616369
You are welcome!

/gustav
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now