Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Error when exporting data into Access

Hello,

i'd like to export Excel data into an existing Access database. But when a
field is empty in Excel, i get an error. I tried to put NOT NULL fields in
the database but that doesn't change.
Do you have any ideas?

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim DBPath As String

DBPath = "J:\bdd.mdb"

' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & DBPath & ";"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "INSTRUMENT", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 5 ' the start row in the worksheet
Do While Len(Range("B" & 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("Instrument_id") = Range("B" & r).Value
.Fields("Name") = Range("C" & r).Value
.Fields("ShortName") = Range("D" & r).Value
.Fields("MasterInstrument_id") = Range("E" & r).Value
.Fields("Instrument_Type") = Range("F" & r).Value
.Fields("Share_Classe") = Range("G" & r).Value
.Fields("Series") = Range("H" & r).Value
.Fields("Currency_id") = Range("I" & r).Value
.Fields("Flag") = Range("J" & r).Value
.Fields("Management_fee") = Range("K" & r).Value
.Fields("Administration_fee") = Range("L" & r).Value
.Fields("Otherfee_x") = Range("M" & r).Value
.Fields("Performance_fee") = Range("N" & r).Value
.Fields("Hurdle_Rate") = Range("O" & r).Value
.Fields("High_Watermark") = Range("P" & r).Value
.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


Thanks !
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Error when exporting data into Access

The trick is if the cell is empty then you don't want to send anything...
Here is how I would modify your first field. The same applies for the rest of
the fields...

if Range("B" & r).Value < empty then .Fields("Instrument_id") = Range("B" &
r).Value

HTH
"Laurent M" wrote:

Hello,

i'd like to export Excel data into an existing Access database. But when a
field is empty in Excel, i get an error. I tried to put NOT NULL fields in
the database but that doesn't change.
Do you have any ideas?

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim DBPath As String

DBPath = "J:\bdd.mdb"

' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & DBPath & ";"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "INSTRUMENT", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 5 ' the start row in the worksheet
Do While Len(Range("B" & 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("Instrument_id") = Range("B" & r).Value
.Fields("Name") = Range("C" & r).Value
.Fields("ShortName") = Range("D" & r).Value
.Fields("MasterInstrument_id") = Range("E" & r).Value
.Fields("Instrument_Type") = Range("F" & r).Value
.Fields("Share_Classe") = Range("G" & r).Value
.Fields("Series") = Range("H" & r).Value
.Fields("Currency_id") = Range("I" & r).Value
.Fields("Flag") = Range("J" & r).Value
.Fields("Management_fee") = Range("K" & r).Value
.Fields("Administration_fee") = Range("L" & r).Value
.Fields("Otherfee_x") = Range("M" & r).Value
.Fields("Performance_fee") = Range("N" & r).Value
.Fields("Hurdle_Rate") = Range("O" & r).Value
.Fields("High_Watermark") = Range("P" & r).Value
.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


Thanks !

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Error when exporting data into Access

ok that's cool

but i have another problem now with this program

how can i update data from my database?
The function Update used with the recordset insert rows into the database,
and i would like to update it. What can i do?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Error when exporting data into Access


Laurent M wrote:
but i have another problem now with this program

how can i update data from my database?


Presumably something very similar, except instead of the .AddNew line:
..Filter = "Instrument_id = " & Range("B" & r).Value

Jamie.

--

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
exporting data from access to excel [email protected] Excel Discussion (Misc queries) 1 April 11th 06 10:17 AM
Exporting data from access to excel vinayak Excel Worksheet Functions 0 April 11th 06 08:20 AM
Exporting Data from Excel to Access Problems Mike[_85_] Excel Programming 1 June 3rd 04 05:01 PM
Exporting Data from Access D.Mulder Excel Programming 1 March 5th 04 08:46 AM
Exporting data to access Carl Meade Excel Programming 3 November 18th 03 04:09 PM


All times are GMT +1. The time now is 09:50 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"