Error Inserting String Value Into Table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

    Error Inserting String Value Into Table

    I'm trying to execute this line of code:

    Code:
    cn.Execute "INSERT INTO StdDegReqCourse VALUES (" & lngClassId & ", """ & strStdDegReqCourse & """, " _
    & "1, Null," & lngIncludeDegGPA & ", " & lngTransDetailId & ", 0, 0" & lngCourseSortOrder & ")", , adExecuteNoRecords
    However, I've found that if the variable strStdDegReqCou rse is THE* I get the error: Invalid Column name 'THE*'. I did not have this problem when working in DAO. How do I fix it?
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Try to specify the column names first like:

    Code:
    cn.Execute "INSERT INTO StdDegReqCourse (ClassId, StdDegReqCourse, num1, Empty, IncludeDegGPA, TransDetailId, num2, num3, CourseSortOrder) VALUES (" & lngClassId & ", """ & strStdDegReqCourse & """, " _
    & "1, Null," & lngIncludeDegGPA & ", " & lngTransDetailId & ", 0, 0" & lngCourseSortOrder & ")", , adExecuteNoRecords
    Nic;o)

    Comment

    • bullfrog83
      New Member
      • Apr 2010
      • 124

      #3
      Originally posted by nico5038
      Try to specify the column names first like:

      Code:
      cn.Execute "INSERT INTO StdDegReqCourse (ClassId, StdDegReqCourse, num1, Empty, IncludeDegGPA, TransDetailId, num2, num3, CourseSortOrder) VALUES (" & lngClassId & ", """ & strStdDegReqCourse & """, " _
      & "1, Null," & lngIncludeDegGPA & ", " & lngTransDetailId & ", 0, 0" & lngCourseSortOrder & ")", , adExecuteNoRecords
      Nic;o)
      That didn't work.

      Comment

      • improvcornartist
        Recognized Expert Contributor
        • May 2007
        • 303

        #4
        Maybe wrap the column names in []?

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          Besides specifying the column list the only difference I see is that I use surrounding "(" and ")" after the .execute

          Another option might be to have the string printed in the immediate window and executed under MS SQL to check the error message from the database engine.

          Nic;o)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32661

            #6
            Originally posted by bullfrog83
            bullfrog83: That didn't work.
            What might be helpful is posting the actual SQL executed that doesn't work. We can probably work it out with some effort, but that's no substitute for having the correct data to work with.

            Comment

            • bullfrog83
              New Member
              • Apr 2010
              • 124

              #7
              Originally posted by NeoPa
              What might be helpful is posting the actual SQL executed that doesn't work. We can probably work it out with some effort, but that's no substitute for having the correct data to work with.
              It occurred to me over the weekend that since I'm working in ADO it'd probably be best to write a sproc that does the inserting and execute it in vba. I did that and it works.

              Comment

              • Jim Doherty
                Recognized Expert Contributor
                • Aug 2007
                • 897

                #8
                Originally posted by bullfrog83
                It occurred to me over the weekend that since I'm working in ADO it'd probably be best to write a sproc that does the inserting and execute it in vba. I did that and it works.
                Not only is it a better idea you are keeping it server side and centralised should you so wish to edit the thing in future. Most CRUD operations should be done using that methodology if you are working unbound using an ADP
                Last edited by Jim Doherty; Jul 12 '10, 02:35 PM.

                Comment

                Working...