Solved

Problem with Type Mismatch error in Access 2007

Posted on 2014-09-08
5
586 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 125 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 125 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 (Access MVP)
Dale Fye (Access MVP) earned 125 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 37

Assisted Solution

by:PatHartman
PatHartman earned 125 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

729 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