Consulting

Results 1 to 4 of 4

Thread: Writing MDB from Excel help needed.

  1. #1

    Question Writing MDB from Excel help needed.

    Hi,

    I am not sure whether I am posting the question in the correct place. Moderator please ensure.

    I want to write data to an existing Access MDB from Excel. It should support adding and updating data in different tables in the MDB. Is there any way to do it? Please help.

  2. #2
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    You can do all of this via DAO (data access objects) or ADO (Active-X Data Objects) connections. There are a number of excellent resources online that explain these two in detail. Look into them, and let us know if you have any other questions.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Some examples

    [vba]

    Sub AddData()
    Dim oConn As Object
    Dim oRS As Object
    Dim sSQL As String

    Set oConn = CreateObject("ADODB.Connection")
    oConn.Open = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & "c:\bob.mdb"

    sSQL = "INSERT INTO Contacts (FirstName, LastName,Phone, Notes) " & _
    " VALUES ('Bob','Phillips','01202 345678','me')"
    oConn.Execute sSQL

    oConn.Close
    Set oConn = Nothing
    End Sub

    Sub GetData()
    Const adOpenForwardOnly As Long = 0
    Const adLockReadOnly As Long = 1
    Const adCmdText As Long = 1
    Dim oRS As Object
    Dim sConnect As String
    Dim sSQL As String
    Dim ary

    sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & "c:\bob.mdb"

    sSQL = "SELECT * From Contacts"
    Set oRS = CreateObject("ADODB.Recordset")
    oRS.Open sSQL, sConnect, adOpenForwardOnly, _
    adLockReadOnly, adCmdText

    ' Check to make sure we received data.
    If Not oRS.EOF Then
    ary = oRS.getrows
    MsgBox ary(0, 0) & " " & ary(1, 0) & ", " & ary(2, 0)
    Else
    MsgBox "No records returned.", vbCritical
    End If

    oRS.Close
    Set oRS = Nothing
    End Sub

    Sub UpdateData()
    Const adOpenForwardOnly As Long = 0
    Const adLockReadOnly As Long = 1
    Const adCmdText As Long = 1
    Dim oConn As Object
    Dim oRS As Object
    Dim sConnect As String
    Dim sSQL As String
    Dim ary

    sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & "c:\bob.mdb"

    sSQL = "SELECT * From Contacts"
    Set oRS = CreateObject("ADODB.Recordset")
    oRS.Open sSQL, sConnect, adOpenForwardOnly, _
    adLockReadOnly, adCmdText

    ' Check to make sure we received data.
    If oRS.EOF Then
    MsgBox "No records returned.", vbCritical
    Else
    sSQL = "UPDATE Contacts " & _
    " SET Phone = 'None' " & _
    "WHERE FirstName = 'Bob' AND LastName = 'Phillips'"
    oRS.ActiveConnection.Execute sSQL

    sSQL = "SELECT * From Contacts"
    oRS.ActiveConnection.Execute sSQL
    ary = oRS.getrows
    MsgBox ary(0, 0) & " " & ary(1, 0) & ", " & ary(2, 0)
    End If


    oRS.Close
    Set oRS = Nothing
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4

    Smile

    Thanks Guys... will try it out...!

Posting Permissions

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