Results 1 to 12 of 12

Thread: check success of sql insert query

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    check success of sql insert query

    VB Code:
    1. If (cn.State = adStateOpen) Then
    2.                     Dim Str As String
    3.                     Str = "INSERT INTO " & SQLTable & "(Username, Password) VALUES(" & Text1.Text & "," & Text2.Text & ")"
    4.                     adoRS.Open str, cn
    5.                 End If

    how can i tell if the insert was successful?
    and does the inset syntax look correct to you guys? i havent tested it yet

    edit**********
    tested it and it throws an 'sql sntax' error

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: check success of sql insert query

    Str = "INSERT INTO " & SQLTable & "(Username, Password) VALUES('" & Text1.Text & "','" & Text2.Text & "')"


    those are strings....

    so it needed the ' '
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: check success of sql insert query

    Well, first off, you need to encapsulate string entries in single quotes
    VB Code:
    1. VALUES('" & Text1.Text & "','" & Text2.Text & "')"
    Also, you OPEN a recordset that is created using a SELECT. For action queries, you use the Execute method of the connection object.
    VB Code:
    1. cn.Execute str

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: check success of sql insert query

    thanks, its still throwing the error

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: check success of sql insert query

    Quote Originally Posted by Pouncer
    thanks, its still throwing the error
    1. Post what you currently have.

    2. What error is it throwing?

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: check success of sql insert query

    Quote Originally Posted by Hack
    Well, first off, you need to encapsulate string entries in single quotes
    VB Code:
    1. VALUES('" & Text1.Text & "','" & Text2.Text & "')"
    Also, you OPEN a recordset that is created using a SELECT. For action queries, you use the Execute method of the connection object.
    VB Code:
    1. cn.Execute str

    oh yeah... execute lol


    Pouncer.. whats the error and what line is it stopping at?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: check success of sql insert query

    Password is a keyword. Place square brackets around field names that are also keywords.

    & "(Username, [Password]) VALUES(" &

    Also, add a space between the table name and the list of columns

    Str = "INSERT INTO " & SQLTable & " (Username

    [Edit]
    Sorry, I just ran a quick test. The space is not required...
    Last edited by brucevde; May 30th, 2006 at 01:23 PM.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: check success of sql insert query

    Quote Originally Posted by brucevde
    Password is a keyword. Place square brackets around field names that are also keywords.
    I completely forgot about that. I always think in terms of PWD = whatever.....but, he is right....the word Password is a keyword.
    Quote Originally Posted by brucevde
    Sorry, I just ran a quick test. The space is not required...
    I have run into problems with VALUES clauses and spaces, or more accurately, the lack of a space, so I think it is always a good idea to put a space between the table name and the list of columns.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: check success of sql insert query

    ok thansk guys i got it to work..

    VB Code:
    1. Str = "INSERT INTO testuser (Username, Password) VALUES('" & Text1.Text & "','" & Text2.Text & "')"
    2.                     cn.Execute Str

    but how can i make it msgbox or something after the execute was successful

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: check success of sql insert query

    VB Code:
    1. Str = "SELECT COUNT("username") FROM " & SQLTable & " WHERE username = '" & Text1.Text & "',"
    2. adoRS.Open str, cn
    3.  
    4. Msgbox adoRS.Fields.Item("username").Value
    If the message box is blank, your INSERT failed.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: check success of sql insert query

    nice one Hack.

  12. #12
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: check success of sql insert query

    Another option

    VB Code:
    1. Dim lngRowsAffected As Long
    2.  
    3. Str = "INSERT INTO testuser (Username, Password) VALUES('" & Text1.Text & "','" & Text2.Text & "')"
    4. cn.Execute Str, lngRowsAffected
    5.  
    6. If lngRowsAffected > 0 Then
    7.    msgbox "Insert Successful"
    8. End if

Posting Permissions

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



Click Here to Expand Forum to Full Width