Access add column by form data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ferndi Kiletus
    New Member
    • Feb 2012
    • 4

    Access add column by form data

    Hi!

    I am struggling with a piece of code. I am new to VBA and i want this code to create a new coloumn in a table with a name that is from a text box on a form (same form as the save button) on button press. But I am not exactly sure how to refer to the value...

    Can somebody help me please?

    Code:
    Private Sub savebutton1_Click()
    mSaved = True
    CurrentDb.Execute ("ALTER TABLE table1 ADD COLUMN Forms![Add employee]![Id].Value Text;")
    MsgBox "Saved!"
    End Sub
    For a bonus question, how can I check if all the fields are correctly filled, and only then make the changes and saving process?

    Any help appriciated...
  • migi48
    New Member
    • Feb 2012
    • 28

    #2
    Hi,

    Have you tried putting the value of the textbox in a string first before you execute your sql code?

    something like this:

    Code:
    Private Sub savebutton1_Click() 
    mSaved = True
    Dim newColName As String 
    Set newColName = Me.txtNewColName.Value
    CurrentDb.Execute ("ALTER TABLE table1 ADD COLUMN " & newColName & "TEXT(50)") 
    MsgBox "Saved!" 
    End Sub
    Not really sure though. Just trying to help. :)

    Comment

    • Ferndi Kiletus
      New Member
      • Feb 2012
      • 4

      #3
      I altered my code like:


      Code:
      Private Sub savebutton1_Click()
        Dim newColName As String
        Set newColName = Me.ID.Value
        CurrentDb.Execute ("ALTER TABLE table1 ADD COLUMN" & newColName & "Text;")
        mSaved = True
        MsgBox "Saved!"
      End Sub
      But it doesnt work, it sais: Compile error, object required, abd highlights Set newColName part.
      Last edited by TheSmileyCoder; Feb 8 '12, 08:55 AM.

      Comment

      • migi48
        New Member
        • Feb 2012
        • 28

        #4
        Hmmmm.

        try this instead:

        Code:
        currentdb.Execute ("ALTER TABLE table1 ADD COLUMN('" & me.ID.Value& "') Text;")

        Comment

        • Ferndi Kiletus
          New Member
          • Feb 2012
          • 4

          #5
          I tried, and there is a syntax error in it

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            I dont know if it can be done in the way you have suggested. I am however fairly sure it can be done if the string is parsed first.
            A thing to note however is that this will likely only work if noone is using the table at the same time, and you should probably have error handling to inform the user in case it fails.

            Example below:
            Code:
            Private Sub savebutton1_Click() 
            On Error goto err_Handler
             'First check for valid input
             If IsNull(Me.ID) then
              Msgbox "Nothing was entered as field name"
              Exit Sub
             End If
            
             Dim strSQL as String
             strSQL="ALTER TABLE tablazat ADD COLUMN [" & Me.Id & "] Text;") 
             CurrentDb.Execute strSQL,dbFailOnError
             
             mSaved=true
             MsgBox "Saved"
            
             Exit Sub
            
            Err_Handler:
              Msgbox "An error occured while trying to add new column." & vbnewline & _
                     "See below for details on error:" & vbnewline & vbnewline & _
                     "[" & err.number & "]" & vbnewline & err.Description
            End Sub

            Comment

            • TheSmileyCoder
              Recognized Expert Moderator Top Contributor
              • Dec 2009
              • 2322

              #7
              The reason that
              Code:
              Set newColName = Me.txtNewColName.Value
              fails, is that only objects needs to use the SET command.
              The correct syntax would be:
              Code:
              newColName = Me.txtNewColName
              The .Value is optional, since .Value is the default parameter for textboxes.

              Comment

              • Ferndi Kiletus
                New Member
                • Feb 2012
                • 4

                #8
                It worked, thank you so much...

                Comment

                • migi48
                  New Member
                  • Feb 2012
                  • 28

                  #9
                  Haha. Good job. :)

                  Like I said, I'm also a newbie in VBA and just trying to help. Sorry for my syntax errors. :D Thanks to TheSmileyCoder for also teaching me. :)

                  Comment

                  Working...