Need help with exporting excel data to a Database

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
788
Office Version
  1. 365
Platform
  1. Windows
Hi,
ive been testing out the following VBA which works "sometimes" but isn't reliable and often after running the script nothing is written in the table.

Code:
Sub exportToAccess()


' 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
' connect to the Access database
Set cn = New ADODB.Connection


cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & "Data Source=C:\Test\DB.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "DATATABLE", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 2 ' the start 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("USERID") = Range("A" & r).Value
.Fields("USERNAM1") = Range("B" & r).Value
.Fields("USENAM2") = Range("C" & r).Value
.Fields("USRDAT") = Range("D" & r).Value
.Fields("USRTIM") = Range("E" & 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

im not sure what is wrong sometimes it works fine, sometimes just 1 record is added or nothing at all is added

i have data on 3 sheets
data1
data2
data3

and been running the following:

Code:
Sub exportAll()
Sheets("Temp").Range("A2:e2000").Value = Sheets("data1").Range("A2:e2000").Value
Sheets("Temp").Range("A2002:e4000").Value = Sheets("data2").Range("A2:e2000").Value
Sheets("Temp").Range("A4002:e6000").Value = Sheets("data3").Range("A2:e2000").Value

Range("A1:A6000").Select
    For Each r In Selection
        If r.Text = "" Then
            r.EntireRow.Delete
        End If
    Next r


Sheets("temp").Select

call exportToAccess

End Sub

appreciate any help
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Anyway to rewrite this part

r = 2 ' the start row in the worksheet


Do While Len(Range("A" & r).Formula) > 0
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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