Solved Problem with INSERT Date value (2 Viewers)

tihmir

Registered User.
Local time
Today, 05:06
Joined
May 1, 2018
Messages
257
Hi,
I am trying to INSERT date form fields form with that code:
Code:
CurrentDb.Execute "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) " & _
                        "VALUES (" & Me.txtVersion & "", #" & Format(Me.txtVersionDate, "mm/dd/yyyy") & "#", '" & Me.txtWhatsNew & "')"
where the fields are: "Value" (Short Text), "VersionDate" (Date/Time), "WhatsNew" (Long Text)
 

Ranman256

Well-known member
Local time
Today, 08:06
Joined
Apr 9, 2015
Messages
4,337
Too many quotes, try:
CurrentDb.Execute "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) VALUES (" & Me.txtVersion & ", #" & Format(Me.txtVersionDate,'mm/dd/yyyy') & "#, '" & Me.txtWhatsNew & "')"
 

tihmir

Registered User.
Local time
Today, 05:06
Joined
May 1, 2018
Messages
257
Too many quotes, try:
CurrentDb.Execute "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) VALUES (" & Me.txtVersion & ", #" & Format(Me.txtVersionDate,'mm/dd/yyyy') & "#, '" & Me.txtWhatsNew & "')"
You are correct. Just replaced the single quote with double 'mm/dd/yyyy'
Thank you!

P.S. As a supplement to ask why the field "Value" ((Short Text)) save me the records the numbers with а comma (9,1) and not as I enter them with dot (9.10). How to format the field?
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 13:06
Joined
Feb 19, 2013
Messages
16,613
Possibly because Value is a reserved word and should not be used as a field name

if it is short text then you should have single quotes around the assignment

VALUES (‘“ & Me.txtVersion & "’, #” &

and if it is a number, perhaps the comma is your format for decimal numbers
 
Last edited:

Josef P.

Well-known member
Local time
Today, 14:06
Joined
Feb 2, 2023
Messages
826
P.S. As a supplement to ask why the field "Value" ((Short Text)) save me the records the numbers with а comma (9,1) and not as I enter them with dot (9.10). How to format the field?
Me.txtVersion is used as a numeric value.
Code:
VALUES (" & Me.txtVersion & ", ...
vs
Code:
VALUES ('" & Me.txtVersion & "', ...
 
Last edited:

tihmir

Registered User.
Local time
Today, 05:06
Joined
May 1, 2018
Messages
257
Me.txtVersion is use as numeric value.
Code:
VALUES (" & Me.txtVersion & ", ...
vs
Code:
VALUES ('" & Me.txtVersion & "', ...
Exactly! That was my mistake.
Тhank you guys so much! 🍻🙏🍻
 

Josef P.

Well-known member
Local time
Today, 14:06
Joined
Feb 2, 2023
Messages
826
[OT]
Tip: use a variable for the SQL string ... so you can check it.

Code:
dim InsertSql as String
InsertSql = "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) ...." & _
                        "VALUES ...."
debug.Print InsertSql
CurrentDb.Execute InsertSql, dbfailonerror

Code should not only work, but also be easy to read =>

If you then outsource the conversion of the value to a SQL string to functions, the code becomes even nicer. :)
Code:
dim InsertSql as String
InsertSql = "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew )" & _
            " VALUES (" & TextToSqlText(Me.txtVersion) & ", " & DateToSqlText(Me.txtVersionDate) & ", " & TextToSqlText(Me.txtWhatsNew) & ")"
debug.Print InsertSql
CurrentDb.Execute InsertSql, dbfailonerror
Note: TextToSqlText and DateToSqlText you have to create.

A further extension could look like this (StringFormat and StringFormatSQL functions can be found in Northwind 2):
Code:
InsertSql = StringFormat("INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) Values ({0}, {1}, {2})", _
                          TextToSqlText(Me.txtVersion), DateToSqlText(Me.txtVersionDate) , TextToSqlText(Me.txtWhatsNew)
or
Code:
InsertSql = StringFormatSQL("INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) Values ({0}, {1}, {2})", _
                            Me.txtVersion.value,  Me.txtVersionDate, Me.txtWhatsNew

The variant with a "real" parameter query should also not be forgotten (QueryDef.Execute).
 
Last edited:

tihmir

Registered User.
Local time
Today, 05:06
Joined
May 1, 2018
Messages
257
[OT]
Tip: use a variable for the SQL string ... so you can check it.

Code:
dim InsertSql as String
InsertSql = "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) ...." & _
                        "VALUES ...."
debug.Print InsertSql
CurrentDb.Execute InsertSql, dbfailonerror

Code should not only work, but also be easy to read =>

If you then outsource the conversion of the value to a SQL string to functions, the code becomes even nicer. :)
Code:
dim InsertSql as String
InsertSql = "INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew )" & _
            " VALUES (" & TextToSqlText(Me.txtVersion) & ", " & DateToSqlText(Me.txtVersionDate) & ", " & TextToSqlText(Me.txtWhatsNew) & ")"
debug.Print InsertSql
CurrentDb.Execute InsertSql, dbfailonerror
Note: TextToSqlText and DateToSqlText you have to create.

A further extension could look like this (StringFormat and StringFormatSQL functions can be found in Northwind 2):
Code:
InsertSql = StringFormat("INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) Values ({0}, {1}, {2})", _
                          TextToSqlText(Me.txtVersion), DateToSqlText(Me.txtVersionDate) , TextToSqlText(Me.txtWhatsNew)
or
Code:
InsertSql = StringFormatSQL("INSERT INTO 01_tblLocalVariables ( Value, VersionDate, WhatsNew ) Values ({0}, {1}, {2})", _
                            Me.txtVersion.value,  Me.txtVersionDate, Me.txtWhatsNew

The variant with a "real" parameter query should also not be forgotten (QueryDef.Execute).
Thanks for the helpful tips, Josef P.!
I appreciate it! 🙏
 

Users who are viewing this thread

Top Bottom