Transferring Excel Columns to Access Fields

mahmud1180

New Member
Joined
Oct 26, 2015
Messages
17
Hi there!! I want to run a VBA code that makes a column in the excel file and then transfer it to Access with a certain Field Name. Then this excel Column is replaced by another set of Values and I want to transfer it to Access with another Field Name.I have a recurring procedure so, I can not use the AccessImport Method. I can add values cell by cell but I want to avoid that as it is gonna take more time. Can anyone help? Thanks a lot!!
 
Are you inserting the fields separately? That's the only difference I can think of from how I ran the code (which aligned fine):


Excel 2010
IJKL
3NorthSouthEastWest
41234
52468
636912
7481216
85101520
96121824
107142128
118162432
129182736
1310203040
1411223344
1512243648
1613263952
1714284256
1815304560
1916324864
2017345168
2118365472
Sheet1


Code:
Public Sub DoTrans()


Set cn = CreateObject("ADODB.Connection")
dbPath = Application.ActiveWorkbook.Path & "\Test2.mdb"
dbWb = Application.ActiveWorkbook.FullName
dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
dsh = "[" & Application.ActiveSheet.Name & "$]"
cn.Open scn

ssql = "INSERT INTO Data ([North], [South], [East], [West]) "
ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh


cn.Execute ssql




End Sub

q2a7XZl.png



(running the code again inserts another copy of the data directly below without any distortion)
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Are you inserting the fields separately? That's the only difference I can think of from how I ran the code (which aligned fine):

Yes! I am Inserting the Fields separately. Actually, I am creating a new Field name every time before inserting values into the Field.
Code:
Public Sub DoTrans()

Dim ORs As New ADODB.Recordset
Dim oConn As New ADODB.Connection
Set cn = CreateObject("ADODB.Connection")
dbpath = Application.ActiveWorkbook.Path & "\DataBase3.accdb"


dbwb = Application.ActiveWorkbook.FullName




dbWs = Application.ActiveSheet.Name
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbpath
dsh = "[" & Application.ActiveSheet.Name & "$D4:D80]"
cn.Open scn


oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
           "Data Source=Z:\Access\BackTestSample.xlsb;" & _
           "Extended Properties=""Excel 8.0;"""
oConn.Close


NameField = ThisWorkbook.Worksheets("BackTest").Cells(4, 4)


'cn.Execute "ALTER TABLE BackTest ADD COLUMN [" & NameField & "] Number;"


ssql = "INSERT INTO BackTest ([" & NameField & "]) "
ssql = ssql & "SELECT * FROM [Excel 12.0;HDR=Yes;DATABASE=" & dbwb & "]." & dsh




Debug.Print ssql


cn.Execute ssql


End Sub
 
Upvote 0
Yes! I am Inserting the Fields separately. Actually, I am creating a new Field name every time before inserting values into the Field.

SQL INSERT adds new records to a table, it doesn't change existing records (unlike UPDATE), that's why you're getting the shifted data arrangement
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

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