Edit Access Table Entry from Excel VBA

kryptonian

Board Regular
Joined
Oct 6, 2006
Messages
104
I'm doing an attendance database with Excel as the entry form and Access as the database. My fields are:

| SAP_ID | LAST_NAME | GIVEN_NAME | TIME_IN | TIME_OUT |

Entering a new record is no problem if TIME_IN is still blank. But if TIME_IN is not blank, I want my code to edit that particular entry to add into TIME_OUT using the SAP_ID and TIME_IN to specify what record to edit. My code below.

*******************************************************
Sub cmdEdit()

Dim cn As ADODB.Connection
Dim cm As Command

Set cn = New ADODB.Connection
Set cm = New ADODB.Command

'On Error GoTo errHandler
cn.ConnectionString = "Provider = Microsoft.Jet.OleDb.4.0;" _
& "Data Source=C:\Users\v-edcepe\Documents\VBA Training Files\Attendance.mdb"
'sets the path for the connection

cn.Open 'opens the connection

Sheet2.Cells(4, 16) = "=now()"

cm.CommandText = "Update tblLog Set TIME_OUT = '" & Sheet2.Cells(4, 16) & "' where SAP_ID = '" & Sheet2.Cells(25, 3) & "' And TIME_IN='" & Sheet2.Cells(25, 6) & "'"

Set cm.ActiveConnection = cn
cm.Execute
cn.Close

'errHandler:
' MsgBox ("Data not found")
End Sub

*******************************************************

I purposely exluded errHandler for now so I know if my code is wrong.

I am getting a type mismatch error on the cm.CommandText line or cm.Execute if I edit it a bit.

Fields are setup properly in Access.

I'm using Windows 7 running Office 2007.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Sheet2.Cells(4, 16) = "=now()"
This drew my attention. It will put a number in the region of 40,650 into the worksheet cell, that being the current Excel date/time serial. Does your Access database contain a numeric value in a text field as your SQL suggests (as you wrapped the value for TIME_OUT in single quotes)? Is TIME_OUT a Date type and is it expected to contain a date and time, or just a time?

Insert Debug.Print cm.CommandText immediately after you set up the SQL string and check that it contains exactly what you're expecting it to.
 
Upvote 0
I have the "=now()" in excel so that this would be the value copied into the Access table for TIME_IN or TIME_OUT. Both are set as Date/Time in Access.

When you say "wrapped the value for TIME_OUT in single quotes", are you saying that I shouldn't do this if I want a Date/Time in that field? How would my CommandText line be like?
 
Last edited:
Upvote 0
I have the "=now()" in excel so that this would be the value copied into the Access table for TIME_IN or TIME_OUT. Both are set as Date/Time in Access.

now() generates an Excel date/time serial which is a number like 40651.652778 (3.40pm on 18th April 2011) not a formatted date/time string which SQL expects.

Please try this: insert Debug.Print cm.CommandText immediately after you set up the SQL string and take a look at exactly what's actually in there.

I suspect you're generating something like Set TIME_OUT = '40651.652778' whereas it should be like Set TIME_OUT = #4/18/2011 15:40#.

You need to make sure that when you pass a date/time from Excel to SQL, you're converting it from a serial number to a formatted string, and vice versa.

When you say "wrapped the value for TIME_OUT in single quotes", are you saying that I shouldn't do this if I want a Date/Time in that field? How would my CommandText line be like?

You'd wrap a SQL date/time in # characters.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top