Coding for exporting data from a spreadsheet to a database

hey yer barred

Board Regular
Joined
Jun 19, 2007
Messages
232
Hi All!

I have this coding beloew that is meant to export some cell values from a spreadsheet to a database....but it does not work.

Private Sub CommandButton1_Click()

Code:
Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sPath As String
Dim sSQL As String

sPath = "J:\My Documents\TestDB's\CreditCare\TestDB4CC.mdb"
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath
db.ConnectionString = sConn
db.Open
sSQL = "SELECT * FROM tblMain"
With rs
    rs.ActiveConnection = db
    rs.LockType = adLockBatchOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sSQL
        If rs.EOF = False Then
        rs.MoveLast
        End If
    rs.AddNew
    rs!Name = "Test"
    rs.Update
    rs.Close
End With

MsgBox "Done!", vbInformation, "Test For Credit Care"

db.Close
Set db = Nothing
End Sub

I dont have any debug errors....it goes through the code fine...but does not update the database!

As you can see, this is a test piece of coding, just to make sure it works before I make the proper version.

Can anyone help please!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi

I think you need to use a Server-side cursor and probably change the LockType too:

Code:
Sub Test()
Dim db As New ADODB.Connection 
Dim rs As New ADODB.Recordset 
Dim sPath As String 
Dim sSQL As String 

sPath = "J:\My Documents\TestDB's\CreditCare\TestDB4CC.mdb" 
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPath 
db.ConnectionString = sConn 
db.Open 
sSQL = "SELECT * FROM tblMain" 
With rs 
    .ActiveConnection = db 
    .LockType = adLockOptimistic 'changed
    .CursorType = adOpenKeyset 'new line
    .CursorLocation = adUseServer
    .Open sSQL 
        'If rs.EOF = False Then   ===unnecessary
        'rs.MoveLast                  ===unnecessary
        'End If                           ===unnecessary
    .AddNew 
    rs!Name = "Test" 
    .Update 
    .Close 
End With 

MsgBox "Done!", vbInformation, "Test For Credit Care" 

db.Close 
Set db = Nothing 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,659
Messages
6,120,786
Members
448,993
Latest member
Seri

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