Insert Non Empty Cell into Access Database by using Excel vba

chacha123

Board Regular
Joined
Dec 4, 2014
Messages
79
Hi All,


Any idea how to insert non empty cell into Access Database by using Excel vba.


The below code is I found out.


Code:
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
'connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\SaveData\Database3.mdb;"
'open a recordset
Set rs = New ADODB.Recordset
rs.Open "Table1", cn, adOpenKeyset, adLockOptimistic, adCmdTable
'all records in a table
r = 3 'the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
'repeat until first empty cell in column A
With rs
'If Not IsEmpty(Len(Range("B" & r))) Then
.AddNew 'create a new record
'add values to each field in the record
.Fields("IName") = Range("A" & r).Value
.Fields("INumber") = Range("B" & r).Value
.Fields("IAddress") = Range("C" & r).Value
'add more fields if necessary…
.Update 'stores the new record
'Else: Exit Sub
'End If
End With
r = r + 1 'next row
Loop


rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub


The below is my insert data




[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Tel[/TD]
[TD]Address[/TD]
[/TR]
[TR]
[TD]Own[/TD]
[TD][/TD]
[TD]10,bb[/TD]
[/TR]
[TR]
[TD]Ali[/TD]
[TD]306660[/TD]
[TD]23,a[/TD]
[/TR]
[TR]
[TD]Abu[/TD]
[TD]5220[/TD]
[TD]7,c[/TD]
[/TR]
[TR]
[TD]Stone[/TD]
[TD][/TD]
[TD]6,b[/TD]
[/TR]
[TR]
[TD]Lily[/TD]
[TD]9600[/TD]
[TD]9,m[/TD]
[/TR]
</tbody>[/TABLE]


I need the data insert into database like this
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Ali[/TD]
[TD]306660[/TD]
[TD]23,a[/TD]
[/TR]
[TR]
[TD]Abu[/TD]
[TD]5220[/TD]
[TD]7,c[/TD]
[/TR]
[TR]
[TD]Lily[/TD]
[TD]9600[/TD]
[TD]9,m[/TD]
[/TR]
</tbody>[/TABLE]


Any idea? Thanks
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try changing the following line in your code:

Code:
[COLOR=#333333]If Not IsEmpty(Len(Range("B" & r))) Then[/COLOR]

To this:

Code:
[COLOR=#333333]If Not IsEmpty(Range("B" & r)) Then[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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