Solved

How to store  date +time in access database  from VB6.

Posted on 2004-04-27
18
689 Views
Last Modified: 2008-03-10
Hi
In my application I am displaying date and time and want to store both in date field of access  database .
i.e . in my current  VB application  I am entering date like this.
cn.execute “Insert into emp (hiredate) values (‘” & date.caption) & “ ‘)

I have one label and I am assigning value to  it using  timer  so   label.cation display  current time in my application
I want to add this  time value  in   horedate field  of emp and  so when I wrote  like
 
cn.execute “Insert into emp (hiredate) values (‘” & date.caption) & “ ‘+ ‘”&lbl_time.caption)&”’ )
it gives me  error invalid  data time ..

format Hiredate field in access database is of  general date  ..

Regards
0
Comment
Question by:rehman123
  • 4
  • 4
  • 3
  • +3
18 Comments
 

Expert Comment

by:black_help
ID: 10930642
Why dont you just add it like this:
cn.execute “Insert into emp (hiredate) values (‘” & now() &”’ )
?
0
 
LVL 3

Expert Comment

by:diegojserrano
ID: 10930704
try
cn.execute “Insert into emp (hiredate) values (‘" & date.caption & " " &lbl_time.caption & "')"
if it doesn't work then msgbox “Insert into emp (hiredate) values (‘" & date.caption & " " &lbl_time.caption & "')" and post here exactly what you see.
0
 

Author Comment

by:rehman123
ID: 10931052
it is giving syntex error
Expected End of statement.
0
 
LVL 3

Expert Comment

by:diegojserrano
ID: 10931078
what line is giving that error?
post the code
0
 

Expert Comment

by:black_help
ID: 10931140
cn.execute "Insert into emp (hiredate) values ('" & date.caption & " " & lbl_time.caption & "')"
0
 
LVL 3

Expert Comment

by:diegojserrano
ID: 10931164
hey, black_help is rigth check out  ' and "
if you only copy and paste from here you can have different characters
0
 

Author Comment

by:rehman123
ID: 10931165
and with now( )  function i got this error ..
Number of query value and destination fields are  not the  same...
0
 

Expert Comment

by:shutuplonnie
ID: 10931178
then your sql statement isn't the same as your database structure..
0
 

Expert Comment

by:shutuplonnie
ID: 10931203
How many fields in the database? and what are they?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10931230
Try:

cn.execute “Insert into emp (hiredate) values (Now())

?

or even simply, you can directly set the Default Now value into a Field, Try open the table in Design View, then select the hiredate field, try go to it's Default Property, enter =Now(), then try save the table. And execute your Statement again, like:

cn.execute “Insert into emp (employeeName, age) values ('ABC', 25)

etc

Hope this helps
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10931272
Missing to close the statement, should be:

cn.execute “Insert into emp (hiredate) values (Now())"

and

cn.execute “Insert into emp (employeeName, age) values ('ABC', 25)"
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10933370
Access requires that you enclose date/time fields with pound(#) signs, so:

cn.execute "Insert into emp (hiredate) values (#" & date.caption) & "#) "
0
 

Author Comment

by:rehman123
ID: 10936206
shutuplonnie and black_help
I tried your  lines  but it gives me error  end of  statement , i copied and pasted from here . u pls check it  out ..
and azrasound your  syntex worked  but i want to store date and time  not just date ..  thanks.
0
 

Accepted Solution

by:
black_help earned 500 total points
ID: 10937219
cn.execute "Insert into emp (hiredate) values (#" & date.caption & " " & lbl_time.caption & "#) "
0
 

Expert Comment

by:black_help
ID: 10958657
Does it work now?
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10960785
>>cn.execute “Insert into emp (hiredate) values (Now())"
Is that not working or what.. ? or you need the same Date/Time as what you had displayed? If yes, just try others suggestion, their suggestion Should works.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10970609
Good job appending MY answer...
0
 

Author Comment

by:rehman123
ID: 10970611
thanks black
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now