Excel VBA creating an ADO field_name variable<p>As 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/><br/></p>© 2009 Microsoft Corporation. All rights reserved.Tue, 23 Jun 2009 21:46:48 Z985c127d-6594-472d-b620-e809419db2e4http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/985c127d-6594-472d-b620-e809419db2e4#985c127d-6594-472d-b620-e809419db2e4http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/985c127d-6594-472d-b620-e809419db2e4#985c127d-6594-472d-b620-e809419db2e4JohnTReedhttp://social.msdn.microsoft.com/Profile/en-US/?user=JohnTReedExcel VBA creating an ADO field_name variable<p>As 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/><br/></p>Tue, 23 Jun 2009 19:08:53 Z2009-06-23T21:32:43Zhttp://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/985c127d-6594-472d-b620-e809419db2e4#533a8b19-31d0-49a4-9678-14deac313677http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/985c127d-6594-472d-b620-e809419db2e4#533a8b19-31d0-49a4-9678-14deac313677Aaron Stanley Kinghttp://social.msdn.microsoft.com/Profile/en-US/?user=Aaron%20Stanley%20KingExcel VBA creating an ADO field_name variableThis forum is for SQL Data Services.  You will have a better chance of getting a fast answer in one of the forums for SQL Server, VB, or Excel.<hr class="sig">Aaron Stanley King - Microsoft Consultant - Perpetual Technologies, Inc. - http://www.perptech.com Tue, 23 Jun 2009 20:59:44 Z2009-06-23T20:59:44Z