Connecting excel with access

akar

New Member
Joined
Jul 6, 2011
Messages
7
Hi All,

I am new to VBA in excel and trying to achieve a funcitonality. Any help will be greatly appreciated.

I have an excel sheet (priceReport.xls) and an access table(Database7.accdb). The functionality that i want to achieve is to enter two dates from the excel sheet to the "Table3" in the access db by a click of a command button. Everytime the command button is clicked, the Table3 in access is erased and the two dates from excel are entered in Table3.

I am having problems connecting to the database as I am not sure of ADO and DAO functionality for access2007. Following is the code that i have used:

Private Sub CommandButton1_Click()
Dim ar As ADODB.Recordset
Dim ad As ADODB.Connection
Dim databaseName As String, SQL As String, AccessConnect As String
Dim fromdt As Date
Dim todt As Date
Set ad = New ADODB.Connection
SQL = "Select * From Table3"
AccessConnect = "Driver={Microsoft Access Driver (*.accdb)};" & _
"Dbq=Database7.accdb;" & _
"DefaultDir=C:\Project1\MatchPrice;" & _
"Uid=;Pwd=;"
databaseName = "C:\Project1\MatchPrice\Database7.accdb"
ad.Open AccessConnect
Set ar = ad.Execute(SQL)
fromdt = Sheets("Sheet1").Cells(2, 2)
todt = Sheets("Sheet1").Cells(2, 3)
If ar.EOF Then
ar.AddNew
Else
ar!DateFrom = Null
ar!DateTo = Null
ar.Update
End If
ar!DateFrom = fromdt
ar!DateTo = todt
ar.Update

End Sub


I am getting a runtime error '-2147467259 (80004005)'

if anyone can give me a sample code or any corrections in this one, that will be great.


Thanks,
-Akar.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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