Solved Problems inserting records into a table using SQL : maybe an error in my concatenation ? (1 Viewer)

alan2013

Registered User.
Local time
Yesterday, 23:54
Joined
Mar 24, 2013
Messages
69
Could someone help me pinpoint where I'm going wrong with the following, please ?

I'm attempting to insert a record into a Table named tblTasks, using the VBA code below.

Code:
Dim strTaskInsertionSQL As String
Dim strTaskTitle As String

strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
strTaskTitle = "Task 1"

DoCmd.RunSQL strTaskInsertionSQL

However, it's not working. I get the following message, and when I click on 'Yes', no record is inserted.

1709976608850.png





If instead I use the following code, it DOES work.

Code:
Dim strTaskInsertionSQL As String
Dim strTaskTitle As String

strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
strTaskTitle = "Task 1"

DoCmd.RunSQL "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"

As I want to be able to expand on this and introduce other 'Task titles', I'd really like to be able to use strTaskInsertionSQL.

Why is the first block of code not picking up the value for strTaskTitle ? Perhaps an issue with the cancatenation of strTaskInsertionSQL ?

Thanks in advance for any pointers.
 

ebs17

Well-known member
Local time
Today, 08:54
Joined
Feb 7, 2020
Messages
1,946
Code:
strTaskTitle = "Task 1"
strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
Swap the order. The variable should first contain the correct value before you use it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:54
Joined
Sep 21, 2011
Messages
14,299
Debug.print your sql string.
That should show you your errors.
 

GPGeorge

Grover Park George
Local time
Yesterday, 23:54
Joined
Nov 25, 2004
Messages
1,867
Could someone help me pinpoint where I'm going wrong with the following, please ?

I'm attempting to insert a record into a Table named tblTasks, using the VBA code below.

Code:
Dim strTaskInsertionSQL As String
Dim strTaskTitle As String

strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
strTaskTitle = "Task 1"

DoCmd.RunSQL strTaskInsertionSQL

However, it's not working. I get the following message, and when I click on 'Yes', no record is inserted.

View attachment 112976




If instead I use the following code, it DOES work.

Code:
Dim strTaskInsertionSQL As String
Dim strTaskTitle As String

strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
strTaskTitle = "Task 1"

DoCmd.RunSQL "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"

As I want to be able to expand on this and introduce other 'Task titles', I'd really like to be able to use strTaskInsertionSQL.

Why is the first block of code not picking up the value for strTaskTitle ? Perhaps an issue with the cancatenation of strTaskInsertionSQL ?

Thanks in advance for any pointers.
Is TaskCurrent a boolean (yes/no) field or a short text field?
 

alan2013

Registered User.
Local time
Yesterday, 23:54
Joined
Mar 24, 2013
Messages
69
Is TaskCurrent a boolean (yes/no) field or a short text field?
It's a Short Text field, in which I enter Yes or No. Thanks for your interest. In any case, my error seems to be in the order of my lines of code, as pointed out by ebs17.
 

alan2013

Registered User.
Local time
Yesterday, 23:54
Joined
Mar 24, 2013
Messages
69
Code:
strTaskTitle = "Task 1"
strTaskInsertionSQL = "INSERT INTO tblTasks (TaskTitle, TaskCurrent, Status) VALUES ('" & strTaskTitle & "', 'Yes', 'A');"
Swap the order. The variable should first contain the correct value before you use it.
Thanks, ebs17. I've taken that on board, and altered by code to take that into account.
 

Users who are viewing this thread

Top Bottom