+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    03-20-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003
    Posts
    1

    Updating Access record from Excel

    Hello everyone, I am new to this forum, I need a macro where I can update particular record in Access table from Excel worksheet. I have a macro where I can append/Add new records in Access table from Excel work sheet :

    Private Sub CommandButton2_Click()

    'Author : Ken Puls (www.excelguru.ca)
    'Macro purpose: To add record to Access database using ADO and SQL
    'NOTE: Reference to Microsoft ActiveX Data Objects Libary required

    Dim cnt As New ADODB.Connection, _
    rst As New ADODB.Recordset, _
    dbPath As String, _
    tblName As String, _
    rngColHeads As Range, _
    rngTblRcds As Range, _
    colHead As String, _
    rcdDetail As String, _
    ch As Integer, _
    cl As Integer, _
    notNull As Boolean

    'Set the string to the path of your database as defined on the worksheet
    dbPath = ActiveSheet.Range("B1").Value
    tblName = ActiveSheet.Range("B2").Value
    Set rngColHeads = ActiveSheet.Range("tblHeadings")
    Set rngTblRcds = ActiveSheet.Range("tblRecords")

    'Concatenate a string with the names of the column headings
    colHead = " ("
    For ch = 1 To rngColHeads.Count
    colHead = colHead & rngColHeads.Columns(ch).Value
    Select Case ch
    Case Is = rngColHeads.Count
    colHead = colHead & ")"
    Case Else
    colHead = colHead & ","
    End Select
    Next ch

    'Open connection to the database
    cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & dbPath & ";"

    'Begin transaction processing
    On Error GoTo EndUpdate
    cnt.BeginTrans

    'Insert records into database from worksheet table
    For cl = 1 To rngTblRcds.Rows.Count

    'Assume record is completely Null, and open record string for concatenation
    notNull = False
    rcdDetail = "('"

    'Evaluate field in the record
    For ch = 1 To rngColHeads.Count
    Select Case rngTblRcds.Rows(cl).Columns(ch).Value
    'if empty, append value of null to string
    Case Is = Empty
    Select Case ch
    Case Is = rngColHeads.Count
    rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)"
    Case Else
    rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'"
    End Select

    'if not empty, set notNull to true, and append value to string
    Case Else
    notNull = True
    Select Case ch
    Case Is = rngColHeads.Count
    rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')"
    Case Else
    rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','"
    End Select
    End Select
    Next ch

    'If record consists of only Null values, do not insert it to table, otherwise
    'insert the record
    Select Case notNull
    Case Is = True
    rst.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, cnt
    Case Is = False
    'do not insert record
    End Select
    Next cl

    EndUpdate:
    'Check if error was encounted
    If Err.Number <> 0 Then
    'Error encountered. Rollback transaction and inform user
    On Error Resume Next
    cnt.RollbackTrans
    MsgBox "There was an error. Update was not succesful!", vbCritical, "Error!"
    Else
    On Error Resume Next
    cnt.CommitTrans
    End If

    'Close the ADO objects
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing
    On Error GoTo 0
    End Sub

    Please help me in building a macro where I can update field "StatusRemarks" where "VendorCode" and "RecdDate" matches the value.

    I have attached my excel file with same fields as in my Access table.

    Your help will be highly appreciated.

    Thanks in advance
    Attached Files Attached Files
    Last edited by P30500612; 03-20-2010 at 04:11 AM.

  2. #2
    Valued Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Eastlake Ohio
    MS-Off Ver
    2010
    Posts
    1,000

    Re: Updating Access record from Excel

    Please wrap your code in tags
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # button at the top of the post window. If you are editing an existing post, press Go Advanced to see the # button. For more information about these and other tags,
    http://www.excelforum.com/misc.php?do=bbcode#code

    Also, this may better be suited in the "Microsoft Office Application Help - Access Help forum"
    http://www.excelforum.com/microsoft-...ss-help-forum/
    Regards

    Rick
    Win7, Office 2010

Thread Information

Users Browsing this Thread

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

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.2.0