473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Referring to recordset field named via string

Here's the deal. I have a table that gets a column added to it using a SQL alter table statement. The new column gets its name from a string. How do I refer to the new field so I can update it? As written, I get an 'Item not found in Collection' error. I'd appreciate any help. Thanks.

Code:

Sub Foo()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strResponse As String

Set dbs = CurrentDb

strResponse = InputBox("Name for new column:")

strSQL = "ALTER TABLE tblTest ADD [" & strResponse & "] TEXT(50)"

DoCmd.RunSQL strSQL

Set rst = dbs.OpenRecordset("tblTest", dbOpenDynaset)

rst!Fields(strResponse) = "Text"

rst.Close

End Sub
Nov 4 '07 #1
3 3062
Jim Doherty
897 Expert 512MB
Here's the deal. I have a table that gets a column added to it using a SQL alter table statement. The new column gets its name from a string. How do I refer to the new field so I can update it? As written, I get an 'Item not found in Collection' error. I'd appreciate any help. Thanks.

Code:

Sub Foo()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strResponse As String

Set dbs = CurrentDb

strResponse = InputBox("Name for new column:")

strSQL = "ALTER TABLE tblTest ADD [" & strResponse & "] TEXT(50)"

DoCmd.RunSQL strSQL

Set rst = dbs.OpenRecordset("tblTest", dbOpenDynaset)

rst!Fields(strResponse) = "Text"

rst.Close

End Sub
Rick,

You need to include column

Use Code tags please (its the hash button when you post a message)

Expand|Select|Wrap|Line Numbers
  1.  
  2. ALTER TABLE tblTest ADD COLUMN [" & strResponse & "] TEXT(50)
  3.  
then when you open the recordset to save any edits as you navigate its this:

Expand|Select|Wrap|Line Numbers
  1.  rst.update
to navigate your rows its this
Expand|Select|Wrap|Line Numbers
  1.  rst.MoveFirst 
  2. rst.MoveNext
  3. rst.MovePrevious
  4. rst.MoveLast
  5.  
to add a new record its

Expand|Select|Wrap|Line Numbers
  1.  rst.AddNew

Jim :)
Nov 4 '07 #2
All good points, but the SQL statement did work, with or without the COLUMN. The problem is when I refer to the new column named by the string, it still comes out as a string, not a column name.

I'll remember the hash button next time. Thanks.
Nov 4 '07 #3
Modified code. This works. Now I just have to modify it to insert all the values:

Expand|Select|Wrap|Line Numbers
  1. Sub Foo()
  2.  
  3. Dim dbs As DAO.Database
  4. Dim rst As DAO.Recordset
  5. Dim strSQL As String
  6. Dim strResponse As String
  7.  
  8. Set dbs = CurrentDb
  9.  
  10. strResponse = InputBox("Name for new column:")
  11.  
  12. strSQL = "ALTER TABLE tblTest ADD COLUMN [" & strResponse & "] TEXT(50)"
  13.  
  14. DoCmd.RunSQL strSQL
  15.  
  16. Set rst = dbs.OpenRecordset("tblTest", dbOpenDynaset)
  17.  
  18. With rst
  19.     .Edit
  20.     rst(strResponse) = "Text"
  21.     .Update
  22. End With
  23.  
  24. rst.Close
  25.  
  26. End Sub
  27.  
Nov 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
5
by: Simone | last post by:
Hello I hope you guys can help me. I am very new to ADO... I am creating a ADODB connection in a module and trying to access it from a command button in a form. Function fxEIDAssgn(plngEID As...
7
by: Peter Bailey | last post by:
I have a querystring built up in vba and I want to open a recordset based on the sql and pass the date value to a textbox or label for use elsewhere on the form. The recordset isnt working as it...
3
by: shumaker | last post by:
This code from the subform works for getting the value of a field of the main form named "WorkSheet": MsgBox Form_WorkSheet.Recordset.Fields("Clerk").Value Each record in the mainform datasheet...
4
by: MLH | last post by:
Somehow, I seem to be able to refer to combo-box controls in 2 distinctly different ways. A combo-box on an Access 97 form named TagCountyChooserBox seems can be referenced these ways... ...
2
by: ey.markov | last post by:
Greetings, in A2K VBA, I set the following recordset: Set rsGPValue = dbs.OpenRecordset("SELECT *, DateSerial(Year(),Month()+4,0) FROM tblGPValue AS OurDate, dbOpenSnapshot) and then I try...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
4
by: Kev | last post by:
Hello, I have an Access 2003 database running on an XP network. I have a datasheet subform containing a 28 day roster - shift1 to shift28. Each record has 1 RosterEmpID, 1 EmployeeNumber, 28...
2
by: wallconor | last post by:
Hi, I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.