?
Solved

ms access dateadd runtime error mismatch

Posted on 2015-02-17
13
Medium Priority
?
258 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

850 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