Syntax Error in insert statement (1 Viewer)

Happyz

Registered User.
Local time
Today, 15:15
Joined
Nov 25, 2017
Messages
22
Hi everyone, i am encountering syntax error in my insert statement. How do i solve it?

Code:
Private Sub cmdAdd_Click()
 'add data to table
 CurrentDb.Execute "INSERT INTO InvestmentRecord(ID,[FSI/NonFSI],CompanyStock,Country,Branch,Currency,NumberOfShares,ActualCost,ImpairmentAllowance,GrossFairValueAdjustmentToEquity)" & _
                   " VALUES (" & Me.txtID & ",'" & Me.txtFSInonFSI & "','" & Me.txtSecurities & "','" & Me.txtCountry & "','" & Me.txtBranch & "','" & Me.txtCurrency & "','" & _
                     Me.txtNumberOfShare & "," & Me.txtOriginalCost & "," & Me.txtImpairmentAllowance & "," & Me.txtGrossfair & ")"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
error on this line you have extra single quote:


Me.txtCurrency & "','" & _
Me.txtNumberOfShare


should be:


Me.txtCurrency & "'," & _
Me.txtNumberOfShare
 

Happyz

Registered User.
Local time
Today, 15:15
Joined
Nov 25, 2017
Messages
22
error on this line you have extra single quote:


Me.txtCurrency & "','" & _
Me.txtNumberOfShare


should be:


Me.txtCurrency & "'," & _
Me.txtNumberOfShare

I tried to change and it still prompt me the same error.

Code:
CurrentDb.Execute "INSERT INTO InvestmentRecord(ID,[FSI/NonFSI],CompanyStock,Country,Branch,Currency,NumberOfShares,ActualCost,ImpairmentAllowance,GrossFairValueAdjustmentToEquity)" & _
                   " VALUE (" & Me.txtID & ",'" & Me.txtFSInonFSI & "','" & Me.txtSecurities & "','" & Me.txtCountry & "','" & Me.txtBranch & "','" & Me.txtCurrency & "'," & _
                     Me.txtNumberOfShare & "'," & Me.txtOriginalCost & "'," & Me.txtImpairmentAllowance & "'," & Me.txtGrossfair & "')"
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
better put this in a Standard Module

Code:
Public Function FixSQL(p As Variant) As Variant
    Select Case VarType(p)
    Case VbVarType.vbNull
        FixSQL = "Null"
    Case VbVarType.vbString
        FixSQL = """" & p & """" 'Chr(34) & p & Chr(34)
    Case VbVarType.vbBoolean, VbVarType.vbByte, _
        VbVarType.vbCurrency, VbVarType.vbDecimal, _
        VbVarType.vbDouble, VbVarType.vbInteger, _
        VbVarType.vbLong, VbVarType.vbSingle
        FixSQL = p
    Case VbVarType.vbDate
        FixSQL = "#" & Format(p, "mm/dd/yyyy") & "#"
    End Select
End Function

the on your insert query:

Currentdb.Execute "Insert Into InvestmentRecord(ID, [FSI/NonFSI]," & _
"CompanyStock,Country,Branch,Currency,NumberOfShares," & _
"ActualCost,ImpairmentAllowance,GrowwFairVauleAdjustmentToEquity) " & _
"SELECT " & _
FixSQL(Me.txtID) & "," & _
FixSQL(Me.txtFSInoFSI) & "," & _
FixSQL(Me.txtSecurities) & "," & _
FixSQL(Me.txtCountry) & "," & _
FixSQL(Me.txtBranch) & "," & _
FixSQL(Me.txtCurrency) & "," & _
FixSQL(Me.txtNumberOfShare) & "," & _
FixSQL(Me.txtOriginalCost) & "," & _
FixSQL(Me.txtImpairmentAllowance) & "," & _
FixSQL(Me.txtGrossfair) & ")"


also, if ID is Autonumber field in your table,
you should not include it in the query.
 

Happyz

Registered User.
Local time
Today, 15:15
Joined
Nov 25, 2017
Messages
22
better put this in a Standard Module

Code:
Public Function FixSQL(p As Variant) As Variant
    Select Case VarType(p)
    Case VbVarType.vbNull
        FixSQL = "Null"
    Case VbVarType.vbString
        FixSQL = """" & p & """" 'Chr(34) & p & Chr(34)
    Case VbVarType.vbBoolean, VbVarType.vbByte, _
        VbVarType.vbCurrency, VbVarType.vbDecimal, _
        VbVarType.vbDouble, VbVarType.vbInteger, _
        VbVarType.vbLong, VbVarType.vbSingle
        FixSQL = p
    Case VbVarType.vbDate
        FixSQL = "#" & Format(p, "mm/dd/yyyy") & "#"
    End Select
End Function

the on your insert query:

Currentdb.Execute "Insert Into InvestmentRecord(ID, [FSI/NonFSI]," & _
"CompanyStock,Country,Branch,Currency,NumberOfShares," & _
"ActualCost,ImpairmentAllowance,GrowwFairVauleAdjustmentToEquity) " & _
"SELECT " & _
FixSQL(Me.txtID) & "," & _
FixSQL(Me.txtFSInoFSI) & "," & _
FixSQL(Me.txtSecurities) & "," & _
FixSQL(Me.txtCountry) & "," & _
FixSQL(Me.txtBranch) & "," & _
FixSQL(Me.txtCurrency) & "," & _
FixSQL(Me.txtNumberOfShare) & "," & _
FixSQL(Me.txtOriginalCost) & "," & _
FixSQL(Me.txtImpairmentAllowance) & "," & _
FixSQL(Me.txtGrossfair) & ")"


also, if ID is Autonumber field in your table,
you should not include it in the query.



Hi,

I am still facing the run-time error '3134':
Syntax error in INSERT INTO statement.
The below is the coding:

Private Sub cmdAdd_Click()

CurrentDb.Execute "Insert Into InvestmentRecord(ID, [FSI/NonFSI]," & _
"CompanyStock,Country,Branch,Currency,NumberOfShar es," & _
"ActualCost,ImpairmentAllowance,GrossFairVauleAdjustmentToEquity) " & _
"SELECT " & _
FixSQL(Me.txtID) & "," & _
FixSQL(Me.txtFSInonFSI) & "," & _
FixSQL(Me.txtSecurities) & "," & _
FixSQL(Me.txtCountry) & "," & _
FixSQL(Me.txtBranch) & "," & _
FixSQL(Me.txtCurrency) & "," & _
FixSQL(Me.txtNumberOfShare) & "," & _
FixSQL(Me.txtOriginalCost) & "," & _
FixSQL(Me.txtImpairmentAllowance) & "," & _
FixSQL(Me.txtGrossfair) & ","

End Sub

The yellow arrow is pointed at the last line of coding:
FixSQL(Me.txtGrossfair) & ","
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
i don't have a comma there at the end.
sorry i have wrong spelling for
GrossFairValueAdjustmentToEquity

CurrentDb.Execute "INSERT INTO InvestmentRecord(ID,[FSI/NonFSI],CompanyStock,Country,Branch,Currency,NumberOfShares,ActualCost,ImpairmentAllowance,GrossFairValueAdjustmentToEquity) " & _
" SELECT " & _
FixSQL(Me.txtID) & "," & _
FixSQL(Me.txtFSInonFSI) & "," & _
FixSQL(Me.txtSecurities) & "," & _
FixSQL(Me.txtCountry & "," & _
FixSQL(Me.txtBranch & "," & _
FixSQL(Me.txtCurrency) & "," & _
FixSQL(Me.txtNumberOfShare) & "," & _
FixSQL(Me.txtOriginalCost) & "," & _
FixSQL(Me.txtImpairmentAllowance) & "," & _
FixSQL(Me.txtGrossfair) & ")"
 

Happyz

Registered User.
Local time
Today, 15:15
Joined
Nov 25, 2017
Messages
22
i don't have a comma there at the end.
sorry i have wrong spelling for
GrossFairValueAdjustmentToEquity

Hi, i am unable to add the record from subform into my table. Hence, i have attached my table above, hope that you are able to help me out on this! Thank you. Have a great day!
 

Attachments

  • Sample.zip
    108.2 KB · Views: 91

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
try this one.
 

Attachments

  • aaSample.zip
    52.5 KB · Views: 100

Happyz

Registered User.
Local time
Today, 15:15
Joined
Nov 25, 2017
Messages
22
try this one.

Hi, there is still an error. I have screenshot the error message and the line of codes with errors. Hope you guys out there can help me out. :)
 

Attachments

  • screenshots of error.jpg
    screenshots of error.jpg
    90.8 KB · Views: 159

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
from which button is that, Edit?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:15
Joined
May 7, 2009
Messages
19,249
here again
 

Attachments

  • aaSample.zip
    53.1 KB · Views: 87

Users who are viewing this thread

Top Bottom