+ Reply to Thread
Results 1 to 7 of 7

Userform with Command Button to Load and Execute from a Textfile

  1. #1
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Userform with Command Button to Load and Execute from a Textfile

    Hi - I am fairly new to VBA (about 1 year) but have a project on the go and have encountered a problem: Here is what I have right now:

    1. Load a UserForm with a variety of command buttons one of which has an Exit function and one a Load function (amongst others)
    2. The Exit button is used to terminate a "session" (not close the workbook) and to empty a procedure which contains variable settings read from a .txt file
    3. The Load button reads a .txt file and pastes into a proc shell that exists within the same module as the UserForm. Not sure if this is necessary but thats where it is right now!
    4. Within the Load command button code I have a call (after the proc shell has been populated...so I now have a valid proc) to execute this proc....in the hope that this will initilise these variables as it supposed to. However it seems to ignore this "CALL" and only executes it when I am performing an unload which (I read somewhere else) can appear like an Initialize event if something is "not loaded previously" (which sounds a bit vague to me).

    So the question is:

    1. How do I get the Load button to "Load" and then execute a successful Call to the newly populated proc?

    Alternatively,

    2. is there a better way to populate a proc from a text file and then execute the code in that proc from a single command button click?

    (I do not currently want to read the variables by doing a line-by line search through the text-file (as I do elsewhere in my project)...for now I want to simply load the proc and then execute it with one button....)

    Thanks in advance for any help
    Last edited by ThermalTD; 04-17-2013 at 04:58 AM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,219

    Re: Userform with Command Button to Load and Execute from a Textfile

    What are you actually trying to do? the process sounds pretty offensive it's not usually a good idea to edit code with code and it's generally an absolute no-no if you are looking to distribute the workbook.

    There must be a better way ....

  3. #3
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Userform with Command Button to Load and Execute from a Textfile

    Thanks for such a quick response....Yes probably offensive...I like to make things complicated but its supposed to make the operation easy....(1 button to load and execute).....ok...let me try again...

    I have a userform which is effectively a control panel with Load, Save, Exit buttons etc....

    I'd like to have an external model textfile initialise some variables within the code. I figured the best way to do this was to load in the code by means of a Load button but which executes code to read in the text file file...place it in a proc (in ready to execute format).... then re-initialize the user-form by including a call at the end of the load to the proc which then unloads and loads the userform...however..this still doesnt work. The only way that call statement (within the initialize event) seems to work is if I exit the userform completely then re-start is manually through the proc that starts it all off...rather than through the call at the end of the load sequence...does that make sense??? Probably not....but this is where I am so far.....ok if I should use code to edit code I am definitely off-track...lol...

    Am I better off just setting the variables directly by reading the text file line by line and extracting the data that way?
    Last edited by ThermalTD; 04-17-2013 at 05:18 AM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,219

    Re: Userform with Command Button to Load and Execute from a Textfile

    You don't really want to import text as code. You'd be better parsing the textfile, or using a database

  5. #5
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Userform with Command Button to Load and Execute from a Textfile

    Fair enough! I think I get the idea that my approach is too complicated. But I guess its prone to ingestion (and corruption) of code by a bad data file. However....I dont think the database approach is what I want either....the input textfile will be in a format that I will create from a seperate tool by the user. That tool will ensure the format is correct and readable....I guess by parsing the textfile you mean extracting the variable values....then thats the way I'll go....thanks very much for your help

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,219

    Re: Userform with Command Button to Load and Execute from a Textfile

    That tool will ensure the format is correct and readable
    hmmm, in what way doesn't a database do this?

    Ah well, glad you're sorted The reason you don't want to write code with code is that it requires users to lower their security settings

  7. #7
    Registered User
    Join Date
    04-17-2013
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Userform with Command Button to Load and Execute from a Textfile

    I understand what you are saying about users. I have spent 25 years as an engineer working and thinking like an engineer and only one year working as a "developer".

    As far as database is concerned maybe I am making some limiting assumptions. I might incorporate a database later for a variety of other aspects to my project...but I dont immediately see how a database would be the right solution here (perhaps I am missing something). During a build session the user will use some tools to build a model...the output of which will be stored in a text file...do you think this would be better stored in a database?? The only thing I am thinking is that the database will use up memory while the textfile doesnt????

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1