Results 1 to 5 of 5
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to populate fields in table with VBA?

    Hi all - I have a query where I have too many iif statements where I run into the max statements allowed and have to continue in a second/third column that ends in calling the results from the 1st two columns. I would like an example of a vba statement that looks at several fields in a table and if that condition is met to return specific text in an empty column that table has.

    Ex. if column A, B, C in table "My_Data" all equal ST2500 then I would like a column result to be "Prior 2010"
    if column A and B = TQ81 and column C = 152 then the column should populate with 2015 and so on and so forth.



    I just need an example to get me started.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Usually you don't "populate" a table's field with a calculation as you can quickly loose data integrity if one of the three columns changes. To display the value that you would store in the table you use a form or a query. In both the result would be calculated "on the fly" by a custom VBA function in which you would use nested Select or If statements to get your value.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    I forgot to mention that this table is updated daily so the values are expected to change - i found an example that appears to be what I need.

    Code:
    Option Compare Database
    
    Sub CreateCalculatedField()
            Dim dbs As DAO.Database
            Dim tdf As DAO.TableDef
            Dim fld As DAO.Field2
            
            ' get the database
            Set dbs = CurrentDb()
            
            ' create the table
            Set tdf = dbs.TableDefs("My_Data")
              
            ' create the calculated field: full name
            Set fld = tdf.CreateField("Test_Column", dbText, 20)
            fld.Expression = "[FirstName] & "" "" & [LastName]"
            tdf.Fields.Append fld
            
            ' append the table and cleanup
            dbs.TableDefs.Append tdf
            
    Cleanup:
            Set fld = Nothing
            Set tdf = Nothing
            Set dbs = Nothing
        End Sub
    The next guidance I need is how could I start writing my if statement in the fld.Expression section? I just need to see an example and I feel that I can complete this task. Note that I will prob. have 15-18 iif statements.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    That code doesn't do anything near to what your initial request is. In the original post you mention a query (a update one) that will update an existing field in a table based on three other fields in the same record. The code you show will add a new calculated field to a (new) table every time it is run.
    Here is some info about calculated fields in tables and why you should avoid them:http://allenbrowne.com/casu-14.html

    To solve your problem you would simply write a public VBA function in a standard module that returns your calculation and use that in a calculated field in a query:
    Code:
     
    Public Function fnCalculateField(sField1 as string,sField2 as String, sField3 as String) as String
    
    
    Select Case sField1
    Case "ST2500"
         Select Case sField2
         Case "ST2500"
             Select Case sField3
             Case "ST2500"
                  fnCalculateField="Prior 2010"
             Case.....
              ...........
              End Select
         Case "TQ81"
             Select Case sField3
             Case .......
                  fnCalculateField=.......
             Case.....
                   .............
             End Select
    	End Select
    Case "TQ81"
    	Select Case sField2
         Case "ST2500"
             Select Case sField3
             Case "ST2500"
                  fnCalculateField=.....
             Case.....
              ...........
              End Select
         Case "TQ81"
             Select Case sField3
             Case "152"
                  fnCalculateField="2015"
             Case.....
                   .............
             End Select
    	End Select
    ........................
    End Select
    And in the query you would add a field CalcField:fnCalculateField([A],[B],[C])

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,537
    I agree with the advice given by Gicu that you should NOT usually store calculated fields. Ignore it at your peril.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Auto populate fields from another table
    By m3zzr in forum Forms
    Replies: 2
    Last Post: 12-20-2019, 09:22 AM
  2. Replies: 6
    Last Post: 10-02-2019, 02:21 PM
  3. Replies: 8
    Last Post: 04-16-2018, 05:49 PM
  4. Replies: 4
    Last Post: 05-15-2016, 03:26 PM
  5. Replies: 1
    Last Post: 06-11-2012, 02:34 PM

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