Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with Type Mismatch error in Access 2007

Posted on 2014-09-08
5
Medium Priority
?
592 Views
Last Modified: 2014-09-09
I have an unbound form that adds a record to a table. One of the fields is a date/time type and is not required. Whenever I fill out the form and hit add record I get a type mismatch error. The field I am having a problem with is separation_date. Below is the code to add the record:

' Open a connection.
    Set cnn1 = New ADODB.Connection
   mydb = "c:\barnett\barnett.accdb"
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & mydb
    cnn1.Open strCnn

' Open employee table.
    Set rstcontact = New ADODB.Recordset
    rstcontact.CursorType = adOpenKeyset
    rstcontact.LockType = adLockOptimistic
    rstcontact.Open "employee", cnn1, , , adCmdTable

'get the new record data
rstcontact.AddNew
        rstcontact!empid = empid
        rstcontact!fname = fname
        rstcontact!lname = lname
        rstcontact!hiredate = hiredate
        rstcontact!i9 = i9
        rstcontact!dl_id = dl_id
        rstcontact!birth_certificate = birth_certificate
        rstcontact!sscard = sscard
        rstcontact!currently_working = currently_working
        rstcontact!temp_agency = temp_agency
        rstcontact!separation_date = separation_date
        rstcontact!separation_reason = separation_reason
        rstcontact!notes = notes
        rstcontact!eligible_for_rehire = eligible_for_rehire
        rstcontact!eaddress = eaddress
        rstcontact!ecity = ecity
        rstcontact!estate = estate
        rstcontact!ezip = ezip
        rstcontact!ephone = ephone
        rstcontact!ecell = ecell
        rstcontact!emergency_name = emergency_name
        rstcontact!emergency_number = emergency_number
        rstcontact!payrate = payrate
        rstcontact!employee_status = employee_status
        rstcontact.Update

' Show the newly added data.
        'MsgBox "New contact: " & rstcontact!Name & " has been successfully added"

'close connections
rstcontact.close

Open in new window


Thanks in advance for your help.
0
Comment
Question by:princeservice
[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
5 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40310594
replace in your code rstcontact!separation_date = separation_date with this:
if Not IsNull(seperation_date) then rstcontact!separation_date = separation_date

Open in new window

0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 500 total points
ID: 40310636
try this, using the Nz() function

rstcontact!separation_date = nz(separation_date,null)
0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 500 total points
ID: 40310674
Where does the value for the variable 'separation_date' come from?

How is that variable (I assume it is a variable) declared.  If it is not a variable, but is actually a control, I would strongly recommend you use some form of naming convention to name your controls so you can distinguish them from variables.

I would generally go with a technique similar to Randy's but would also test for blanks, something like:
If Trim(separation_date & " ") <> "" then 
    rstcontact!separation_date = separation_date
End If

Open in new window

0
 
LVL 39

Assisted Solution

by:PatHartman
PatHartman earned 500 total points
ID: 40310879
When you use an unbound form, Access will not do any automatic validation of data types unless you give it a clue what is in the field.  Set the format of the date control to be a date and Access will only allow null or a valid date.

Checking for not null is not sufficient for a date field from an unbound control.  If you don't do what I suggested above and even if you do and want to validate further, then use the IsDate() function.

If IsDate(separation_date)  then 
    rstcontact!separation_date = separation_date
End If

Open in new window

0
 

Author Comment

by:princeservice
ID: 40312142
I figured out that I didn't need that field in the "add employee" form in the first place. The field works good in the "edit employee" form and that is a bound form. I will use all of the information in this question for any possible future problems. Thanks everyone for the lesson.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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