Solved

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

Posted on 2004-04-27
18
692 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
 
LVL 51

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 51

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 51

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 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