Solved

ms access dateadd runtime error mismatch

Posted on 2015-02-17
13
214 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 35

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
 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 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