How do I run this code?

T

Tony Williams

I want to set all my number fields to have a default value of 0. Bredan
Reynolds was good enough to provide me with this code
Public Sub SetDefaultFieldValues( _
ByVal FieldDataType As DAO.DataTypeEnum, _
ByVal NewDefaultValue As Variant)

Dim db As DAO.Database
Dim tdfs As DAO.TableDefs
Dim tdf As DAO.TableDef
Dim strPrefix As String
Dim flds As DAO.Fields
Dim fld As DAO.Field

Set db = CurrentDb
Set tdfs = db.TableDefs
For Each tdf In tdfs
strPrefix = UCase$(Left$(tdf.Name, 4))
If strPrefix <> "MSYS" And strPrefix <> "USYS" Then
Set flds = tdf.Fields
For Each fld In flds
If fld.Type = FieldDataType Then
fld.DefaultValue = NewDefaultValue
End If
Next fld
End If
Next tdf

End Sub

Public Sub TestSetDefaultFieldValues()

On Error GoTo ErrorHandler
DoCmd.Hourglass True
SetDefaultFieldValues dbByte, 0
SetDefaultFieldValues dbCurrency, 0
SetDefaultFieldValues dbDecimal, 0
SetDefaultFieldValues dbDouble, 0
SetDefaultFieldValues dbInteger, 0
SetDefaultFieldValues dbLong, 0
SetDefaultFieldValues dbSingle, 0
DoCmd.Hourglass False
MsgBox "Done!"

ExitProcedure:
Exit Sub

ErrorHandler:
DoCmd.Hourglass False
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume ExitProcedure

End Sub



However I'm a newbie at VBA. I have created a Module with this code in it
but how do I run it?
Simple steps would be appreciated. Thanks
Tony
 
D

Douglas J Steele

Since this is something you're only going to want to run once, there's no
need to create a user interface to run it. Simply go the Immediate window
(Ctrl-G), type TestSetDefaultFieldValues and hit the Enter key.
 

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