dcsimg
Results 1 to 7 of 7

Thread: Random Number List Maker V2 - Problem with positive integer textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    58

    Random Number List Maker V2 - Problem with positive integer textbox

    Hi All.

    I have updated to V2. Here is a screen shot and the code that I used. Thanks MarkT for the code to get rid of the preceding comma in the output box.

    I still have one more problem. The "txtMULT.Text textbox" for generating more than one number works properly initially (only makes positive integers with zero NOT allowed) but after clicking the command button to generate say 20 values, then a zero value is allowed in the txtMULT.Text textbox. In other words, the txtMULT_KeyPress(KeyAscii As Integer) stops working properly after generating multiple random values with the cmdGenerateMult_Click() procedure. I'm not sure why this has happened. Hmm, scratching my head.

    Name:  screen.jpg
Views: 1096
Size:  50.1 KB

    The code:

    Code:
    Option Explicit
    
    Private Sub cmdCancel_Click()
    Unload Me
    End Sub
    
    Private Sub cmdClear_Click()
    txtOutput.Text = ""
    txtLB.Text = ""
    txtUB.Text = ""
    txtMULT.Text = ""
    End Sub
    
    Private Sub cmdGenerate_Click()
    Dim LB, UB As Long
    Dim random As Integer
    If txtLB.Text = "" Then
        MsgBox "Enter value for Lower Bound", vbOKOnly + vbInformation, "Missing Value"
        txtLB.SetFocus
    ElseIf txtUB.Text = "" Then
        MsgBox "Enter value for Upper Bound", vbOKOnly + vbInformation, "Missing Value"
        txtUB.SetFocus
    Else
        LB = Val(txtLB.Text)
        UB = Val(txtUB.Text)
        Randomize
        random = Int(Rnd * (UB + 1 - LB) + LB)
        If txtOutput.Text = "" Then
       txtOutput.Text = random
    Else
        txtOutput.Text = txtOutput.Text & " ," & random
    End If
        End If
    End Sub
    
    Private Sub cmdGenerateMult_Click()
    Dim i As Integer
    If txtMULT.Text = "" Then
        MsgBox "Enter a value for the number of numbers generated per click", vbOKOnly + vbInformation, "Missing Value"
        txtMULT.SetFocus
        End If
    For i = 1 To Val(txtMULT.Text)
    cmdGenerate_Click
    Next i
    End Sub
    
    Private Sub Form_Load()
    txtOutput.Text = ""
    End Sub
    
    Private Sub txtLB_KeyPress(KeyAscii As Integer)
    If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8) Then KeyAscii = 0: Beep
    
    End Sub
    
    Private Sub txtUB_KeyPress(KeyAscii As Integer)
    If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8) Then KeyAscii = 0: Beep
    End Sub
    
    Private Sub txtMULT_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 48             ' deal with leading zeros
                If txtMULT.Text = "" Then
                    KeyAscii = 0
                End If
            Case 49 To 57, 8    ' allow numbers and backspaces
            Case Else
                KeyAscii = 0
        End Select
    End Sub
    Thanks for helping out,

    DreamerRandom Number List Maker Updated V2.zip
    Last edited by CreativeDreamer; Mar 8th, 2015 at 05:25 AM. Reason: Update code to V2

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,133

    Re: Random Number List Maker- How to get rid of the preceding comma in output textbox

    Just do a check before adding the number to the textbox. If it's empty just add the number. Otherwise add the comma and then the number.

    Change this line
    Code:
    txtOutput.Text = txtOutput.Text & " ," & random
    to
    Code:
    If txtOutput.Text = "" Then
        txtOutput.Text = random
    Else
        txtOutput.Text = txtOutput.Text & " ," & random
    End If

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    58

    Re: Random Number List Maker V2 - Problem with positive integer textbox

    Thanks MarkT for the code. I tried something like this initially, but my syntax was not correct.

    As mentioned in the edited first post, the positive integer "txtMULT.Text textbox" (where the 69 is in the screen shot) seems to allow a zero value after one click of the cmdGenerateMult_Click() command button. I'm not sure why this happened. After pressing the Clear button, the positive integer "txtMULT.Text textbox" seems to revert back to working properly again. Hmmm, frustrated.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,675

    Re: Random Number List Maker V2 - Problem with positive integer textbox

    p.s. Just saw dilettante and LaVolpe's responses in your other post concerning the use of Val, so that would apply to the code below as well.
    Posted an update in the next post.

    You can restrict to integer and test for your leading 0, negative, remove extraneous non-numeric characters and limit to a maximum value in the Change event.
    You won't need the KeyPress code as the change event will "fix" any bad inputs, even if pasted in by someone trying to bypass the keypress code.
    Code:
    'I Removed the txtMULT_KeyPress Sub since it wasn't needed
    
    Private Sub txtMULT_Change()
      Dim L As Long
      L = Abs(Val(txtMULT.Text)) 'get rid of any negative sign, non-numeric chars
      
      If L = 0 Then              'a leading 0 or not a number (leading non-numeric chars)
        txtMULT.Text = ""
      ElseIf L > 999 Then        'Limit top end
        txtMULT.Text = "999"
        txtMULT.SelStart = Len(txtMULT.Text) 'put I-beam text cursor at end of number
      Else   'removes extraneous characters (leaving only int
        txtMULT.Text = L
        txtMULT.SelStart = Len(txtMULT.Text)
      End If
    End Sub
    You can keep the txtMULT_KeyPress sub it you want as it prefilters keystrokes. If someone types a non-numeric character, you might see it show briefly (not usually noticeable on my machine) before being removed by the change event, which is Ok with me, but your choice.
    Last edited by passel; Mar 8th, 2015 at 07:41 AM.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,675

    Re: Random Number List Maker V2 - Problem with positive integer textbox

    To handle dilettante's and LaVolpe's concerns, I would use a function to replace Val that will still sort of act like Val and return 0 if it couldn't convert the number. You would modify the line in the previous sub to use the function rather than Val.
    Change
    L = Abs(Val(txtMULT.Text)) 'get rid of any negative sign, non-numeric chars
    to
    L = Abs(txtToLong(txtMULT.Text))

    Code:
    Function txtToLong(txt As String) As Long
    On Error GoTo tryAFix
      txtToLong = CLng(txt)  'if all goes well, we'll leave
    Exit Function
    
    tryAFix:                'didn't like the number so
    On Error GoTo zeroIt
      If Len(txt) > 1 Then  'see if only the last character was a problem
        txtToLong = CLng(Left$(txt, Len(txt) - 1)) 'if all goes well, we'll leave
      Else 'we only have one character then return 0
        txtToLong = 0
      End If
    Exit Function
    
    zeroIt:  'removing the last character didn't fix it so return 0
      txtToLong = 0
    End Function

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Random Number List Maker V2 - Problem with positive integer textbox

    Quote Originally Posted by passel View Post
    Code:
    On Error GoTo tryAFix
    . . .
    tryAFix:                'didn't like the number so
    On Error GoTo zeroIt
    . . .
    zeroIt:  'removing the last character didn't fix it so return 0
    . . .
    As discussed in this thread a couple of weeks ago, any error within an active error handler is fatal, so it is a good idea to "disable" the current error handler first before activating a new one.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,675

    Re: Random Number List Maker V2 - Problem with positive integer textbox

    Thanks Bonnie.
    I thought I might need to disable the handler, and thought I tested it but obviously didn't generate a case that caused the nested error condition.
    When I did generate the condition, then it did cause the exception rather than handling it as you would expect.
    I remember reading that thread but guess it didn't sink in enough to recall it when writing the code.
    So, if using that function, add the additional line to disable the current handler before setting the second.
    Code:
    Function txtToLong(txt As String) As Long
    On Error GoTo tryAFix
      txtToLong = CLng(txt)  'if all goes well, we'll leave
    Exit Function
    
    tryAFix:                'didn't like the number so
    On Error GoTo -1        'reset the current error handler
    On Error GoTo zeroIt    'set a new error handler
      If Len(txt) > 1 Then  'see if only the last character was a problem
        txtToLong = CLng(Left$(txt, Len(txt) - 1)) 'if all goes well, we'll leave
      Else     'we only have one character then return 0
        txtToLong = 0
      End If
    Exit Function
    
    zeroIt:  'removing the last character didn't fix it so return 0
      txtToLong = 0
    End Function

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width