Results 1 to 5 of 5
  1. #1
    hockeyman9474 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2013
    Location
    Manalapan, NJ
    Posts
    45

    .CreateField Mehtod used with a Variable

    I have an application that allows a user to select a month from a combo box and on the change event will delete a temp table and recreate it with information according to what the user selected. So i.e. the user selects June, temp_Table is dropped and recreated with columns 1-6(1 for Jan, 2 for Feb etc.)

    Also in the on change event I have code that includes a counter that counts the columns/fields to know how many months exist. Next a loop runs adding the missing months as fields to the table using the current value of the counter. So if June is selected, the counter should be at 6 and the loop knows to run until it reaches 13 (the first column is not a month). With each iteration in the loop I am using the code below. My question is can I, use a variable to name a field. For example, cnt is my variable. I have scoured the internet all day looking for answer but alas have not been successful in finding one.


    Set db = CurrentDb
    Set tdf = db.TableDefs("temp_Table")


    Do Until cnt = 13




    tdf.Fields.Append tdf.CreateField(cnt, dbInteger, 5)
    cnt = cnt + 1

    Loop

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,478
    Not clear on your issue but usually you can put a variable in parentheses to get its name?

    For Each fld In rs1.Fields
    z = rs1(fld.Name).Name
    y = rs1(fld.Name)

  3. #3
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    For starters, starting a field name with a number is a very bad idea. You could add a prefix such as "A_" & cnt....

    Did you try running this routine? That is the best way to find out if it works or not.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    hockeyman,

    Do you have a clear description of what you are trying to do in business terms --plain English?
    Seems you have a method (how to do something) that isn't quite working. It would be better for readers
    if we had a clear requirement. There may be many ways to do what you need.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have to agree with orange. Right now what you have told us seems bad on many levels.

    - Constantly deleting and creating tables will lead to dB bloat at the least and dB corruption at the worst.
    - What you describe is a spreadsheet design, not a database design.
    - The month names (Jan, Feb,...) you want to use as field names are actually data. Again, this is a spreadsheet design.


    No one told me about "committing spreadsheet" , so it took me many years to understand the concept and correct my thinking about table designs.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  2. Object variable or With block variable not defined
    By PorscheMan in forum Programming
    Replies: 3
    Last Post: 01-16-2013, 01:53 PM
  3. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  4. Replies: 4
    Last Post: 08-05-2010, 01:26 PM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums