473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Display the date in a textbox in a form (Access VBA)

14
Hello,
I would like to display the current date (in a field called "Latest date" in a form) when a button is pressed in that form. The field in the form has a source in a table; thus, I am trying to run on the click() event of the button the following query to update the cell in the table and the field in the form.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "INSERT INTO [myTable] ([Latest date]) VALUES (" & Format(currDateTime, "\#yyyy-mm-dd hh:nn:ss\#") & ")"
Any suggestions?
Thank you for your time,
Regards,
Jul 6 '15 #1
4 4382
zmbd
5,501 Expert Mod 4TB
How you've explained this is a tad difficult to follow.

It would be a good thing to tell us what Access is doing and not doing for you.

With that said.

First, I suggest that you do not build your string within the function. I know that's the way they show to do this; however, it makes troubleshooting a pain.
Expand|Select|Wrap|Line Numbers
  1. Dim sSQL as String
  2. '...
  3. sSQL = "INSERT INTO " & _
  4.    "[myTable] ([Latest date]) VALUES (" & _ 
  5.     Format(currDateTime _
  6.        , "\#yyyy-mm-dd hh:nn:ss\#") & ")"
  7. '
  8. '[update]
  9. Debug.Print sSQL 
  10. '
  11. DoCmd.RunSQL sSQL
  12.  
Make this little change, and then we can insert a debug.print sSQL and run, presto, <ctrl><g> and we can see how the string is resolving.

Second your date field in your table would accept #MM/DD/YYYY# by default from code. The only reason to format is for a text field - and such is highly unadvisable for date/time data. Allen has an insight for International Dates in Access
If your system date is set to "yyyy-mm-dd hh:nn:ss" Access will often handle that without too many issues... sometimes. In any case... using the "#" delimitation, access is expecting #MM/DD/YYYY hh:nn:ss

Third, you have to force the form to either refresh or requery the bound recordset. Using an "Insert" you will have to me.requery
Jul 6 '15 #2
NeoPa
32,556 Expert Mod 16PB
ZMBD:
Second your date field in your table would accept #MM/DD/YYYY# by default from code. The only reason to format is for a text field
I'm not sure I would agree with that Z.

Even in the USA I would recommend that date values are Format()ed into the SQL string as it is only a coincidence that USA date formats are acceptable in a SQL string. Almost anywhere else in the world pushing the date value into the SQL string without formatting it first will cause failures and unrequired results. yyyy-m-d is a particularly good way to format the date as it implicitly makes clear that the date is formatted for SQL rather than accidentally or randomly dropped in, and is fully internationally independent.

One addition I would advise for full portability (Some countries recognise the '-' as a date separator and convert it automatically to a '\' and thus break the SQL string.) would be to escape the '-' characters with the backslash. Thus the format used would be :
Expand|Select|Wrap|Line Numbers
  1. \#yyyy\-m\-d hh:nn:ss
See Literal DateTimes and Their Delimiters (#) for more on this.

NB. For the original question, it seems that Now() would be a good way to express the current Date/Time. It works in both SQL and VBA.
As for the rest of the question, I'm equally confused as to what you're asking.
Jul 6 '15 #3
JM11
14
Actually my problem is that I am not able to save the value of the current date in my table. I am running an SQL query from a form to insert the date into the table, but it is not working.

(The column in the table corresponding to the date is always empty; nothing is being saved)
Jul 7 '15 #4
zmbd
5,501 Expert Mod 4TB
well, let's take a look at the sql-string you are using.
please make the changes as I suggested by pulling the string out of the function and then inserting the debug.print (I've updated the code block to show this)

Run your code, click on the button, etc...

once done, press <ctrl><g> and the VBA editor and immedates window should open, cut and paste the string in a post here
PLEASE format that string using the [CODE/] formatting tool in the toolbar.
Jul 7 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: daimler | last post by:
is there a way to enter texts into msaccess 2000 form from vba form codes? i am using dao automation ole to open the msaccess 2000 form but cannot seem to enter texts into the form's cell through...
0
by: andreas | last post by:
Hi there, I have the following situation: 1. DOT.NET Application is started 2. DOT.NET Application instantiates Access.Application.8 3. Opens a specified database (MDB) 4. DOT.NET...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
5
by: zwasdl | last post by:
In Access VBA, the immediate window only displays a couple hundred lines of my debug output. How do I display more lines there, for example 500 lines, or even 5000 lines? Is there a way to set the...
1
by: tmcjunkin | last post by:
I'm trying to back into this project by defining what the user needs to see when they open a form. I'm between beginner and intermediate with vba, but have been using access for years. When the...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
1
by: JFKJr | last post by:
Hello everyone, this one might be simple but driving me crazy! Your help will be greatly appreciated. Basically, I created a textbox bound to "Date" field in an Access VBA form. But when the...
4
by: JM11 | last post by:
Hello, I would like to loop over the rows of a form and check in each row the value of 1 column; depending on this value, the cell (for the corresponding row and column) should change color. This...
3
by: JM11 | last post by:
Hello, I would like to replace the character 'Â' with an empty space for all the values that belong to the column entitled 'Montant' of a form. Below is the code that I wrote but is not working. Any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.