Results 1 to 10 of 10

Thread: should be simple.. how to make a variable equa a lnumeric value typed into a textbox?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    should be simple.. how to make a variable equa a lnumeric value typed into a textbox?

    Not used VB6 for a while, and now I just can't remember how to do this...
    I have a textbox on form1, I want to take a user inputted value (3 digits usually), and make a 'double' variable take it's value, using 'Val'. But I've fallen at the first hurdle... I've forgotten how to make a variable take a value inputted to a text box by a user.
    What is the syntax for this?

    The textbox appears as 'Textbox2' on the form while I'm writing code.

    The sub is called 'Private Sub pictureHeight_Change()'

    If IsNumeric(TextBox2.Text) Then picinput$ = TextBox2.Text
    PicHeight_Px = Val(picinput$)

    was my best effort

    Thanks.
    Last edited by moorea21; Mar 18th, 2017 at 09:32 AM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    Do you need to save the value in picinput$ for another purpose?
    You can just do a Val on the TextBox text, although once you've verified it is numeric, then using CDbl is technically more efficient.
    Code:
    If IsNumeric(TextBox2.Text) Then 
      PicHeight_Px = Val(TextBox2.Text)
    End If
    Last edited by passel; Mar 18th, 2017 at 10:04 AM.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    Not only more efficient, CDbl() is locale-aware unlike the crusty old Val() function.

    And there isn't much point to your temporary picinput$ the way you are using it.

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    It seems I had to substitute pictureHeight.Text for TextBox2.Text...


    If IsNumeric(pictureHeight.Text) Then
    PicHeight_Px = Val(pictureHeight.Text)
    End If

    Only trouble is, as soon as I type just 1 digit into the textbox, it immediately starts running this sub.

    How do I make sure it only does this when the user has finished typing in the new value into the textbox?

    Thanks

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    Well, how do you know WHEN the user is 'done'? You said that 'usually' it is 3 digits, which means to me it could be 1, 2, 4 or more as well. One way to do that is when the user 'finishes', he can hit the enter key (you can figure that one out, I am sure). Or have a cmd button next to the textbox and the user would click it when 'done' entering whatever number of digits he wants.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    Just a personal preference.

    When I develop forms that require user input via textboxes, I generally create a separate procedure that validates all the textbox values are valid and filled in, as needed. In other words, unless absolutely necessary, I don't validate textbox input as the input occurs or when the textbox loses focus. This validation routine would be called when the user selects an action that uses the input. If validation fails, then the user is informed via a msgbox that required information is missing or invalid.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    I had considered using an 'ok' or 'enter' button, not sure whether to or not yet. Validation will be something I'll add when I've got my head around the basic idea of adding the values. The textbox 'losing focus' seems like a good way of signalling the end of user input, and there's another button that has to be pressed immediately afterwards anyway, in most cases.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    What do you mean by:

    when I've got my head around the basic idea of adding the values.
    Still need help with that...and what are you adding to what?

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    I meant user behaviour when adding values to this particular text box.

    The fact that the bit of code I initially had trouble with gets called whenever any digit is changed in the text box turns out not to matter, so I left it as per Passel's example, and added a dummy 'ok' button next to it, which just makes the user feel more certain that the program has taken notice of them! A message box that pops up whenever a non numerical value is entered takes care of the odd typo.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: should be simple.. how to make a variable equa a lnumeric value typed into a text

    Note that if numerical input is the main reason for this thread, you may want to search the forum for similar solutions. There are several ways to ensure user can only enter numerical data in a textbox including during input, post-input, using masked edit control, and using APIs.

    Key search terms: textbox numbers only
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width