Alter Table - VB Approach

G

Guest

Hey Guys,

I have an SQL Alter Table statment which removes two columns from a table.

However, the name of the table changes daily (the date is added onto the
end). Is there anyway of incorporating a Variable for the name within the SQL
Code?

I can't seem to find away of achieving this so far. Here's the code which
obviously doesn't work at the moment, due to the variable being used as the
table name.

Function AmendTodaysNameChanges()

Dim Table As String

TableNAME = "Name Changes Report" & Format(Date - 1, "dd-mm-yyyy")

Dim strSQLRemoveColumns As String
DoCmd.SetWarnings False
strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"
DoCmd.RunSQL strSQLRemoveColumns
DoCmd.SetWarnings True

End Function
 
G

Guest

Change:

strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"

to:

strSQLRemoveColumns = "Alter table " & TableNAME & " Drop Column
id,adt,dateid;"

Good luck.

BW
 
G

Guest

Sorry, I forget the []'s ! - use:

strSQLRemoveColumns = "Alter table [" & TableNAME & "] Drop Column
id,adt,dateid;"

BeWyched said:
Change:

strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"

to:

strSQLRemoveColumns = "Alter table " & TableNAME & " Drop Column
id,adt,dateid;"

Good luck.

BW

Whelan said:
Hey Guys,

I have an SQL Alter Table statment which removes two columns from a table.

However, the name of the table changes daily (the date is added onto the
end). Is there anyway of incorporating a Variable for the name within the SQL
Code?

I can't seem to find away of achieving this so far. Here's the code which
obviously doesn't work at the moment, due to the variable being used as the
table name.

Function AmendTodaysNameChanges()

Dim Table As String

TableNAME = "Name Changes Report" & Format(Date - 1, "dd-mm-yyyy")

Dim strSQLRemoveColumns As String
DoCmd.SetWarnings False
strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"
DoCmd.RunSQL strSQLRemoveColumns
DoCmd.SetWarnings True

End Function
 
G

Guest

BeWyched, that is ruddy brilliant.

Thank you very much!!

Tom

BeWyched said:
Sorry, I forget the []'s ! - use:

strSQLRemoveColumns = "Alter table [" & TableNAME & "] Drop Column
id,adt,dateid;"

BeWyched said:
Change:

strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"

to:

strSQLRemoveColumns = "Alter table " & TableNAME & " Drop Column
id,adt,dateid;"

Good luck.

BW

Whelan said:
Hey Guys,

I have an SQL Alter Table statment which removes two columns from a table.

However, the name of the table changes daily (the date is added onto the
end). Is there anyway of incorporating a Variable for the name within the SQL
Code?

I can't seem to find away of achieving this so far. Here's the code which
obviously doesn't work at the moment, due to the variable being used as the
table name.

Function AmendTodaysNameChanges()

Dim Table As String

TableNAME = "Name Changes Report" & Format(Date - 1, "dd-mm-yyyy")

Dim strSQLRemoveColumns As String
DoCmd.SetWarnings False
strSQLRemoveColumns = "Alter table [tablename] Drop Column
id,adt,dateid;"
DoCmd.RunSQL strSQLRemoveColumns
DoCmd.SetWarnings True

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top