How do I INSERT INTO a table a variable value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonHuff
    New Member
    • Sep 2010
    • 14

    How do I INSERT INTO a table a variable value

    Here is my code. I am trying to INSERT INTO "Master_Tab le" field "Edited_By" the variable value of "Editor"

    "Editor" is the User Name of the person logged onto that computer

    Code

    Code:
    Option Compare Database
    Option Explicit
    
          Declare Function WNetGetUser Lib "mpr.dll" _
          Alias "WNetGetUserA" (ByVal lpName As String, _
          ByVal Editor As String, lpnLength As Long) As Long
    
          Const NoError = 0 
    
    Sub GetUserName()
    
         Const lpnLength As Integer = 255
    
          Dim status As Integer
          Dim lpName, Editor As String
    
           Editor = Space$(lpnLength + 1) 
           status = WNetGetUser(lpName, Editor, lpnLength)
    
          If status = NoError Then
             Editor = Left$(Editor, InStr(Editor, Chr(0)) -1)
    
             CurrentDb.Execute "INSERT INTO Master_Table " &_
                  "(Edited_By) " & _
                     "VALUES ('" & Editor & "');"
    
          Else
    
             MsgBox "Unable to get the name."
             End
    
          End If
    
    End Sub
    Last edited by NeoPa; Sep 15 '10, 01:25 PM. Reason: Please use the [code] tags provided
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    And? You haven't left us with a question. Nor have you explained what happens and when with the code.

    I'm sure we can help you, but we need somewhere to start.

    Comment

    • JonHuff
      New Member
      • Sep 2010
      • 14

      #3
      response

      The question was "How do I INSERT INTO a table a variable value". My code is not working.

      the code provided showed I was trying to INSERT INTO table "Master_Tab le" field "Edited_By" the value of the variable "Editor". I dont know how else to say it.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        Well, the obvious place to start would be with my prompting question.

        What line does the code fail on?
        What error message do you get if any?
        If it fails on line #23 as I suspect then what is the actual SQL string passed to the execute command?

        Comment

        • pod
          Contributor
          • Sep 2007
          • 298

          #5
          What is this lonely "End" doing on line #30?

          Comment

          • JonHuff
            New Member
            • Sep 2010
            • 14

            #6
            reply

            I figured it out. I was trying to execute the code while my form was open. The form is based off the table I was trying to write to (DUH !!!). I changed it to write to a new test table - it works fine now. Thanks for looking anyways.

            Comment

            Working...