Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default DAO Excel to Access

I'm trying to store 3 values held in range (B4:B6) in an Acces table using DAO.
Here is the code in Excel :-

Private Sub CommandButton1_Click()

Dim ma(6, 1) As Long
Dim c As Long
Dim fn As String
Dim db As Database
Dim rs As Recordset

ma(1, 1) = Cells(4, 2).Value
ma(2, 1) = Cells(5, 2).Value
ma(3, 1) = Cells(6, 2).Value

Set db = OpenDatabase("F:\AJR Dev\ExceltoAccess.mdb")
' open the database
Set rs = db.OpenRecordset("Table1", dbOpenTable)
c = 1
With rs
Do While c < 4

.AddNew ' create a new record
' add values to each field in the record
fn = "Field" & c
.Fields(fn) = ma(c, 1)

c = c + 1 ' next value

Loop

.Update ' store the new record

End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

It works fine BUT only stores Field3 value (B6) each time - any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default DAO Excel to Access

Shouldn't adding a new record be above your loop:

Private Sub CommandButton1_Click()

Dim ma(6, 1) As Long
Dim c As Long
Dim fn As String
Dim db As Database
Dim rs As Recordset

ma(1, 1) = Cells(4, 2).Value
ma(2, 1) = Cells(5, 2).Value
ma(3, 1) = Cells(6, 2).Value

Set db = OpenDatabase("F:\AJR Dev\ExceltoAccess.mdb")
' open the database
Set rs = db.OpenRecordset("Table1", dbOpenTable)
c = 1
With rs
.AddNew ' create a new record
Do While c < 4

' add values to each field in the record
fn = "Field" & c
.Fields(fn) = ma(c, 1)

c = c + 1 ' next value

Loop

.Update ' store the new record

End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

--
Regards,
Tom Ogilvy

"Andy" wrote in message
...
I'm trying to store 3 values held in range (B4:B6) in an Acces table using

DAO.
Here is the code in Excel :-

Private Sub CommandButton1_Click()

Dim ma(6, 1) As Long
Dim c As Long
Dim fn As String
Dim db As Database
Dim rs As Recordset

ma(1, 1) = Cells(4, 2).Value
ma(2, 1) = Cells(5, 2).Value
ma(3, 1) = Cells(6, 2).Value

Set db = OpenDatabase("F:\AJR Dev\ExceltoAccess.mdb")
' open the database
Set rs = db.OpenRecordset("Table1", dbOpenTable)
c = 1
With rs
Do While c < 4

.AddNew ' create a new record
' add values to each field in the record
fn = "Field" & c
.Fields(fn) = ma(c, 1)

c = c + 1 ' next value

Loop

.Update ' store the new record

End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

It works fine BUT only stores Field3 value (B6) each time - any ideas?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default DAO Excel to Access

Thanks again Tom - just didn't spot that - works fine now - Andy.

"Tom Ogilvy" wrote:

Shouldn't adding a new record be above your loop:

Private Sub CommandButton1_Click()

Dim ma(6, 1) As Long
Dim c As Long
Dim fn As String
Dim db As Database
Dim rs As Recordset

ma(1, 1) = Cells(4, 2).Value
ma(2, 1) = Cells(5, 2).Value
ma(3, 1) = Cells(6, 2).Value

Set db = OpenDatabase("F:\AJR Dev\ExceltoAccess.mdb")
' open the database
Set rs = db.OpenRecordset("Table1", dbOpenTable)
c = 1
With rs
.AddNew ' create a new record
Do While c < 4

' add values to each field in the record
fn = "Field" & c
.Fields(fn) = ma(c, 1)

c = c + 1 ' next value

Loop

.Update ' store the new record

End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

--
Regards,
Tom Ogilvy

"Andy" wrote in message
...
I'm trying to store 3 values held in range (B4:B6) in an Acces table using

DAO.
Here is the code in Excel :-

Private Sub CommandButton1_Click()

Dim ma(6, 1) As Long
Dim c As Long
Dim fn As String
Dim db As Database
Dim rs As Recordset

ma(1, 1) = Cells(4, 2).Value
ma(2, 1) = Cells(5, 2).Value
ma(3, 1) = Cells(6, 2).Value

Set db = OpenDatabase("F:\AJR Dev\ExceltoAccess.mdb")
' open the database
Set rs = db.OpenRecordset("Table1", dbOpenTable)
c = 1
With rs
Do While c < 4

.AddNew ' create a new record
' add values to each field in the record
fn = "Field" & c
.Fields(fn) = ma(c, 1)

c = c + 1 ' next value

Loop

.Update ' store the new record

End With

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

It works fine BUT only stores Field3 value (B6) each time - any ideas?




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
Query from Access into Excel cause Access to go to read only T Stephens Excel Discussion (Misc queries) 0 March 24th 09 04:17 PM
Can Excel access data from Access?! Al Excel Discussion (Misc queries) 5 April 5th 08 03:52 PM
export access to excel. change access & update excel at same time fastcar Excel Discussion (Misc queries) 0 June 24th 05 09:27 PM
access--excel--access Tim Excel Programming 2 February 6th 04 11:41 AM
How to access ACCESS from Excel! Gordon Cartwright[_2_] Excel Programming 3 November 8th 03 11:50 AM


All times are GMT +1. The time now is 11:35 AM.

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

About Us

"It's about Microsoft Excel"