PC Review


Reply
Thread Tools Rate Thread

Programmatically adding fields to table based on structure and content of a particular record

 
 
rwfreeman
Guest
Posts: n/a
 
      26th May 2007
Access 2033. Code below evaluates the .Fields of an ADO table and
adds a field as needed (the first two fields are already in the table,
other fields will contain data associated with those fields but there
is no a priori way to know the number of additional fields for any
particular record). All records would have the same number of
additional filelds but only some of those records would have data in
all fields. Structure for the table would eventually have fields
PlanYear, SetAside (the two required fields), Claim1, Claim2, ... ,
ClaimN.

What I''d really like to do is go to a particular record in the table,
see which of the fields higher than Field2 contain data, and then add
data to the first unoccupied field or add a new one, if needed.

If anyone's following what I want to do, could you suggest some VBA
code that would make my desires happen?

BTW if a field has to be added it would be added in another procedure
(the user has some decision points before the field would need to be
added).

Thanks in advance for any suggestions.

Richard Freeman
Public Sub GetClaimNumberandFieldName()
On Error GoTo Err_GetClaimNumberandFieldName

Dim i As Integer
Dim cnCAFEPlan As ADODB.Connection
Dim rsPlanYear As ADODB.Recordset
Dim adoField As ADODB.Field 'strDateOfClaimFieldName and
strClaimIdentifier are DIMmed in the Declaration section of the form's
module

Set cnCAFEPlan = CurrentProject.AccessConnection
Set rsPlanYear = New ADODB.Recordset
rsPlanYear.Open "tblPlanYear", cnCAFEPlan

i = 0
If rsPlanYear.Fields.Count = 2 Then 'tblPlanYear reflects no claims
have been made
strDateOfClaimFieldName = "Claim0" & i + 1
strClaimIdentifier = Forms!frmMain.Form.cboxPlanYear & "Claim" & i
Else
For Each adoField In rsPlanYear
i = i + 1
Next adoField 'count the number of fields
i = i - 2
strClaimIdentifier = Forms!frmMain.Form.cboxPlanYear & "Claim" & i
strDateOfClaimFieldName = "Claim" & IIf(i < 12, "0", "") & i
End If

Exit_GetClaimNumberandFieldName:
rsPlanYear.Close
Set rsPlanYear = Nothing
Exit Sub

Err_GetClaimNumberandFieldName:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_GetClaimNumberandFieldName

End Sub

 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      26th May 2007
On 25 May 2007 18:48:51 -0700, rwfreeman <(E-Mail Removed)> wrote:

>Structure for the table would eventually have fields
>PlanYear, SetAside (the two required fields), Claim1, Claim2, ... ,
>ClaimN.


Then the structure of your table WOULD BE WRONG.

You're using a relational database... *use it relationally*! "Fields are
expensive, records are cheap". You have a one (plan) to many (claims)
relationship; model it as a one to many relationship, with TWO tables. Each
Claim would be in a new record in the claims table, and that table would have
a foreign key linking it to your Plan table.


John W. Vinson [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a Structure to a List(Of T) in another Structure JerryB Microsoft Dot NET 0 14th May 2011 04:10 PM
update multiple fields in a record based on a record in another table bertil.hedenstrom@gmail.com Microsoft Access Queries 3 19th Sep 2006 11:49 AM
Modifying table structure programmatically Alexei Adadurov Microsoft ADO .NET 2 22nd Feb 2005 06:46 PM
copy fields from one table to the field structure of another table Educo Gent Microsoft Access ADP SQL Server 2 10th Feb 2005 06:29 PM
Creating a table in Access based on structure of existing table Microsoft C# .NET 1 20th Dec 2003 04:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 AM.