+ Reply to Thread
Results 1 to 15 of 15

InputBox Function not as flexible as needed

Hybrid View

  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:
    Function candfirstname() As String
    
    Dim Prompt As String
    Dim Title As String
    'Declares Prompt and Title as string variables
    
    Prompt = "Please enter the candidate's first name"
    Title = "Candidate First Name"
    'Assigns string values to Prompt and Title for display in the InputBox
    
    candfirstname = InputBox(Prompt, Title)
    'Calls InputBox Application
    
    End Function
    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,644

    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.

    ' InputBoxNewMarginPlacement Macro
    ' Macro calls multiple inputboxes to assign values for loading into NewCandProfile object,_
    ' created from CTempCandidate Class
    
    
    Sub InputBoxNewMarginPlacement()
    
    'Calls Class CTempCandidate to create New Object comprised of candidate variables in_
    'String, Double and Integer Types
    
    Dim NewCandProfile As New CTempCandidate
    'Declares new Object named NewCandProfile from Class CTempCandidate
    
    'Dim ConcatenationProof As String
    'Dim objectfirstname As String
    'objectfirstname = candfirstname
    'Dim objectlastname As String
    'objectlastname = candlastname
    'Dim objectclientname As String
    'objectclientname = clientname
    'Delares a new variable as a string for naming of NewCandProfile object
    'to test concatenation in MsgBox at end of Sub
    
    Dim sFirstName As String
    Dim sLastName As String
    Dim sClientName As String
    Dim sPayRate As Double
    Dim sBillRate As Double
    Dim sEmployStatus As Double
    Dim sSplitPercent As Integer
    'Declares variables in primary macro to receive variables passed from InputBox_
    'these variables are then passed to object NewCandProfile
    
    sFirstName = candfirstname
    sLastName = candlastname
    sClientName = clientname
    sPayRate = candpayrate
    sBillRate = candbillrate
    sEmployStatus = employmentstatus
    sSplitPercent = splitpercentage
    'InputBox series runs and passes variables into primary macro
    
    'ConcatenationProof = objectfirstname & objectlastname & objectclientname
    
    Set NewCandProfile = New CTempCandidate
     NewCandProfile.sFirstName = sFirstName
     NewCandProfile.sLastName = sLastName
     NewCandProfile.sClientName = sClientName
     NewCandProfile.sCandPayRate = sPayRate
     NewCandProfile.sCandBillRate = sBillRate
     NewCandProfile.sEmployStatus = sEmployStatus
     NewCandProfile.sSplitPercent = sSplitPercent
    'Creates an object named NewCandProfile from the class CTempCandidate then sets values in new object_
    'as those returned from InputBox series
    
    'MsgBox ConcatenationProof
    'MsgBox "Candidate First Name: " & NewCandProfile.sFirstName & vbCrLf & "Candidate Last Name: " & NewCandProfile.sLastName & vbCrLf & "Client Name: " & NewCandProfile.sClientName & vbCrLf & "Candidate Pay Rate: " & NewCandProfile.sCandPayRate & vbCrLf & "Candidate Bill Rate: " & NewCandProfile.sCandBillRate & vbCrLf & "Employment Status: " & NewCandProfile.sEmployStatus & vbCrLf & "Referral Split: " & NewCandProfile.sSplitPercent
    'Calls a MsgBox to display results from class
    
    End Sub

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

    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
    ' candfirstname Function
    ' Function calls an InputBox function to record candfirstname as a string value
    
    Function candfirstname() As String
    
    Dim Prompt As String
    Dim Title As String
    'Declares Prompt and Title as string variables
    
    Prompt = "Please enter the candidate's first name"
    Title = "Candidate First Name"
    'Assigns string values to Prompt and Title for display in the InputBox
    
    candfirstname = InputBox(Prompt, Title)
    'Calls InputBox Application
    
    End Function
    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?

    ' InputBoxNewMarginPlacement Macro
    ' Macro calls multiple inputboxes to assign values for loading into NewCandProfile object,_
    ' created from CTempCandidate Class
    
    
    Sub InputBoxNewMarginPlacement()
    
    'Calls Class CTempCandidate to create New Object comprised of candidate variables in_
    'String, Double and Integer Types
    
    Dim NewCandProfile As New CTempCandidate
    'Declares new Object named NewCandProfile from Class CTempCandidate
    
    'Dim ConcatenationProof As String
    [ISSUE]'Dim objectfirstname As String
    'objectfirstname = candfirstname[ISSUE]
    'Dim objectlastname As String
    'objectlastname = candlastname
    'Dim objectclientname As String
    'objectclientname = clientname
    'Delares a new variable as a string for naming of NewCandProfile object
    'to test concatenation in MsgBox at end of Sub
    
    Dim sFirstName As String
    Dim sLastName As String
    Dim sClientName As String
    Dim sPayRate As Double
    Dim sBillRate As Double
    Dim sEmployStatus As Double
    Dim sSplitPercent As Integer
    'Declares variables in primary macro to receive variables passed from InputBox_
    'these variables are then passed to object NewCandProfile
    
    [ISSUE]sFirstName = candfirstname[ISSUE]
    sLastName = candlastname
    sClientName = clientname
    sPayRate = candpayrate
    sBillRate = candbillrate
    sEmployStatus = employmentstatus
    sSplitPercent = splitpercentage
    'InputBox series runs and passes variables into primary macro
    
    'ConcatenationProof = objectfirstname & objectlastname & objectclientname
    
    Set NewCandProfile = New CTempCandidate
     NewCandProfile.sFirstName = sFirstName
     NewCandProfile.sLastName = sLastName
     NewCandProfile.sClientName = sClientName
     NewCandProfile.sCandPayRate = sPayRate
     NewCandProfile.sCandBillRate = sBillRate
     NewCandProfile.sEmployStatus = sEmployStatus
     NewCandProfile.sSplitPercent = sSplitPercent
    'Creates an object named NewCandProfile from the class CTempCandidate then sets values in new object_
    'as those returned from InputBox series
    
    'MsgBox ConcatenationProof
    'MsgBox "Candidate First Name: " & NewCandProfile.sFirstName & vbCrLf & "Candidate Last Name: " & NewCandProfile.sLastName & vbCrLf & "Client Name: " & NewCandProfile.sClientName & vbCrLf & "Candidate Pay Rate: " & NewCandProfile.sCandPayRate & vbCrLf & "Candidate Bill Rate: " & NewCandProfile.sCandBillRate & vbCrLf & "Employment Status: " & NewCandProfile.sEmployStatus & vbCrLf & "Referral Split: " & NewCandProfile.sSplitPercent
    'Calls a MsgBox to display results from class
    
    End Sub
    Class CTempPlacement (for reference)
    'Class designed to create a NewCandidateProfile
    
    Private cCandFirstName As String
    Private cCandLastName As String
    Private cClientName As String
    Private cCandPayRate As Double
    Private cCandBillRate As Double
    Private cEmployStatus As Double
    Private cSplitPercent As Integer
    '#######################################################################
    'Creates an exchange path for input and output of Candidate's First Name
    Public Property Get sFirstName() As String
        sFirstName = cCandFirstName
    End Property
    Public Property Let sFirstName(Value As String)
        cCandFirstName = Value
    End Property
    '###########################################################################
    'Creates an exchange path for input and output of the Candidate's Last Name
    Public Property Get sLastName() As String
        sLastName = cCandLastName
    End Property
    Public Property Let sLastName(Value As String)
        cCandLastName = Value
    End Property
    '#########################################################################
    'Creates an exchange path for input and output of Client Organization Name
    Public Property Get sClientName() As String
        sClientName = cClientName
    End Property
        Public Property Let sClientName(Value As String)
            cClientName = Value
        End Property
    '#####################################################################
    'Creates an exchange path for input and output of Candidate's Pay Rate
    Public Property Get sCandPayRate() As Double
        sCandPayRate = cCandPayRate
    End Property
    Public Property Let sCandPayRate(Value As Double)
        cCandPayRate = Value
    End Property
    '######################################################################
    'Creates an exchange path for input and output of Candidate's Bill Rate
    Public Property Get sCandBillRate() As Double
        sCandBillRate = cCandBillRate
    End Property
    Public Property Let sCandBillRate(Value As Double)
        cCandBillRate = Value
    End Property
    '############################################################################
    'Creates an exchange path for input and output of Candidate Employment Status
    Public Property Get sEmployStatus() As Double
        sEmployStatus = cEmployStatus
    End Property
    Public Property Let sEmployStatus(Value As Double)
        cEmployStatus = Value
    End Property
    '##########################################################################
    'Creates an exchange path for input and output of Referral Split Percentage
    Public Property Get sSplitPercent() As Integer
        sSplitPercent = cSplitPercent
    End Property
    Public Property Let sSplitPercent(Value As Integer)
        cSplitPercent = Value
    End Property
    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,644
    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,644

    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.
    Sub InputBoxNewMarginPlacement()
    'InputBox series runs and passes variables into primary macro
    
    'ConcatenationProof = objectfirstname & objectlastname & objectclientname
    
        Set NewCandProfile = New CTempCandidate
        NewCandProfile.sFirstName = candfirstname
        NewCandProfile.sLastName = candlastname
        NewCandProfile.sClientName = clientname
        NewCandProfile.sCandPayRate = candpayrate
        NewCandProfile.sCandBillRate = candbillrate
        NewCandProfile.sEmployStatus = employmentstatus
        NewCandProfile.sSplitPercent = splitpercentage
        'Creates an object named NewCandProfile from the class CTempCandidate then sets values in new object_
        'as those returned from InputBox series
    
        'MsgBox ConcatenationProof
        'MsgBox "Candidate First Name: " & NewCandProfile.sFirstName & vbCrLf & "Candidate Last Name: " & NewCandProfile.sLastName & vbCrLf & "Client Name: " & NewCandProfile.sClientName & vbCrLf & "Candidate Pay Rate: " & NewCandProfile.sCandPayRate & vbCrLf & "Candidate Bill Rate: " & NewCandProfile.sCandBillRate & vbCrLf & "Employment Status: " & NewCandProfile.sEmployStatus & vbCrLf & "Referral Split: " & NewCandProfile.sSplitPercent
        'Calls a MsgBox to display results from class
    
    End Sub
    This part of the code is taking the return values of the functions and assigning them to the new candidate.
    NewCandProfile.sFirstName = candfirstname
        NewCandProfile.sLastName = candlastname
        NewCandProfile.sClientName = clientname
        NewCandProfile.sCandPayRate = candpayrate
        NewCandProfile.sCandBillRate = candbillrate
        NewCandProfile.sEmployStatus = employmentstatus
        NewCandProfile.sSplitPercent = splitpercentage

  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:

    ConcatenationProof = objectfirstname & objectlastname & objectclientname
    NewCandProfile = ConcatenationProof
    
    Set NewCandProfile = New CTempCandidate
     NewCandProfile.sFirstName = sFirstName
     NewCandProfile.sLastName = sLastName
     NewCandProfile.sClientName = sClientName
     NewCandProfile.sCandPayRate = sPayRate
     NewCandProfile.sCandBillRate = sBillRate
     NewCandProfile.sEmployStatus = sEmployStatus
     NewCandProfile.sSplitPercent = sSplitPercent
    'Creates an object named NewCandProfile from the class CTempCandidate then sets values in new object_
    'as those returned from InputBox series
    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.


    ' InputBoxNewMarginPlacement Macro
    ' Macro calls multiple inputboxes to assign values for loading into NewCandProfile object,_
    ' created from CTempCandidate Class
    
    
    Sub InputBoxNewMarginPlacement()
    
    'Calls Class CTempCandidate to create New Object comprised of candidate variables in_
    'String, Double and Integer Types
    
    Dim NewCandProfile As New CTempCandidate
    'Declares new Object named NewCandProfile from Class CTempCandidate
    
    Dim sFirstName As String
    Dim sLastName As String
    Dim sClientName As String
    Dim sPayRate As Double
    Dim sBillRate As Double
    Dim sEmployStatus As Double
    Dim sSplitPercent As Integer
    'Declares variables in primary macro to receive variables passed from InputBox_
    'these variables are then passed to object NewCandProfile
    
    sFirstName = candfirstname
    sLastName = candlastname
    sClientName = clientname
    sPayRate = candpayrate
    sBillRate = candbillrate
    sEmployStatus = employmentstatus
    sSplitPercent = splitpercentage
    'InputBox series runs and passes variables into primary macro
    
    Dim ConcatenationProof As String
    Dim objectfirstname As String
    objectfirstname = sFirstName
    Dim objectlastname As String
    objectlastname = sLastName
    Dim objectclientname As String
    objectclientname = sClientName
    'Delares a new variable as a string for naming of NewCandProfile object
    'to test concatenation in MsgBox at end of Sub
    
    ConcatenationProof = objectfirstname & objectlastname & objectclientname
    NewCandProfile = ConcatenationProof
    
    Set NewCandProfile = New CTempCandidate
     NewCandProfile.sFirstName = sFirstName
     NewCandProfile.sLastName = sLastName
     NewCandProfile.sClientName = sClientName
     NewCandProfile.sCandPayRate = sPayRate
     NewCandProfile.sCandBillRate = sBillRate
     NewCandProfile.sEmployStatus = sEmployStatus
     NewCandProfile.sSplitPercent = sSplitPercent
    'Creates an object named NewCandProfile from the class CTempCandidate then sets values in new object_
    'as those returned from InputBox series
    
    'MsgBox ConcatenationProof
    'MsgBox "Candidate First Name: " & NewCandProfile.sFirstName & vbCrLf & "Candidate Last Name: " & NewCandProfile.sLastName & vbCrLf & "Client Name: " & NewCandProfile.sClientName & vbCrLf & "Candidate Pay Rate: " & NewCandProfile.sCandPayRate & vbCrLf & "Candidate Bill Rate: " & NewCandProfile.sCandBillRate & vbCrLf & "Employment Status: " & NewCandProfile.sEmployStatus & vbCrLf & "Referral Split: " & NewCandProfile.sSplitPercent
    'Calls a MsgBox to display results from class
    
    End Sub

  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:

    'Note, Type:=1 is numeric, Type:=2 is string
    Do
        sfirstname = Application.InputBox("Enter First Name", "First Name", Type:=2)
        If Len(sfirstname) > 0 Then Exit Do
    Loop
    Do
        sLastname = Application.InputBox("Enter Last Name", "Last Name", Type:=2)
        If Len(sLastname) > 0 Then Exit Do
    Loop
    Do
        sClientName = Application.InputBox("Enter Client Name", "Client Name", Type:=2)
        If Len(sClientName) > 0 Then Exit Do
    Loop
    Do
        sPayRate = Application.InputBox("Enter Candidate Pay Rate", "Pay Rate", Type:=1)
        If Len(sPayRate) > 0 Then Exit Do
    Loop
    Do
        sBillRate = Application.InputBox("Enter Candidate Billing Rate", "Bill Rate", Type:=1)
        If Len(sBillRate) > 0 Then Exit Do
    Loop
    Do
        sEmployStatus = Application.InputBox("Enter Employment Status", "Status", Type:=2)
        If Len(sEmployStatus) > 0 Then Exit Do
    Loop
    Do
        sSplitPercent = Application.InputBox("Enter Split Percentage", "Status", Type:=1)
        If Len(sSplitPercent) > 0 Then Exit Do
    Loop
    
    
    Set NewCandProfile = New CTempCandidate
     NewCandProfile.sfirstname = sfirstname
     NewCandProfile.sLastname = sLastname
     NewCandProfile.sClientName = sClientName
     NewCandProfile.sCandPayRate = sPayRate
     NewCandProfile.sCandBillRate = sBillRate
     NewCandProfile.sEmployStatus = sEmployStatus
     NewCandProfile.sSplitPercent = sSplitPercent
    _________________
    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
    Dim NewCandProfile As New CTempCandidate
    just use
    Dim NewCandProfile As CTempCandidate
    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