Going trough all tables in Access with macro?

donalde

Board Regular
Joined
Feb 27, 2002
Messages
80
Hello. This will be 1st macro I code with VBA in Access, so be patient, I may ask silly questions. (Also, I also have very little experience using Access)

I am trying to do macro, which activates each table in my access DB, clears tables contents, then copies data from excel, and paste append clipboard to table. Then close table, and opens next table. Table and Excel sheet have exactly same name, depending name table has different dimensions and formats.

How do I do macro that goes trough all tables I have in my Access database? In excel going trough all worksheets I’d do following method:

Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
Select Case sh.Name
Case “Sheet1”
… some function calls in here
Case “Sheet9”
… some other function calls in here
End Select
Next

But how I do same in access?

Before copying data to clipboard, I need to run macro in excel which search correct area from excel sheet, but that macrocode is easy to code for me. Area which I need to copy varies in each excel sheet.

I can do delete and copy manually, but it is quite boring, since I have some 40+ tables to update.

I am using Access 2k, Excel 2k
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Pasi

I use the following code to check the length of the text in every text field in every table in my database. Perhaps it is of some use?
Code:
Sub TestTextFields()
Dim db As Database
Dim tdf As TableDef
Dim rst As Recordset
Dim fld As Field
Dim MaxLength

    Set db = CurrentDb
    
    For Each tdf In db.TableDefs
        
        For Each fld In tdf.Fields
            If fld.Type = dbText Then
                Set rst = db.OpenRecordset("SELECT Max(Len([" & fld.Name & "])) FROM [" & tdf.Name & "];")
                MaxLength = rst.Fields(0).Value
                If MaxLength < fld.Size Then
                    Debug.Print tdf.Name, fld.Name & "(" & fld.Size&; ")", MaxLength
                End If
            End If
        Next
    Next
    
    Set rst = Nothing
    Set db = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,827
Messages
6,162,202
Members
451,752
Latest member
freddocp

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