J
Jonathan Brown
I have a one to many relationship between an employees table and a
sitehistory table. What I'd like to do is, whenever I create a new record in
my employees table, automatically create an associated record in the
sitehistory table with site number 9 as the default site and Date() as the
default date.
EmployeesTable
Empnum <--Primary Key
FirstName
LastName
....
SiteHistoryTable
SHistoryNum <--auto-increment primary key
SiteNum <---foreign key to SitesTable
EmpNum <---Foreign key to EmployeesTable
StartDate
On my form my RecordSource is EmployeesTable. I've created the following
code for my add button:
----------------------------------------------------------------------------------------------------------
'Verify required fields
If IsNull(me.empnum) or isnull(me.firstname) or IsNull(me.lastname) or
IsNull(me.email) then
msgbox ("Please fill in all required fields")
exit sub
end if
'Add a default record to the SiteHistoryTable.
Dim db As Database
Dim strsql As String
strsql = "INSERT INTO SiteHistoryTable ( EmpNum, SiteNum, StartDate ) SELECT
" & me.empnum & " AS EmpNum, 9 as SiteNum, " & Date & " as StartDate;"
Set db = CurrentDb()
db.Execute strsql
Set db = Nothing
'Start a new record
DoCmd.GoToRecord , , acNext
sitehistory table. What I'd like to do is, whenever I create a new record in
my employees table, automatically create an associated record in the
sitehistory table with site number 9 as the default site and Date() as the
default date.
EmployeesTable
Empnum <--Primary Key
FirstName
LastName
....
SiteHistoryTable
SHistoryNum <--auto-increment primary key
SiteNum <---foreign key to SitesTable
EmpNum <---Foreign key to EmployeesTable
StartDate
On my form my RecordSource is EmployeesTable. I've created the following
code for my add button:
----------------------------------------------------------------------------------------------------------
'Verify required fields
If IsNull(me.empnum) or isnull(me.firstname) or IsNull(me.lastname) or
IsNull(me.email) then
msgbox ("Please fill in all required fields")
exit sub
end if
'Add a default record to the SiteHistoryTable.
Dim db As Database
Dim strsql As String
strsql = "INSERT INTO SiteHistoryTable ( EmpNum, SiteNum, StartDate ) SELECT
" & me.empnum & " AS EmpNum, 9 as SiteNum, " & Date & " as StartDate;"
Set db = CurrentDb()
db.Execute strsql
Set db = Nothing
'Start a new record
DoCmd.GoToRecord , , acNext