+ Reply to Thread
Results 1 to 5 of 5

writing cell contents to Access Database

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2007
    Posts
    6

    writing cell contents to Access Database

    Hi Folks

    I'm looking for a complete working example of how to write a single cell to a Access table using a SQL command.
    More to the point, i'm looking to UPDATE an access table field with a certain record ID with the current contents of a cell. I am using Excel 2003.

    I would be most thankful you give you permission to marry my daughter.

    Thanks in Advance
    Last edited by lushl0sn; 10-02-2008 at 05:37 PM.

  2. #2
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387
    Here's an example I found that's commented pretty well.
    Option Explicit
    Const stDB As String = "c:\area.mdb"
    Const stCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=" & stDB & ";"
     
     
    Sub updateAccess()
        Dim cnt As ADODB.Connection
        Dim stSQL As String
        Dim wbBook As Workbook
        Dim wsSheet As Worksheet
        Dim rnData As Range
        
        Set wbBook = ThisWorkbook
        Set wsSheet = wbBook.Worksheets(1)
        
        With wsSheet
            'Range in the source worksheet
            Set rnData = .Range("A2")
        End With
        
        'Instantiate the ADODB-object.
        Set cnt = New ADODB.Connection
        
        strquery = "select * from STATISTICA"
        cnt.Open stCon
            
            
            ' Create a recordset object.
            Dim rsPubs As ADODB.Recordset
            Set rsPubs = New ADODB.Recordset
            
            With rsPubs
                ' Assign the Connection object.
                .ActiveConnection = cnPubs
                ' Extract the required records.
                .Open strquery
                ' Copy the records into cell A4 on Sheet1.
                Worksheets("TABELLA_AND").Range("A2").CopyFromRecordset rsPubs
                ' Tidy up
                .Close
            End With
            
            
        'Close the connection
        cnt.Close
     
        
        'Release objects from memory
        Set cnt = Nothing
        Set rsPubs = Nothing
        'Delete the added values from the soure worksheet
        rnData.ClearContents
    End Sub

  3. #3
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Row 1 contains the fieldnames. Row 2 contains the data. If you are adding one record for one field value, add the field name in some scratch sheet area and the value in the cell below it and name the range to pass.

    One wife is a handful as it is...
    Sub demo()
      Dim objRS As Object, nwindPath As String
      Set objRS = CreateObject("ADODB.Recordset")
      nwindPath = ThisWorkbook.Path & "\nwind.mdb"
      
      Dim r As Range
      [a1] = "LastName"
      [b1] = "FirstName"
      [a2] = "Hobson"
      [b2] = "Kenneth"
      Set r = [a1:b2]
      r.Name = "MyRange"
     
      objRS.Open "INSERT INTO Employees SELECT * FROM [MyRange] IN '" & ThisWorkbook.FullName & "' 'Excel 8.0;'", _
          "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & nwindPath
     
      Set objRS = Nothing
    End Sub
    Last edited by Kenneth Hobson; 10-02-2008 at 04:50 PM.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Here is another method using Update. Update is needed to update the MDB as the recordset is disconnected until then. AddNew is used to add a new record.
    Sub ADO()
    '   the Microsoft ActiveX Data Objects 2.x Library
        Dim DBFullName As String
        Dim Cnct As String, Src As String
        Dim Connection As ADODB.Connection
        Dim Recordset As ADODB.Recordset
        Dim Col As Integer, Row As Integer, s As String
        
        'On Error GoTo EndNow
    '   Set window and calc off to speed updates
        SpeedOn
    
    '   Database information
        'DBFullName = "C:\myfiles\vbabook\Names.mdb"
        DBFullName = "u:\Material\ADO\NWind.mdb"
        
    '   Open the connection
        Set Connection = New ADODB.Connection
        Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
        Cnct = Cnct & "Data Source=" & DBFullName & ";"
        Connection.Open ConnectionString:=Cnct
        
    '   Create RecordSet
        Set Recordset = New ADODB.Recordset
    '   Next two lines critical to work in QPro properly.  Excel does not need them.
        Recordset.CursorType = adOpenKeyset
        Recordset.LockType = adLockOptimistic
          
        With Recordset
    '       Filter
            Src = "SELECT * FROM Orders "
            'Src = Src & "and CategoryID = 30"
            Recordset.Open Source:=Src, ActiveConnection:=Connection
    
    '   Cells.Clear 'Used in Excel to clear a sheet
    '       Write the field names
            'For Col = 0 To .Fields.Count - 1
               'Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name   'Excel method
            'Next
    
    If .RecordCount < 1 Then GoTo EndNow 'Query found no matching records
    '       Write the recordset by Excel method
            'Range("A1").Offset(1, 0).CopyFromRecordset Recordset
    'Add a new record (not pushed to the database until Update)
        MsgBox CStr(.RecordCount), vbInformation, "#Records"
        .AddNew
        Recordset("ShipName") = [Name!A2]
        Recordset("ShipAddress") = [Address!B6]
        Recordset("ShipCity") = Worksheets("City").Range("C3")
        .Update
         MsgBox CStr(.RecordCount), vbInformation, "#Records"
      
      End With
      
    EndNow:
        Set Recordset = Nothing
        Connection.Close
        Set Connection = Nothing
    '   Reset window and calculation
        SpeedOff
    End Sub

  5. #5
    Registered User
    Join Date
    10-31-2007
    Posts
    6
    Thanks Guys!

    You are the best!!!
    The examples worked and I can leave work happy!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Program a cell link to database cell
    By drewdb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2008, 06:45 PM
  2. When deleting cell contents, they come right back?
    By Paul987 in forum Excel General
    Replies: 3
    Last Post: 03-24-2008, 02:08 PM
  3. Excel formulas for checking contents within a cell.?
    By Aubbie in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-02-2008, 03:22 PM
  4. Update Access database through changes in Excel worksheet
    By munkayboi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2007, 07:16 PM
  5. Writing to a database using VBA
    By matpj in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-09-2007, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1