+ Reply to Thread
Results 1 to 2 of 2

MS Access record delete from excel database query results

  1. #1
    Registered User
    Join Date
    03-25-2004
    Posts
    3

    MS Access record delete from excel database query results

    I have an Access *.mde file that makes you update records from within a specific GUI manually. If you erroneously enter incorrect info it just appends the correction to the history table of actions taken by that user.

    I have used excel to show a specific table for all of the history for a specific record.

    How can I use excel to delete the records that I select in the query?


    (Object) (Object Status History)
    ------------------------------------------------
    (312)( Item Entered Record )
    (312)( Item Record Appended )
    (312)( Item Record Shipped )
    (312)( Item Record Reshipped )
    (312)( Item Record Closed )
    (312)( Item Record Re-Opened )
    (312)( Item Record Closed )

    Say that I wanted to update this table query result from Excel
    and get rid of my erroneous shipped - reshipped items?

    VR,
    Chachi

  2. #2
    Registered User
    Join Date
    03-25-2004
    Posts
    3

    One Way to approach

    I figured out that it is a four step process...

    Step #1

    Sub DisplayObjectHistory()
    'Display the records for a certain object item
    Dim InputA As String
    InputA = InputBox("Enter ObjectId")
    MsgBox ("You entered: " & InputA)
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
    "ODBC;DSN=MS Access Database;DBQ=C:\Dbase\dBase.mde;DriverId=25;FIL=MS Access;MaxBuffe" _
    ), Array("rSize=2048;PageTimeout=5;")), Destination:=Range("A1"))
    .CommandText = Array( _
    "SELECT * FROM History History WHERE (History.ObjectID like'" & InputA & "%') ORDER BY History.DateEntered")
    .Name = "QGetting Object Info..."
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub


    Step #2
    Copy this info to sheet 2 (you can easily write a macro to do this)

    Step #3
    Delete all records from the same ObjectID with this

    Sub ADO_Delete()
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long, InputA As String
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\dBase\dBase.mde;"
    On Error Resume Next
    Set rs = New ADODB.Recordset
    InputA = InputBox("Enter ObjectID to Delete All records from")
    MsgBox ("You entered: " & InputA)
    rs.Open "Delete * From History ObjectID Where (History.ObjectID='" & InputA & "')", cn
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    End Sub

    Step #4
    Enter Edited Records from copied Worksheet

    Sub Update_dBase()
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=c:\dBase\dBase.mde;"
    On Error Resume Next
    Set rs = New ADODB.Recordset
    rs.Open "History", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    r = 2 ' row to start update records where info starts
    Do While Len(Range("A" & r).Formula) > 0
    With rs
    .AddNew
    .Fields("Object") = Range("A" & r).Value
    .Fields("Object_Status_History") = Range("B" & r).Value
    .Update
    End With
    r = r + 1
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    End Sub

    Maybe I can automate all of this into one macro??????

    Chachi WROX
    I was able to figure this out with a lot of beer and internet searches..
    LOL
    Helping to build better worlds...

+ Reply to Thread

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.6.0 RC 1