How to Insert current date by clicking a form

wongray

Registered User.
Local time
Today, 20:42
Joined
Jan 28, 2009
Messages
15
Hi Again, Firstly, I like to thanks people who tough me a lot on how to use access programming, it does help me.

I do have another question that I hope I can get an answer.

I want to insert a date into a table (log) after I click a button in a form. but for some reason, I can't do it.

here is the script. please assist.
=========================
Private Sub Add_Record_Click()

Dim strsql As String
dd = Now()
strsql = "Insert Into Tbl_Project_Name(Log_ray) Values (dd)"
DoCmd.GoToRecord , , acNewRec
Exit_Add_Record_Click:

End Sub
=========================

Many thanks again.
wongray
 
You would need to modify to something like this:
Code:
Private Sub Add_Record_Click()

Dim strsql As String
[COLOR=red][B]Dim dd As Date[/B][/COLOR]

dd = Now()
strsql = "Insert Into Tbl_Project_Name(Log_ray) Values ([B][COLOR=red]#" &[/COLOR][/B] dd [COLOR=red][B]& "#[/B][/COLOR])"
  [COLOR=red][B] If Not Me.NewRecord Then[/B][/COLOR]
      DoCmd.GoToRecord , , acNewRec
   [COLOR=red][B]End If[/B][/COLOR]
Exit_Add_Record_Click:

End Sub
 
Hi SOS,

Thanks for the advice. I try and add it into the database but cannot insert the date into the ray_log field.

can you have please advice? here is the sample data

many thanks
 

Attachments

Two things:

1. I just realized that you aren't executing the code. Just assigning to strSQL isn't going to do anything. You need to do a

CurrentDb.Execute strSQL, dbFailOnError

in there.

2. But why do that when you can just have a control bound to that field on the form and you can set its visible property to NO and then you can just set it instead of trying to use SQL to update something that is already open in the form. You are likely going to get errors about two people updating the same record source if you have the bound form and the SQL executing on the same record.
 

Users who are viewing this thread

Back
Top Bottom