Insert Into query using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omozali
    New Member
    • Nov 2006
    • 36

    Insert Into query using VBA

    Can someone please tell me why INSERT INTO generates syntax error

    Private Sub Command7_Click( )
    Dim strUSER As String
    strUSER = Nz(DLookup("[user]", "table2", "[user] ='" & username.Value & "'"), "nouser")
    If strUSER = "nouser" Then
    INSERT INTO table2;
    VALUES (username.Value ,password.Value );
    MsgBox "User added"
    Else
    MsgBox "user already exist"
    End If
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Try to execute the query like:

    If strUSER = "nouser" Then
    currentdb.execu te ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;
    MsgBox "User added"
    else

    VBA isn't capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execu te.
    The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

    Nic;o)

    Comment

    • omozali
      New Member
      • Nov 2006
      • 36

      #3
      Originally posted by nico5038
      Try to execute the query like:

      If strUSER = "nouser" Then
      currentdb.execu te ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;
      MsgBox "User added"
      else

      VBA isn't capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execu te.
      The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

      Nic;o)
      Unfortunately the code generated syntax error :(

      Comment

      • omozali
        New Member
        • Nov 2006
        • 36

        #4
        Originally posted by omozali
        Unfortunately the code generated syntax error :(

        I tried this

        CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES (username.value ,password.value )")

        generates: too few parameters, expect 2

        I also tried
        CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ('username.valu e','password.va lue')")

        Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

        3rd attempt

        CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

        would generate error at the first " ' "

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Originally posted by omozali
          Unfortunately the code generated syntax error :(
          Try this slightly fixed version :
          Code:
          currentdb.execute ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;"
          It's exactly the same except Nico left off the last double-quote (easily done).

          Comment

          • omozali
            New Member
            • Nov 2006
            • 36

            #6
            Originally posted by omozali
            I tried this

            CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES (username.value ,password.value )")

            generates: too few parameters, expect 2

            I also tried
            CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ('username.valu e','password.va lue')")

            Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

            3rd attempt

            CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

            would generate error at the first " ' "
            an update for those interested , this code works

            Private Sub Command7_Click( )
            Dim strUSER As String
            Dim UN As String
            Dim PW As String
            UN = username.Value
            PW = password.Value
            strUSER = Nz(DLookup("[user]", "table2", "[user] ='" & username.Value & "'"), "nouser")
            If strUSER = "nouser" Then
            Rem CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass])" & _
            "VALUES (UN,PW)")
            DoCmd.RunSQL ("INSERT INTO table2 ([user], [pass]) VALUES (username.value ,password.value )")

            No idea why the previous one didn't


            ACCESS VB is very tricky language to learn and it make no sense to me why microsoft made so many variations for commands that would only confuse us

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by omozali
              I tried this

              CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES (username.value ,password.value )")

              generates: too few parameters, expect 2

              I also tried
              CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ('username.valu e','password.va lue')")

              Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

              3rd attempt

              CurrentDb.Execu te ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

              would generate error at the first " ' "
              Let us know if Nico's (fixed) version works first.
              If not then we can revisit it, but I suspect all will be fine.

              Comment

              • omozali
                New Member
                • Nov 2006
                • 36

                #8
                one small thing when the code run a small window would appear saying
                you are about to a append 1 row and asks for confirmation

                any way to remove this annoying window?

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Originally posted by omozali
                  one small thing when the code run a small window would appear saying
                  you are about to a append 1 row and asks for confirmation

                  any way to remove this annoying window?
                  Code:
                  DoCmd.SetWarnings(False)
                  and after all finished
                  DoCmd.SetWarnings(True)
                  Can I take it then that Nico's code worked fine for you?

                  Comment

                  • omozali
                    New Member
                    • Nov 2006
                    • 36

                    #10
                    Originally posted by NeoPa
                    Code:
                    DoCmd.SetWarnings(False)
                    and after all finished
                    DoCmd.SetWarnings(True)
                    Can I take it then that Nico's code worked fine for you?

                    unfortunately, it didn't

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      Originally posted by omozali
                      unfortunately, it didn't
                      In that case - are you happy that you have a working answer?
                      Or are you looking for more help sorting that out?

                      Comment

                      • nico5038
                        Recognized Expert Specialist
                        • Nov 2006
                        • 3080

                        #12
                        Please start with creating the INSERT query in the query editor till it's working.
                        Than start with transforming that SQL into the string needed.

                        Nic;o)

                        Comment

                        • omozali
                          New Member
                          • Nov 2006
                          • 36

                          #13
                          Originally posted by NeoPa
                          In that case - are you happy that you have a working answer?
                          Or are you looking for more help sorting that out?

                          Sorry for the late responce. my situation is ok now thanks

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            Originally posted by omozali
                            Sorry for the late responce. my situation is ok now thanks
                            Do you still have the warning come up (We can help to remove the warning)?
                            Or are you happy with everything?

                            Comment

                            • PEB
                              Recognized Expert Top Contributor
                              • Aug 2006
                              • 1418

                              #15
                              Originally posted by omozali
                              Can someone please tell me why INSERT INTO generates syntax error

                              INSERT INTO table2;
                              VALUES (username.Value ,password.Value );
                              MsgBox "User added"
                              End If
                              Can't use SQL just like this in VBA! Use docmd.runsql YourSQL

                              Comment

                              Working...