Clear Access Table Records using VBA from Excel

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
I need your help to Clear/Delete the content in Access Table Records using VBA from Excel

I have a Access Table "StudentsRecords" as my Database with Excel 2010 being by front end application to feed in data.

To create a new record, I check the table is the student record is existing for the Department and for the given week (students records will be created seperate for each Department and Week). Which means A student will have only 1 Unique record for any given Week & Department combined. I know this is funny, but this has to be this way to track the students records over a period for analysis.

Access Table
StudentName
Department
UpdateWeek
CurrentProgress
Achievements
CrissManagement
26
A
B+
JohnFinance26AB++
PeterFinance26AB-
CrissManagement
27
A
C

<tbody>
</tbody>

I want to move next level, defining what is Recent Update (Latest Record) for the given Student and Department Combined by looking at the UpdateWeek Column

For example there are 3 records available for Student "Criss" in Department "Management" in all 3 records UpdateWeek 28 is the latest available record.

Access Table
StudentName
Department
UpdateWeek
CurrentProgress
Achievements
Latest
CrissManagement26AB+
JohnFinance26AB++Yes
PeterFinance26AB-Yes
CrissManagement27AC
Criss
Management
28
A
B+
Yes

<tbody>
</tbody>


I want to how to Fill the "Latest" Column with "Yes" in matching with the criteria and rest of the records should be Blank or "No".

Below the Excel VBA code I use to define if the student record is existing in Access Table with given Department and Week, and Create a record if not exist or update if available.

Code:
Function UpdateDataBase(iDepartment, WeekNum, Student)
    Dim cn As ADODB.Connection, rs As ADODB.Recordset
    Dim sProduct As String, sVariety As String, cPrice As Variant
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
        "Data Source=C:\Tool-BetaTest\Database21.accdb;"
    
        ' open a recordset
        Set rs = New ADODB.Recordset
        rs.Open "StudentsActivities", cn, adOpenKeyset, adLockOptimistic, adCmdTable
            
            rs.Filter = "Department='" & iDepartment & "' AND UpdateWeek='" & WeekNum & "' AND StudentName='" & Student & "'"
            If rs.EOF Then
                'Student record not available, create new
                rs.Filter = ""
                rs.AddNew
                'Proceed further
            Else
                'Student record Available, Update it
                'Proceed further
            End If
            rs.Update
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Function
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
to clear a table, run a delete query... docmd.openquery "qdDelAllInTbl"
delete * from table
 
Upvote 0
Thanks for the reply, but I do not want to delete entire rows.

I want to delete data from only one selected Column where the Content in Another Column contain specified value

I tried below code but it did not worked.

Code:
Delete * from Table.[Column3] where Table.[Column1] is like "Col1DataLookup" and Table.[Column2] is like "Col2DataLookup"

Any Suggestions?
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top