Run Macro

C

Chris

I need a macro to populate a table. However if the table
already contains data, I want the macro to stop. How do I
write the code to do that?
 
B

Bob Flenniken

-----Original Message-----
I need a macro to populate a table. However if the table
already contains data, I want the macro to stop. How do I
write the code to do that?
.

I think that would be much easier with code. The function
below assumes using an append query to populate the table
and using DAO. The operable line is "If Not rst.BOF". If
the recordset's BOF property is true, it is Before the
first record meaning there are no records in it.

Function Populate()
On Error GoTo Err_Proc
Dim rst as DAO.Recordset
Dim db as DAO.Database

Set db = CurrentDB()
Set rst = db.OpenRecordset
("tblNameToPopulate",dbOpenDynaset)

If Not rst.BOF Then
DoCmd.SetWarnings False
DoCmd.OpenQuery "qappAppendDataToTable"
Else
MsgBox "The table already had data in
it.",vbInformation+vbOKOnly,"Table Has Data"
EndIf

Exit_Proc:
DoCmd.SetWarnings True
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
If Not db Is Nothing Then db.Close
Set db = Nothing

Err_Proc:
MsgBox "Error: " & Err.Number & ", " & Err.Description
Resume Exit_Proc

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top