Solved

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

Posted on 2011-03-19
16
275 Views
Last Modified: 2012-05-11
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
Comment
Question by:dpawar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35171785
well you just

select getdate()
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 35171829
At the OnClick event of the button use code like this:

  Me!txtBoxBoundToYourDateField = Now

/gustav
0
 

Author Comment

by:dpawar
ID: 35171965
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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35172023
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
 

Author Comment

by:dpawar
ID: 35172126
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
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35172317
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
 

Author Comment

by:dpawar
ID: 35172551
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
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35172571
Go to menu Tools, References.
Add a reference to Microsoft DAO 3.6

/gustav
0
 

Author Comment

by:dpawar
ID: 35172744
How do I resolve this error:

Compile error:
Method or data member not found


This occurred near this:

 With rst
    .AddNew
      .Field
0
 

Author Comment

by:dpawar
ID: 35172756
The name of Table is LogIn

The field name is LabDate
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35172938
Oops, it should read:

      .Fields("LabDate").Value = Now
0
 

Author Comment

by:dpawar
ID: 35173032
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
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35173322
Code is correct, thus something else is going on. Even a typo would raise another error.

/gustav
0
 

Author Comment

by:dpawar
ID: 35176379
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
 

Author Closing Comment

by:dpawar
ID: 35176388
Put this code in Button not in Text Box
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 35176936
You are welcome!

/gustav
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question