+ Reply to Thread
Results 1 to 9 of 9

Sql not executing, insert into syntax error

  1. #1
    Registered User
    Join Date
    07-04-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    38

    Sql not executing, insert into syntax error

    Hello All,
    I have created a userform where the data needs to store in access db automatically,
    However i m getting a syntax error at INSERT INTO statement,yellow coloured at .execute stSQL tried in google to solve the error, but seems no assistance.
    Here is my code:
    Public Sub Save_Click()
    'Initialize all variables
    Dim Cn As New ADODB.Connection
    Dim Rs As New ADODB.recordSet
    Dim stDB As String, stSQL As String, stProvider As String
    stDB = "Data Source= E:\SAMI.accdb"
    stProvider = "Microsoft.ACE.OLEDB.12.0"
    With Cn
    ' Opening connection to database
    .ConnectionString = stDB
    .Provider = stProvider
    .Open
    'SQL Statement of what I want from the database
    stSQL = "INSERT INTO SAMI_Data (CSTName, EmailSub,) " & _
    "Values ('" & SAMI_Tracking.Cst_Nme.Value & "', '" & SAMI_Tracking.Email_Sub.Value & "',')"
    .Execute(stSQL)
    End With
    Set Rs = Nothing
    Set Cn = Nothing
    End Sub

    It would be really appreciable if any 1 can really help me on the same.

    Many thanks

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Sql not executing, insert into syntax error

    I reckon your SQL oughta be
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    07-04-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    38

    Re: Sql not executing, insert into syntax error

    Hi Joseph sir,

    Thank you for your update,

    However I m not able to see any difference in between my code & your code,

    I m getting error at .execute (Synatx Error Insert into statement)

    I m worried is my Sql Statement is wrong ? or is there any problem while executing the sql statement.

    I m very much confused, highly appreciable if you can help me on the same.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Sql not executing, insert into syntax error

    your sql
    "INSERT INTO SAMI_Data (CSTName, EmailSub,) Values ('" & SAMI_Tracking.Cst_Nme.Value & "', '" & SAMI_Tracking.Email_Sub.Value & "',')

    my sql
    "INSERT INTO SAMI_Data (CSTName, EmailSub) Values ('" & SAMI_Tracking.Cst_Nme.Value & "', '" & SAMI_Tracking.Email_Sub.Value & "')

  5. #5
    Registered User
    Join Date
    07-04-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    38

    Re: Sql not executing, insert into syntax error

    Hi Joseph sir,

    Thank you for your update,

    However I m not able to see any difference in between my code & your code,

    I m getting error at .execute (Synatx Error Insert into statement)

    I m worried is my Sql Statement is wrong ? or is there any problem while executing the sql statement.

    I m very much confused, highly appreciable if you can help me on the same.

  6. #6
    Registered User
    Join Date
    07-04-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    38

    Re: Sql not executing, insert into syntax error

    Thank you so much sir, its working ... it was just an small mistake I made and could not recognize,

    However also can you help me with the any code help, where I can view the same data on the userform listbox.

    It would be much appreciated. thanks in advance.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Sql not executing, insert into syntax error

    what data? your code doesn't retrieve any data

  8. #8
    Registered User
    Join Date
    07-04-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    38

    Re: Sql not executing, insert into syntax error

    Hi Sir,

    This is the code I have added to retrieve data from access to userform listbox
    Where I am getting the file could not be found, However my Path is correct.

    Private Sub Access_to_database()
    'Microsoft ActiveX Data Objects 2.8 Library-Activate
    On Error GoTo UserForm_Initialize_Err
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.recordSet
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source = E:\SAMI.accdb"


    rst.Open "SELECT (CSTName),(EmailSub) FROM SAMI_Data ORDER BY [EmailSub];", _
    cnn, adOpenStatic

    rst.MoveFirst
    With Summary.Listbox1
    .Clear
    Do
    .AddItem rst![CSTName]
    rst.MoveNext
    Loop Until rst.EOF
    End With
    UserForm_Initialize_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
    UserForm_Initialize_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume UserForm_Initialize_Exit
    End Sub

    would need your prestigious help to resolve the error.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Sql not executing, insert into syntax error

    please add code tags to your post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1