+ Reply to Thread
Results 1 to 15 of 15

InputBox Function not as flexible as needed

  1. #1
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    InputBox Function not as flexible as needed

    I'm using a simple inputbox function to query a number of variables from the workbook user, to be used in a new object created from a class module.

    I'd like to name the new object a unique concatenation of the variables being stored within it to archive in a library for tracking.

    Currently, my functions (seven in all) are written as such:
    Please Login or Register  to view this content.
    I run into trouble because everytime I need to reference the returned variable in my primary macro I call a new InputBox (the function). I will also need to error check these user inputs in the function to ensure they're the appropriate datatype, thus it makes sense to return a different variable name from the function.

    How can I write my functions to call the inputbox function (function within a function) in such a way that a different variable name is returned? Please bear in mind the function should allow for IF statements to qualify the user input before being returned to the sub.

    I've included the workbook for reference. The primary macro is named "AddNMP", and the sub "InputBoxNewMarginPlacement" will ultimately be inserted / called into / by "AddNMP." The code is a bit messier than usual because I've been trying to test the concatenation, but those lines have been remarked - feel free to ignore.
    Last edited by dystopianprotagonist; 03-04-2013 at 10:59 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: InputBox Function not as flexible as needed

    What exactly are you trying to concatenate?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    In principle I'd like to assign the Function's return for string variables candfirstname, candlastname, and clientname to concatenate in a new string to be used for naming the object it will be contained in.

    This should make it easier to search object storage in a separate data library. Ultimately, multiple workbooks will share the data library as a work around to some issues I was having with standard password protection (i.e. master workbooks requiring the password of every feeder workbook).

    In practice I've been able to do this, but in doing so called the function a second time producing a duplicate inputbox. My thought is that by distinguishing the function's name from the return variable, I can avoid doing this.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: InputBox Function not as flexible as needed

    How are the input boxes being duplicated?

    What is it you want to change about them?

  5. #5
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    I've tried to put it into context below.

    Function candfirstname
    Function calls InputBox function to query user for candidate's first name and stores in string candfirstname
    Please Login or Register  to view this content.
    Sub InputBoxNewMarginPlacement
    Calls Class CTempCandidate to create object for storage of user input in an array of strings, singles and integers. The Functions are called for user input, then passed to a class to create a new object. Hit Ctrl+F and search for [ISSUE] to see the two specific areas that call the function and explain the duplicate InputBoxes. The solution I had in mind involved adding a RETURN to the Function and differentiating the Function name and the returned variable. Thoughts on how to change the Function to achieve this?

    Please Login or Register  to view this content.
    Class CTempPlacement (for reference)
    Please Login or Register  to view this content.
    Last edited by dystopianprotagonist; 02-25-2013 at 11:43 PM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Do you actually need the variables?

    Can't you assign directly to the object from the functions?

  7. #7
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    I think I'd need the arguments as variables since ill be using them twice (once to store the value on the object, once to concatenate the name of the object.

    Do you know an alternative to type the InputBox function besides the one I've used below?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: InputBox Function not as flexible as needed

    What wll you be concatentating the name of the object to?

    To me it sounds like you want to do that for some sort of dynamic variable name.

    By the way, this is what I was meaning about not needing the variables.
    Please Login or Register  to view this content.
    This part of the code is taking the return values of the functions and assigning them to the new candidate.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    I was able to concatenate a new variable name by rearranging the code in the InputBoxNewMarginPlacement Sub. I've referenced the sFirstName, sLastName, sClientName variables to define a new string then attempted to define my new object with:

    Please Login or Register  to view this content.
    But now I have a Runtime error 438 when I try to compile. I'm reasonably sure I'm running into this issue as I'm declaring a new object (using a class as template), then attempting to substitute a concatenated string in as its value. I'm certain VB has the ability to have dynamic object names... I'm just not sure how to do it in the above situation.

    Any help is appreciated.


    Please Login or Register  to view this content.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: InputBox Function not as flexible as needed

    I would recommend a more direct approach, there's no need to call a separate function for each variable, and it appears things are getting confusing by trying. Just collect the information directly in the main macro:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InputBox Function not as flexible as needed

    I would also recommend you don't use
    Please Login or Register  to view this content.
    just use
    Please Login or Register  to view this content.
    and then set to a New instance as you are doing. that may also help show you why you are getting a 438 error-you're trying to assign a string to an object
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  12. #12
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    JosephP,

    Appreciate the recommendation. In my research on collection classes, I uncovered that I can assign the concatenated string as the key when using the add method on collection classes. I'll fiddle around with the language a bit later tonight and update the thread accordingly.

    What I need to do now is strengthen the typing around my input box functions so that there's less of an opportunity for the end user to enter inaccurate details (i.e. entering a string instead of a float / double, or a float / double instead of a string.)

    I'm thinking an IF ELSE statement in each function can get me there. Any suggestions?

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InputBox Function not as flexible as needed

    your class properties should validate the input data but using application.inputbox as Jerry demonstrated gives you the option to specify a type unlike the vba.inputbox function

  14. #14
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    I was under the impression that the inputbox method (not the function) was not available in Excel 2003 / VB 6.5?

  15. #15
    Registered User
    Join Date
    02-16-2013
    Location
    New York
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: InputBox Function not as flexible as needed

    Ok I updated with Joe and Jerry's suggestions and it's a bit more streamlined - thanks for the tips.

    Next I was to replace some of the inputboxes with combo boxes (need radio buttons on one, and drop downs on another). Any recommendations on getting started are appreciated. MSDN seems to have a lot of references, but I still haven't found what I need.

+ 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