• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

How to Print System Date & Time in Data Base Table field?

Hi!  How to Print System Date & Time in Data Base Table field?  I want to create a Button in a Access 2010 Form that when I click, it prints system Date & Time in the field in the Table.
Thanks.
0
dpawar
Asked:
dpawar
  • 8
  • 7
1 Solution
 
Aaron ShiloChief Database ArchitectCommented:
well you just

select getdate()
0
 
Gustav BrockCIOCommented:
At the OnClick event of the button use code like this:

  Me!txtBoxBoundToYourDateField = Now

/gustav
0
 
dpawarAuthor Commented:
This works for Text Box, But I wanted  like a "Button" control to do it.  How can I do it with Button control or make text box as Button control
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Gustav BrockCIOCommented:
Here is how. Call this from your button click:

Call InsertRecord("NameOfYourTable")

Public Sub InsertRecord(ByVal strTable As String)

  Dim dbs   As DAO.Database
  Dim rst   As DAO.Recordset
  
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset(strTable)
  With rst
    .AddNew
      .Field("NameOfYourDateTimeField").Value = Now
    .Update
    .Close
  End With
  Set rst = Nothing
  Set dbs = Nothing
  
End Function

Open in new window

/gustav
0
 
dpawarAuthor Commented:
I am new to Visual Basic, this should go on Event procedure on Click?  Most of the time it is Private Sub, you have here Public, any difference?
0
 
Gustav BrockCIOCommented:
You can put it all in your OnClick event:
  Dim dbs   As DAO.Database
  Dim rst   As DAO.Recordset
  
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("NameOfYourTable")
  With rst
    .AddNew
      .Field("NameOfYourDateTimeField").Value = Now
    .Update
    .Close
  End With
  Set rst = Nothing
  Set dbs = Nothing

Open in new window

/gustav
0
 
dpawarAuthor Commented:
I got following error:

Comile error:

User-defined type not defined

Here is the code that I entered

Private Sub Log_In_Click()
  Dim dbs   As DAO.Database
  Dim rst   As DAO.Recordset
 
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("LogIn")
  With rst
    .AddNew
      .Field("LabDate").Value = Now
    .Update
    .Close
  End With
  Set rst = Nothing
  Set dbs = Nothing
End Sub
0
 
Gustav BrockCIOCommented:
Go to menu Tools, References.
Add a reference to Microsoft DAO 3.6

/gustav
0
 
dpawarAuthor Commented:
How do I resolve this error:

Compile error:
Method or data member not found


This occurred near this:

 With rst
    .AddNew
      .Field
0
 
dpawarAuthor Commented:
The name of Table is LogIn

The field name is LabDate
0
 
Gustav BrockCIOCommented:
Oops, it should read:

      .Fields("LabDate").Value = Now
0
 
dpawarAuthor Commented:
This is the other error:

Run-Time error '91':
Object variable or With block variable not set


Code here:

Private Sub Log_In_Click()
  Dim dbs   As DAO.Database
  Dim rst   As DAO.Recordset
 
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("LogIn")
  With rst
    .AddNew
      .Fields("LabDate").Value = Now
    .Update
    .Close
  End With
  Set rst = Nothing
  Set dbs = Nothing
End Sub
0
 
Gustav BrockCIOCommented:
Code is correct, thus something else is going on. Even a typo would raise another error.

/gustav
0
 
dpawarAuthor Commented:
Hey, I finally resorted to your 1st solution.  I had put the code in text box that is way I did not like like but after putting the code in Button it worked great.  

Thanks for your help.

0
 
dpawarAuthor Commented:
Put this code in Button not in Text Box
0
 
Gustav BrockCIOCommented:
You are welcome!

/gustav
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now