Late Binding - exporting data from excel to Access

Status
Not open for further replies.

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
114
HI everyone,

My code is below. I want to have a button that will allow someone to add a record to access without having to open up the VBA editor to select the reference library.

Code:
[COLOR=#333333]Private Sub CommandButton2_Click()MsgBox ("MAKE SURE THE ENTIRE INPUT TAB IS CORRECT")[/COLOR]

Application.ScreenUpdating = False


Dim db As database
Dim rs As DAO.Recordset


Set db = OpenDatabase("J:\samart\Projects\model output 2.0\discussion doc\Prelim data tape.accdb")
Set rs = db.OpenRecordset("Prelim List", dbOpenTable)


rs.AddNew
rs.Fields("Deal Name") = Sheets("import").Range("A2").Value
rs.Fields("Scenario") = Sheets("import").Range("B2").Value
rs.Fields("Deal Type") = Sheets("import").Range("C2").Value
rs.Fields("Shelf") = Sheets("import").Range("D2").Value




rs.Update


rs.Close
db.Close

Also if you know an easy way to create my own error message instead of the scary debug one (just incase someone freaks out in the office), that would be helpful as well.

ThanKS! Excel and access 2013

Pat
 

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.
This current macro requires the reference library for Access 2013 activated. This is defaulted to not-active.

If I use late binding, I know I don't have to select the reference library everytime.

How do I convert my current macro to using late binding?
 
Upvote 0
How about the code not opening the database and using ADO instead of DAO?
 
Last edited:
Upvote 0
Here you go... just change DB path , table name , Field name and Worksheet name according to your need.
Code:
Sub AddRecord_ExcelToAccess()
Dim ws As Worksheet
Dim cn As Object, rs As Object, r As Long
Set ws = Worksheets("Sheet1")
Set cn = CreateObject("ADODB.Connection")


cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"  ''''''change db path here


Set rs = CreateObject("ADODB.Recordset")


rs.Open "TableName”, cn, adOpenKeyset, adLockOptimistic, adCmdTable"  ''Change Tablename here


r = 3 ' the start row in the worksheet
Do While Len(ws.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("FieldName1") = ws.Range("A" & r).Value
   .Fields("FieldName2") = ws.Range("B" & r).Value
   .Fields("FieldName3") = ws.Range("C" & 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

in case if you are using 2010 or later replace with below line .

cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=C:\FolderName\DataBaseName.accdb;" ''''''change db path here
 
Last edited:
Upvote 0
I'm getting a Run-time error '2709' on rs.Open "Prelim List”, cn, adOpenKeyset, adLockOptimistic, adCmdTable" ''Change Tablename here



I tried it with [Prelim List]. Then it says that a "adOpenKeyset" hasn't been defined.

I know for a fact that its opening access, I just can't get the table open.
 
Upvote 0
Try adding this at the top of the code.
Code:
Const adOpenKeyset = 1
Const adLockOptimistic = 3
Const adCmdTable = 2
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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