Use an Excel VBA Variable for a field name in an DAO SQL statement for AccessAs you can see below in the code the  &quot; &amp; tblName &amp; &quot;  will work fine but instead of using the <strong>BankName </strong>(which the user cannot change because it is in the code) I would like to do the same  as I did with the Table name and use a variable that gets its name from an excel spreadsheet rather that an actual name hard coded in a VBA module. I would like the variable field name entered in the Excel Spreadsheet by the user just like the table name is filled in by the user. <br/><br/>The problem is getting the ADO SQL string ( <em>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>BankName </strong>Char(25)&quot; </em> ) to recognize that the value in the Variable is the field name.<br/><br/>Sub AddFieldToAccess()<br/>    <br/>Dim cnn As ADODB.Connection<br/>Dim cmd As ADODB.Command<br/>Dim tblName  as string     'User to input Table Name in cell C2<br/>Dim fldName  as string     'User to input Field Name in cell C3<br/><br/>tblName = ActiveSheet.Range(&quot;C2&quot;).Value        'Table Name<br/>fldName = ActiveSheet.Range(&quot;C3&quot;).Value        'Field Name<br/><br/>'Open Access Database<br/>Set cnn = New ADODB.Connection<br/>    With cnn<br/>        .Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot;<br/>        .Open MyConn<br/>    End With<br/>    <br/>    Set cmd = New ADODB.Command<br/>    Set cmd.ActiveConnection = cnn<br/>    'create table<br/><br/>  <br/><strong>'I have already tried to use the same approach as the table Variable and no work <br/></strong><br/>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>&quot; &amp; fldName &amp; &quot;</strong> Char(25)&quot;<br/>    cmd.Execute , , adCmdText<br/><strong><br/><br/>'If I do this I end up with a field named BankName</strong><br/><br/>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>BankName </strong>Char(25)&quot;<br/>    cmd.Execute , , adCmdText<br/><br/><br/>    Set cmd = Nothing<br/>    Set cnn = Nothing<br/>End Sub<br/>© 2009 Microsoft Corporation. All rights reserved.Thu, 25 Jun 2009 04:56:27 Zc4941363-8384-43f1-a6ce-e1e0db95ffa0http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/c4941363-8384-43f1-a6ce-e1e0db95ffa0#c4941363-8384-43f1-a6ce-e1e0db95ffa0http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/c4941363-8384-43f1-a6ce-e1e0db95ffa0#c4941363-8384-43f1-a6ce-e1e0db95ffa0JohnTReedhttp://social.msdn.microsoft.com/Profile/en-US/?user=JohnTReedUse an Excel VBA Variable for a field name in an DAO SQL statement for AccessAs you can see below in the code the  &quot; &amp; tblName &amp; &quot;  will work fine but instead of using the <strong>BankName </strong>(which the user cannot change because it is in the code) I would like to do the same  as I did with the Table name and use a variable that gets its name from an excel spreadsheet rather that an actual name hard coded in a VBA module. I would like the variable field name entered in the Excel Spreadsheet by the user just like the table name is filled in by the user. <br/><br/>The problem is getting the ADO SQL string ( <em>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>BankName </strong>Char(25)&quot; </em> ) to recognize that the value in the Variable is the field name.<br/><br/>Sub AddFieldToAccess()<br/>    <br/>Dim cnn As ADODB.Connection<br/>Dim cmd As ADODB.Command<br/>Dim tblName  as string     'User to input Table Name in cell C2<br/>Dim fldName  as string     'User to input Field Name in cell C3<br/><br/>tblName = ActiveSheet.Range(&quot;C2&quot;).Value        'Table Name<br/>fldName = ActiveSheet.Range(&quot;C3&quot;).Value        'Field Name<br/><br/>'Open Access Database<br/>Set cnn = New ADODB.Connection<br/>    With cnn<br/>        .Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot;<br/>        .Open MyConn<br/>    End With<br/>    <br/>    Set cmd = New ADODB.Command<br/>    Set cmd.ActiveConnection = cnn<br/>    'create table<br/><br/>  <br/><strong>'I have already tried to use the same approach as the table Variable and no work <br/></strong><br/>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>&quot; &amp; fldName &amp; &quot;</strong> Char(25)&quot;<br/>    cmd.Execute , , adCmdText<br/><strong><br/><br/>'If I do this I end up with a field named BankName</strong><br/><br/>cmd.CommandText = &quot;ALTER TABLE &quot; &amp; tblName &amp; &quot; Add Column <strong>BankName </strong>Char(25)&quot;<br/>    cmd.Execute , , adCmdText<br/><br/><br/>    Set cmd = Nothing<br/>    Set cnn = Nothing<br/>End Sub<br/>Tue, 23 Jun 2009 21:45:32 Z2009-06-23T21:45:32Zhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/c4941363-8384-43f1-a6ce-e1e0db95ffa0#96cfa620-d256-48f2-b7aa-aa586cd177b5http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/c4941363-8384-43f1-a6ce-e1e0db95ffa0#96cfa620-d256-48f2-b7aa-aa586cd177b5Kapil Muni Guptahttp://social.msdn.microsoft.com/Profile/en-US/?user=Kapil%20Muni%20GuptaUse an Excel VBA Variable for a field name in an DAO SQL statement for AccessHi,<br/><br/>This forum is for SQL Data Services, a Microsoft's Cloud database service offering. You have to raise this in  a proper forum from forum home: <a href="http://social.msdn.microsoft.com/Forums/en-US/categories"><span style="color:#0033cc">http://social.msdn.microsoft.com/Forums/en-US/categories</span></a> <p>Thanks<br/>Kapil</p>Wed, 24 Jun 2009 07:20:21 Z2009-06-24T07:20:21Z