Results 1 to 10 of 10
  1. #1
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70

    Calcualtion on a table field

    I have a Access table with fields named MM1, MM2, MM3...MM17.
    I need to add 1 to the current value stored in the field, depending on the result of some calculations done on several variables.
    The code I currently use, was proposed by ChatGPT, but it does not work. Code as follows:

    Code:
            Case 9
                Me!Frm_TyreForecastSubForm.Form!("MM0_" & Me("MM" & t) - Val(MinMM)) = Me!Frm_TyreForecastSubForm.Form!("MM0_" & Me("MM" & t) - Val(MinMM)) + 1
                If t = 1 Then FieldName = "MM" & t
                CurrentDb.Execute "UPDATE TempMMtable SET " & FieldName & " = " & FieldName & " + 1 WHERE CLRegistration = '" & rs!CLRegistration & "';"
    In the above scenario, the result is not important, but the field "MM1" must be changed by 1. The SQL statement should read as follows (just for clarity sake):


    "UPDATE TempMMtable SET MM1 = MM1 +1;" - Because t was equal to 1, field MM1 needs to be updated. If t = 2, then field MM2 must be updated with 1.

    Any suggestions?
    Thanks

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    Put your criteria/SQL into a string variable and debug.print that until you get it correct.
    You can also post the output of the debug back here if you cannot see the error.

    Basic debugging 101.

    Also check your control values are what you think they are.
    I would even do the calculation of the control name separately so I can check I have the correct name.

    Not impressed with ChatGPT coding so far.

    Code:
    debug.print "MM0_" & Me("MM" & t) - Val(MinMM)) + 1
    
    Also it appears your DB is not normalised. Having digits after fieldnames gives that away, especially that high a number.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70
    Thanks for the suggestions. I narrowed it down and am receiving the following error now:

    Click image for larger version. 

Name:	Screenshot 2024-04-22 132850.png 
Views:	24 
Size:	7.9 KB 
ID:	51718

    The field is not auto filled and no restrictions on the field.

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,417
    Here's a bare bones solution that updates the table based on value of t without knowing full context of problem. Maybe you can adapt it to fit.

    DCDS-davegri-v01.zip

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,118
    What kind of table is it? Regular Access local table, linked Access, linked Excel, linked ODBC?
    Can you post a screen shot of the table in design view showing the properties window for field MM1?
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,811
    Look for one of these causes for the error message
    http://allenbrowne.com/ser-61.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    DC CS is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2023
    Location
    Vaalpark, South Africa
    Posts
    70
    The table is an Access Linked table. Property sheet below:

    Click image for larger version. 

Name:	Table Field MM1.png 
Views:	17 
Size:	31.7 KB 
ID:	51720

    Click image for larger version. 

Name:	Table Field MM1 Properties.png 
Views:	17 
Size:	19.1 KB 
ID:	51721

  8. #8
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    167
    Quote Originally Posted by DC CS View Post
    The table is an Access Linked table. Property sheet below:

    Click image for larger version. 

Name:	Table Field MM1.png 
Views:	17 
Size:	31.7 KB 
ID:	51720

    Click image for larger version. 

Name:	Table Field MM1 Properties.png 
Views:	17 
Size:	19.1 KB 
ID:	51721
    What's MM[n] mean? Seems like this should be a child table, because this looks an awful lot like a repeating field.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    You do not have the field names correct?
    You have MyMMn in the table and are using MMn in the code?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,444
    According to your code, if t<>1 then fieldname is not determined

    If t = 1 Then FieldName = "MM" & t

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

Similar Threads

  1. Replies: 7
    Last Post: 11-08-2021, 10:01 AM
  2. Replies: 4
    Last Post: 05-17-2018, 10:17 AM
  3. Replies: 4
    Last Post: 11-15-2015, 10:15 AM
  4. Replies: 1
    Last Post: 08-27-2014, 04:16 PM
  5. Replies: 5
    Last Post: 11-01-2012, 09:26 AM

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