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

    Hard Coding

    Hello,

    I wanted to get a second opinion on hardcoding on forms.

    On a biweekly basis, I regularly input data into my DB. All fields have the same info, except for a few. For the fields that share info, I hardcode in VBA as following:


    I typically copy and paste info from a website. Then I use the following code to automatically pull the data I want.
    .txtDate = Mid(.txtInfo, InStr(.txtInfo, ";") - 10, 10)
    .txtCity = Left(.txtCity, (InStr(.txtCity, ",") - 1))


    I also use this code to input set data as soon as I click on btnAutoFillJPN
    Me.txtActivity.Value = "New"
    Me.txtCountry.Value = "Japan"
    etc.
    etc.


    I also have this:
    If Me.txtCity.Value = "Cinncinnati" then Me.txtCity.Value = "CIN"


    As a rough estimate, I would say I have about 60 lines of code more or less set up like:

    Me.txtFieldName.Value = "xxx"


    and
    IF Me.txtFieldName.Value = "ccc" then Me.txtFieldName.Value = "bbb"



    I want to know if this is okay. I litterally only use this form to input data on a biweekly basis. No one else uses it. And I have it set very particular to automate EVERYTHING that I will do. I am concerned that there may be some unforeseen consequences such as:
    1. The code on this form would somehow damage the code on all my other forms
    2. My VBA for the whole DB becomes corrupted

  2. #2
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    This is a cen...
    Last edited by zashaikh; 10-30-2017 at 01:52 PM.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why would you be concerned that this code would cause corruption any more than any other code?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I agree with June --Why would you suspect this code may cause corruption?

    Do you have backup(s)?

    You don't have to have values in properCase to do compares in Access --you could if case is critical.

  5. #5
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by orange View Post
    I agree with June --Why would you suspect this code may cause corruption?

    Do you have backup(s)?

    You don't have to have values in properCase to do compares in Access --you could if case is critical.

    Yes. I have backups. But I am just paranoid. So many people tell me to "never hardcode in VBA". And ive never gotten a straight reason why

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    If you step back from the "hard coded" approach for a moment, there might be a way to make what you are working with into a function. Maybe not, but if you do something on a frequent basis, it may be an option to "parameter-ize" your set up. If things don't change from session to session then hard coding might be the proper solution.

    There is an old saying --if it ain't broke, don't fix it.

    Said differently, if what you have works, then don't change it. If it's too slow, or cumbersome, or can only be executed by you, then you could look at options to improve that issue.

    Good luck.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Because hard coding is less flexible than dynamically pulling data from tables. If you someday add a value "DDD", will have to modify code and if multiple users, redistribute database (frontend only if split) whereas just adding a new record into table is simpler.

    Why need to correct spelling of Cincinnati in code, and only Cincinnati?

    Do you really want a space in front of Alpha and Blue? Because your code is saving those values with a preceding space. Access table will drop the space at the end.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank you guys. I appreciate the input. Puts me at ease.

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

Similar Threads

  1. Replies: 4
    Last Post: 03-31-2017, 08:35 AM
  2. Really - just how hard is this to code???
    By DubCap01 in forum Programming
    Replies: 11
    Last Post: 02-07-2017, 02:32 PM
  3. Why is it so hard to explain VBA?
    By DigitalAdrenaline in forum Access
    Replies: 1
    Last Post: 08-20-2016, 06:04 AM
  4. is this possible & is this hard to do Please help
    By Debbie_P in forum Database Design
    Replies: 4
    Last Post: 02-10-2015, 08:21 AM
  5. Hard to explain how to do this?
    By flemingjo in forum Forms
    Replies: 1
    Last Post: 08-29-2014, 06:03 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