views:

191

answers:

1

Hi everyone....

Consider that i have an excel workbook and an access table not necessarily having a similar structure (i.e. They may not have same number of columns)

When i open the workbook the rows in the excel sheet get populated by the rows in access table (copied from the access table into the excel sheet's particular range of cells specified using macros).

Then i modify certain cells in the excel sheet.

I also have a button called "Save" in the excel sheet. When pressed, this will execute a macro.

My question how can i update the access table to reflect the changes in the excel sheet when the save button is clicked...?

Thanks for your time and suggestions...!

+3  A: 

You can use ADO and some code.

Here are some notes.

Let us say you get some data like so:

Sub GetMDB()
Dim cn As Object
Dim rs As Object

strFile = "C:\Docs\DBFrom.mdb"
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT * FROM Table1"
rs.Open strSQL, cn

With Worksheets(7)
    For i = 0 To rs.Fields.Count - 1
        .Cells(1, i + 1) = rs.Fields(i).Name
    Next

    rs.MoveFirst
    .Cells(2, 1).CopyFromRecordset rs
End With
End Sub

You could update the data using ADO like so:

Sub UpdateMDB()
Dim cn As Object
Dim rs As Object

''It wuld probably be better to use the proper name, but this is
''convenient for notes
strFile = Workbooks(1).FullName

''Note HDR=Yes, so you can use the names in the first row of the set
''to refer to columns
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

''Selecting the cell that are different
strSQL = "SELECT * FROM [Sheet7$] s " _
    & "INNER JOIN [;Database=c:\Docs\DBFrom.mdb;].Table1 t " _
    & "ON s.id=t.id " _
    & "WHERE s.Field1<>t.Field1"

rs.Open strSQL, cn, 1, 3 ''adOpenKeyset, adLockOptimistic

''Just to see
''If Not rs.EOF Then MsgBox rs.GetString

''Editing one by one (slow)
rs.MoveFirst
Do While Not rs.EOF
    rs.Fields("t.Field1") = rs.Fields("s.Field1")
    rs.Update
    rs.MoveNext
Loop

''Batch update (faster)
strSQL = "UPDATE [;Database=c:\Docs\DBFrom.mdb;].Table1 t " _
    & "INNER JOIN [Sheet7$] s " _
    & "ON s.id=t.id " _
    & "SET t.Field1=s.Field1 " _
    & "WHERE s.Field1<>t.Field1 "

cn.Execute strSQL

End Sub
Remou
I'm still in the process of learning VBA.. can u plz suggest a place where i can find some "seed" code to start up the process...?
SpikETidE
I have added some notes.
Remou
Thank You, Remou...!! I'll try this and gt back to you...! Thanks for your time...!
SpikETidE
Hi Remou... Got a little doubt.... My DB path is Database=C:\Documents and Settings\staff\Desktop\Excel Insert Issue\table.accdb.... What is the proper syntax to substitute it in [;Database=c:\Docs\DBFrom.mdb;]...?And Also.. I get the error 'No value given for one or more required parameters' on the line 'rs.Open strSQL, cn, 1, 3 ''adOpenKeyset, adLockOptimistic'
SpikETidE
You would use : [;Database=C:\Documents and Settings\staff\Desktop\Excel Insert Issue\table.accdb;] However, you will need a different connection string for ACCDB. See http://www.connectionstrings.com/access-2007As for the problem, it is likely to be something wrong with your SQL string, please post it.
Remou
The sql string is strSQL = "SELECT * FROM [Sheet2$] s " _
SpikETidE
Are you familiar with SQL? Have you set up test data that uses the same fields as mine, that is ID (unique key), Field1 (any sample data)?
Remou