SQL Code converting a String to a number

I am trying to write a SQL code to insert a record into a ODBC table.

The variable 'lscus' holds a 12 character string for customer # e.g. 000000000022.

The variable captures and displays a 12 character string, but when the INSERT SQL is run it converts the variable into a number e.g. 22.

Why is it converting to a number

How can I Insert a string value for 'lscus'

Thanks for your help

Private Sub cmdAddRecord_Click()
On Error GoTo Err_cmdAddRecord_Click
' Create todays date into a Format "YYYYMMDD" string
Dim stDate As String
Dim stTime As Double


stDate = CStr(Mid(Now(), 6, 4)) + CStr(Mid(format(Now(), "yyyy/mm/dd"), 6, 2)) + CStr(Mid(format(Now(), "yyyy/mm/dd"), 9, 2))
stTime = CDbl(CStr(Hour(Time)) + CStr(Minute(Time)) + CStr(Second(Time)))

Dim lscus As String

lscus = Me.SYS_NOTE_NAME_VALUE 'Capture the customer number = Primary Key needed on the insert
MsgBox (lscus)
MsgBox (stDate)
MsgBox (stTime)

'Create a SQL Insert statement to enter new notes against this customer
Dim stSQL As String
stSQL = "INSERT INTO SYSNOTES2 (SYS_NOTE_NAME,SYS_NOTE_NAME_VALUE,SYS_NOTE_DATE,SYS_NOTE_TIME,SYS_NOTE_TOPIC, " _
& " SYS_NOTE_1,SYS_NOTE_2,SYS_NOTE_3,SYS_NOTE_4,SYS_NOTE_5) VALUES(""AR-CUSTOMER"", " & lscus & " ," _
& " " & stDate & " , " & stTime & " , ""TEST"","" "","" "","" "","" "","" "");"



DoCmd.RunSQL (stSQL)

Exit_cmdAddRecord_Click:
Exit Sub

Err_cmdAddRecord_Click:
MsgBox Err.Description
Resume Exit_cmdAddRecord_Click

End Sub



Answer this question

SQL Code converting a String to a number

  • MPrutz

    If the datatype is a string you should use single quotes forinserting the data:

    CUSTOMER"", '" & lscus & "' ," _

    (There are single quotes before the double ones)

    HTH, Jens Suessmeyer.


  • ScottStamper

    Jens,

    Thank you!!

    That change has it working perfectly!

    Steve S.


  • csharpnewbie001

    What is the datatype of the target column
  • SQL Code converting a String to a number