Solved Syntax error (1 Viewer)

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
Hello Guys,

My code block is meant to add comments to the current comments. Any idea why the "else" block would be red (Syntax error)?
Code:
Private Sub cmdUpdateComments_Click()
    Dim strInput As String
    Dim strComments As String
    Dim sqlUpdate As String
    Dim d As Date
    
    strComments = Me.Comments.Value
    strInput = InputBox("Please enter new comment", "Update Comments")
    d = Date
    
    If IsNull(strComments) Then
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= '# & d & # & " - " & " & strInput & " '"
    Else
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]=' " & strComments & " & " " & # & d & # & " - " & " & strInput & " ' "
    End If
    
End Sub

Thank you
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:14
Joined
Oct 29, 2018
Messages
21,454
I think your quotes may be in the wrong places.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:14
Joined
Sep 21, 2011
Messages
14,238
Try Debug.Print sqlUpdate and see what is produced.?
 

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
Try Debug.Print sqlUpdate and see what is produced.?
It says:
compile error:
Syntax error

It is not printing the Debug.print. I also tried to remove the Else block and place it under an "If NOT IsNull(variable) Then" but still the same error
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:14
Joined
Oct 29, 2018
Messages
21,454
It says:
compile error:
Syntax error

It is not printing the Debug.print. I also tried to remove the Else block and place it under an "If NOT IsNull(variable) Then" but still the same error
Hi. Although your Else block may be getting colored in red, your If block is not correct either. Double check your placement of the double quotes in your code.

Sent from phone...
 

plog

Banishment Pending
Local time
Today, 16:14
Joined
May 11, 2011
Messages
11,638
Code:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]=' " & strComments & " & " " & # & d & # & " - " & " & strInput & " ' "

You are building a string correct? You can do that with literals (things inside quote marks) and variables (things that contain data other than what they say on their face). When you compose a string made of both of them you must seperate them by an ampersand (&). So if something isn't surrounded by quotes its interperted as a variable.

In the code I posted above, do you really have a variable named #?
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 17:14
Joined
Apr 27, 2015
Messages
6,321
I "think" PatAccess is under the impression it is needed when adding a date to a text field...

In which case:
SQL:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= """ & d & " - " & strInput & """"
 
Last edited:

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
Code:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]=' " & strComments & " & " " & # & d & # & " - " & " & strInput & " ' "

You are building a string correct? You can do that with literals (things inside quote marks) and variables (things that contain data other than what they say on their face). When you compose a string made of both of them you must seperate them by an ampersand (&). So if something isn't surrounded by quotes its interperted as a variable.

In the code I posted above, do you really have a variable named #?
I do not. d is a Date variable so I thought I have to surround it by #
 

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
I "think" PatAccess is under the impression it is needed when adding a date to a text field...

In which case:
SQL:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= """ & d & " - " & strInput & """"
Yes, that's what I thought. but what would I surround the variables with? I thought it was double quotes because shouldn't the query be:
Code:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= '(String)'"
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 17:14
Joined
Apr 27, 2015
Messages
6,321
Yes, that's what I thought. but what would I surround the variables with? I thought it was double quotes because shouldn't the query be:
Code:
sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= '(String)'"
As plog said, variables inside of quotes become literal strings: d = Date becomes "d" instead of 3/17/2021
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:14
Joined
Sep 21, 2011
Messages
14,238
As plog said, variables inside of quotes become literal strings: d = Date becomes "d" instead of 3/17/2021
If anything I would have thought the Format() option would be needed to convert the date to the format required AND make it a string for the comment concatenation?
I would have put the new comment into a separate variable, so I could debug.print it and then just set the Comment field to that variable in the SQL.?

Also wouldn't you need a WHERE statement in there as to which record are you updating?
 

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
If anything I would have thought the Format() option would be needed to convert the date to the format required AND make it a string for the comment concatenation?
I would have put the new comment into a separate variable, so I could debug.print it and then just set the Comment field to that variable in the SQL.?

Also wouldn't you need a WHERE statement in there as to which record are you updating
I've placed the WHERE clause in it but something is still wrong. It is saying missing parameters for my strComments, strd, and strInput variables. I honestly don't know what I am doing wrong here
Here is what I have now
Code:
Private Sub cmdUpdateComments_Click()
    Dim strInput As String
    Dim strComments As String
    Dim sqlUpdate As String
    Dim strd As String
    Dim d As Date
    
    strComments = Me.Comments.Value
    strInput = InputBox("Please enter new comment", "Update Comments")
    d = Date
    strd = Format(d, "Short date")
    
    If IsNull(strComments) Then
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= strd & " - " & strInput WHERE 'StateCorpLicID=" & Me.StateCorpLicID & "'"
    Else
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= strComments & ' ' & strd & ' - ' & strInput WHERE 'StateCorpLicID=" & Me.StateCorpLicID & "'"
    End If
    Debug.Print sqlUpdate
    DoCmd.RunSQL sqlUpdate
End Sub

And here is what the Debug print is showing
Code:
UPDATE TblStateCorpLic SET [Comments]= strComments & ' ' & strd & ' - ' & strInput WHERE 'StateCorpLicID=54'

When I hover over strComments (in my immediate window), it is giving me the correct information. But when I hover over strd and strInput, they are both empty

I step through the code and
strComments holds the correct information
strd holds the correct information
strInput holds the correct information

However, the problem seems to happen within the SQL statement because sqlUpdate is empty?
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:14
Joined
Sep 21, 2011
Messages
14,238
I've placed the WHERE clause in it but something is still wrong. It is saying missing parameters for my strComments, strd, and strInput variables. I honestly don't know what I am doing wrong here
Here is what I have now
Code:
Private Sub cmdUpdateComments_Click()
    Dim strInput As String
    Dim strComments As String
    Dim sqlUpdate As String
    Dim strd As String
    Dim d As Date
   
    strComments = Me.Comments.Value
    strInput = InputBox("Please enter new comment", "Update Comments")
    d = Date
    strd = Format(d, "Short date")
   
    If IsNull(strComments) Then
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= strd & " - " & strInput WHERE 'StateCorpLicID=" & Me.StateCorpLicID & "'"
    Else
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= strComments & ' ' & strd & ' - ' & strInput WHERE 'StateCorpLicID=" & Me.StateCorpLicID & "'"
    End If
    Debug.Print sqlUpdate
    DoCmd.RunSQL sqlUpdate
End Sub

And here is what the Debug print is showing
Code:
UPDATE TblStateCorpLic SET [Comments]= strComments & ' ' & strd & ' - ' & strInput WHERE 'StateCorpLicID=54'

When I hover over strComments (in my immediate window), it is giving me the correct information. But when I hover over strd and strInput, they are both empty

I step through the code and
strComments holds the correct information
strd holds the correct information
strInput holds the correct information

However, the problem seems to happen within the SQL statement because sqlUpdate is empty?
You concatenation is off. Try
Code:
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= '" & strd & "' - '" & strInput & "' WHERE StateCorpLicID=" & Me.StateCorpLicID
 

PatAccess

Registered User.
Local time
Today, 17:14
Joined
May 24, 2017
Messages
284
You concatenation is off. Try
Code:
        sqlUpdate = "UPDATE TblStateCorpLic SET [Comments]= '" & strd & "' - '" & strInput & "' WHERE StateCorpLicID=" & Me.StateCorpLicID
Thank you. My concatenation was indeed off. It works.
 

Users who are viewing this thread

Top Bottom