Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Spreadsheet to access

Urkec,
You are great, it works fine.
Thank you very much!!

"urkec" wrote:

"boris" wrote:


Please,help
When i run the code below i get message:
"the connection can not be used to perform this operation. it is either
closed or invalid in this context



Private Sub ADOFromExcelToAccess()

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long

Set cn = New ADODB.Connection
'Replace with the path to your database
cn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Documents and
Settings\Boris\My Documents\test/" _
& "Data.mdb;Persist Security Info=False"

Set rs = New ADODB.Recordset
rs.Open "test", cn, adOpenKeyset, adLockOptimistic, adCmdTable

r = 2 ' the starting row in the worksheet

Do While Len(Range("A" & r).Formula) 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("C" & r).Value
.Fields("FieldName3") = Range("D" & r).Value
.Fields("FieldName4") = Range("F" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub


Hi Boris,

I think you need to use

cn.Open

before

Set rs = New ADODB.Recordset
rs.Open "test", cn, adOpenKeyset, adLockOptimistic, adCmdTable

Hope that helps.

--
urkec

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
limited access spreadsheet BNT1 via OfficeKB.com Excel Discussion (Misc queries) 3 March 31st 09 05:01 PM
Import Spreadsheet into Access Jon Excel Worksheet Functions 1 May 30th 08 02:33 PM
Remote Access to a Spreadsheet [email protected] Excel Discussion (Misc queries) 5 October 2nd 07 07:41 PM
need help with using VBA to access a second spreadsheet [email protected] Excel Discussion (Misc queries) 2 September 13th 07 12:47 PM
cannot access excel spreadsheet Renn Excel Discussion (Misc queries) 2 October 5th 05 12:23 PM


All times are GMT +1. The time now is 01:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"