ALTER TABLE with local variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ineedahelp
    New Member
    • Sep 2006
    • 98

    ALTER TABLE with local variable

    Can anyone help me with the code for an ALTER TABLE commmand? I have a list box that lists all tables in database. The user chooses. I next want to add a column to this table. I might also want to delete a column from this table. what I have for code so far is:

    Private Sub lstTableList_Af terUpdate()
    Dim tblClientTable As String
    tblClientTable = Forms!frmDailyC lient!lstTableL ist
    Debug.Print tblClientTable
    mAnswer = MsgBox("Add a field for LS Data?", vbYesNo)
    If mAnswer = 6 Then
    ALTER TABLE " & tblClientTable & " ADD COLUMN LSRate Long;"
    End If
    Me!lstFieldList .Visible = True
    Me!lstFieldList .SetFocus
    Me!lstTableList .Visible = False
    End Sub

    I am guessing that it doesn't like my variable. Is it because I have dim it as a string? Do I need to set is as "currentdb" ? Thanks again for any help!!
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    Originally posted by ineedahelp
    If mAnswer = 6 Then
    ALTER TABLE " & tblClientTable & " ADD COLUMN LSRate Long;"
    End If
    The Dim is OK but you can't execute SQL code as if it were VB as in line quoted above.

    Comment

    • ineedahelp
      New Member
      • Sep 2006
      • 98

      #3
      Can you tell me what I need to do to make this work? Thank you in advance!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        Not really - I don't have much experience amending table definitions via SQL.
        I can say that to execute SQL code you can use :-
        Code:
        DoCmd.RunSQL(SQLStatement:=???, UseTransaction:=???)
        I only ever use direct SQL for SELECT, UNION and Pass-Through queries.

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          You have to create a TableDef variable

          Code:
           
          Private Sub lstTableList_AfterUpdate()
          Dim db As Database 
          Dim tbl As TableDef 
          Dim fld As Field 
          Dim tblClientTable As String
           
          	tblClientTable = Forms!frmDailyClient!lstTableList
          	Debug.Print tblClientTable
          	mAnswer = MsgBox("Add a field for LS Data?", vbYesNo)
          	If mAnswer = 6 Then
           
          		' Start by opening the database 
          		Set db = CurrentDb() 
          		' Create a tabledef object 
          		Set tbl = db.TableDefs(tblClientTable)
           
          		' Create field; set its properties; add it to the tabledef 
          		Set fld = tbl.CreateField("LSRate", dbLong) 
          		tbl.Fields.Append fld 
           
          	End If
           
          	Me!lstFieldList.Visible = True
          	Me!lstFieldList.SetFocus
          	Me!lstTableList.Visible = False
           
          	fld.close
          	set fld = nothing
          	tbl.close
          	set tbl = nothing
          	set db = nothing
           
          End Sub

          Comment

          Working...