Results 1 to 10 of 10
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    Is hardcoding data a good idea?

    So I am thinking about hardcoding data so that if I type in a certain phrase into a field, my form will autopopulate. To do this, I would enter:



    IF txtFood = apple Then
    txtType = Fruit
    txtWeight = 0.5kg
    txtColor = red
    End If.

    That way if I type in "Apple" to the txtfood field in my form, Fruit, 0.5kg, and Red will auto populate the remaining fields.

    I was going to copy this code for another 20 food products.

    I know there are more efficient ways to do this... but this is the easiest for me. Is this going to cause problems? Glitches? Data being overwritten?

    thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    use a table.
    apple, fruit, .5kg, red
    ...

    then you can use a query to fill in the fields. NEVER hardcode.
    and add other items.

  3. #3
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    I am trying to figure out how to use a table to do this, but I am not having any luck

  4. #4
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    How would I use a query to fill in the fields?

  5. #5
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    One way would be open a recordset using a query. Then set the text boxes as the recordset values for (e.g.) apple


    Sent from my iPhone using Tapatalk

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Or if there was only one row with apple you could use

    Dlookup function


    Sent from my iPhone using Tapatalk

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    The general question here:

    Is hardcoding data a good idea?

    often comes up concerning how to populate Comboboxes/Listboxes and the same applies to your specific question/situation.

    If the data will never, ever, ever change the possible values for txtFood...i.e. you'll never add another kind of fruit, never edit a given fruit's txtType, txtWeight or txtColor, and you'll never decide you need to add another descriptive component (say 'genetically modified' or 'non-genetically modified') then sure, hard code it.

    But if there's any chance at all of having to do this, down the line, then take the Table approach as already suggested. Then if you have to do change, for example, apple's txtColor to green, you simple change it in the underlying Table, rather than having to go back into your code and change the hard-wiring.

    Experience has told veteran developers that, unless the only possible values are never, ever going to change (i.e. the value for "Has employee previously worked here?" can only be 'Yes' or 'No') you shouldn't hard wire data.

    Who knew, thirty years ago, that the question "Gender" could be answered with anything but 'male' or 'female?' Who knew, once more, thirty years ago that 'Type of telephone' could be 'Landline,' 'Cell', or 'Satellite' instead of simply 'Landline?'

    When in doubt don't hard wire!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by andy49 View Post
    Or if there was only one row with apple you could use

    Dlookup function
    Yes!!! "Apple" only appears in one row in the the table. Can I make my autofill my form after update using Dlookup?

  9. #9
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by Missinglinq View Post
    The general question here:

    Is hardcoding data a good idea?

    often comes up concerning how to populate Comboboxes/Listboxes and the same applies to your specific question/situation.

    If the data will never, ever, ever change the possible values for txtFood...i.e. you'll never add another kind of fruit, never edit a given fruit's txtType, txtWeight or txtColor, and you'll never decide you need to add another descriptive component (say 'genetically modified' or 'non-genetically modified') then sure, hard code it.

    But if there's any chance at all of having to do this, down the line, then take the Table approach as already suggested. Then if you have to do change, for example, apple's txtColor to green, you simple change it in the underlying Table, rather than having to go back into your code and change the hard-wiring.

    Experience has told veteran developers that, unless the only possible values are never, ever going to change (i.e. the value for "Has employee previously worked here?" can only be 'Yes' or 'No') you shouldn't hard wire data.

    Who knew, thirty years ago, that the question "Gender" could be answered with anything but 'male' or 'female?' Who knew, once more, thirty years ago that 'Type of telephone' could be 'Landline,' 'Cell', or 'Satellite' instead of simply 'Landline?'

    When in doubt don't hard wire!

    Linq ;0)>
    Yes. The data I have will never change. Thank you so much!!!

  10. #10
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    But in terms of using a table to do this...

    I want to tblAutoFill to autofill data onto frmFood. I want all the information in frmFood to be saved in a separate table called tblFood.

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

Similar Threads

  1. Replies: 3
    Last Post: 02-21-2017, 05:15 AM
  2. Replies: 11
    Last Post: 06-02-2013, 10:42 PM
  3. Replies: 7
    Last Post: 01-30-2013, 09:47 AM
  4. Replies: 1
    Last Post: 08-03-2012, 12:32 PM
  5. Can I use Access for this Idea?
    By sandiego5 in forum Access
    Replies: 5
    Last Post: 11-17-2011, 09:14 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