Question : Problem with code to INSERT INTO newly created Access 2007 table

I am trying to create a table (tblTopDate) and insert a value (dteTopDate) into the newly created table.

The first part of the code (lines 1-12) work fine.  The tblTopDate table is properly created.

I declared a date variable (dteTopDate) to select the newest date from the table tblSales.

The scond part of the code (line 15) is to select dteTopDate
This code also works.  I added a msgbox to test and show the value of dteTopDay.

However, when I run the code, I get an Enter Paramater Value asking for dteTopDate.
If I enter a date, this date is inserted into the table.  What I cannot figure out is why dteTopDate is not automatically inserted into the table.

Any help will be appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Private Sub cmdImportSales_Click()

Dim dbs As Database, tbl As TableDef, fld As Field
Dim dteTopDate As Date

DoCmd.SetWarnings False
Set dbs = CurrentDb
Set tbl = dbs.CreateTableDef("tblTopDate")
Set fld = tbl.CreateField("TopDate", dbDate)
tbl.Fields.Append fld
dbs.TableDefs.Append tbl
dbs.TableDefs.Refresh

'Find top date in the existing tblSales table
dteTopDate = DMax("InvDate", "[tblSales]")

'This is a test to verify that the maximum date is being selected
MsgBox dteTopDate

'Add the Top Date to the newly created tblTopDate table
DoCmd.RunSQL "INSERT INTO [tblTopDate] ([TopDate]) VALUES (dteTopDate)"

End Sub

Answer : Problem with code to INSERT INTO newly created Access 2007 table

DoCmd.RunSQL "INSERT INTO [tblTopDate] ([TopDate]) VALUES (#" & dteTopDate & "#)"
Random Solutions  
 
programming4us programming4us