LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default Add/imply fields when importing range into ACCESS

On Sun, 15 Feb 2009 05:35:00 -0800, MikeF
wrote:



Prefer your suggestion #3 as follows.
An example would be sincerely appreciated.

3) Don't link the tables via the Access UI. Create the tables in VBA using
ADO. This will give you maximum flexibility, but it's the most complex of
the options.


Here's the basics Mike. Set a reference to Microsoft ActiveX Data Objects.

Sub MakeAccessTable()

Dim sqlMake As String
Dim sqlDelete As String
Dim sqlInsert As String
Dim sCon As String
Dim rRow As Range
Dim rCell As Range
Dim rCity As Range
Dim rEvent As Range
Dim adCon As ADODB.Connection

Set rCity = Sheet1.Range("CityID")
Set rEvent = Sheet1.Range("EventID")
Set adCon = New ADODB.Connection

sCon = "DSN=MS Access Database;DBQ=C:\Documents and Settings\Dick\"
sCon = sCon & "My Documents\testimport.mdb;DefaultDir=C:\Documents "
sCon = sCon & "and Settings\Dick\My Documents;DriverId=25;FIL=MS "
sCon = sCon & "Access;MaxBufferSize=2048;PageTimeout=5;"

sqlMake = "CREATE TABLE tblMyRange (CityID Long, EventID Long, Field3 "
sqlMake = sqlMake & "Long, Field4 Long, Field5 Long, Field6 Long)"

sqlDelete = "DROP TABLE tblMyRange"

adCon.Open sCon
On Error Resume Next
adCon.Execute sqlDelete
On Error GoTo 0
adCon.Execute sqlMake

For Each rRow In Sheet2.Range("MyRange").Rows
sqlInsert = "INSERT INTO tblMyRange VALUES (" & _
rCity.Value & ", " & rEvent.Value & ", "

For Each rCell In rRow.Cells
sqlInsert = sqlInsert & rCell.Value & ", "
Next rCell

sqlInsert = Left$(sqlInsert, Len(sqlInsert) - 2) & ")"
adCon.Execute sqlInsert
Next rRow

adCon.Close

End Sub
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com
 
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
Importing Access DB into Excel changes my calculated fields in Exc Shadow27_us Excel Worksheet Functions 3 January 7th 10 10:10 PM
Append key fields to range for ACCESS import MikeF[_2_] Excel Programming 0 February 11th 09 05:30 PM
Append key fields to range for ACCESS import MikeF[_2_] Excel Programming 0 February 11th 09 05:28 PM
Append key fields to range for ACCESS import MikeF[_2_] Excel Programming 0 February 11th 09 03:52 PM
Importing data from Access into Excel: prob w/ cutting off fields Nicole L. Excel Worksheet Functions 1 February 7th 05 10:05 PM


All times are GMT +1. The time now is 09:56 AM.

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"