+ Reply to Thread
Results 1 to 9 of 9

Validating vb user form text box - help!

  1. #1
    Registered User
    Join Date
    11-27-2008
    Location
    Bristol
    Posts
    13

    Validating vb user form text box - help!

    Hi,

    I have a nifty little userform designed to retrieve & hold six values in textboxes taken from a worksheet based on an input that the user enters into the first textbox on the form ('uniqueref') which is then searched for in column A. The code is as follows:

    Private Sub uniqueref_AfterUpdate()
    lastrow = Range("A65536").End(xlUp).Row
    findrow = Range("A1:A" & lastrow).Find(uniqueref.Value, Cells(1, 1)).Row
    For I = 1 To 6
    UserForm1.Controls.Item("TextBox" & CStr(I)).Value = Cells(findrow, I).Value
    Next I
    End Sub

    The userform also has the ability to rewrite any amended values on the form back to the worksheet using:

    Private Sub Ok_Click()
    For I = 1 To 6
    Cells(findrow, I).Value = UserForm1.Controls.Item("TextBox" & CStr(I)).Value
    UserForm1.Controls.Item("TextBox" & CStr(I)).Value = ""
    Next I
    End Sub

    This all works well however I want to adapt the code so that:
    a) the form will only accept a numeric value in the 'uniqueref' textbox on the userform
    b) the form will only accept a value that exists in column A of the workheet
    c) if the above occurs, a msgbox will be displayed saying either "must be a number!" or "number does not exist!"
    d) also, that the uniqueref field will be cleared and focus reset so that the user can attempt a new (valid) input

    I'm guessing I need a Private Sub uniqueref_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean) routine but I'm a bit out of my depth here . Can anyone help please?

    Thanks in anticipation of assistance.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Validating vb user form text box - help!

    Hi, Beam_me_up,

    a) check any entry into the box to be numeric
    Please Login or Register  to view this content.
    Please try the following amendment to the code (untested):
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    11-27-2008
    Location
    Bristol
    Posts
    13

    Re: Validating vb user form text box - help!

    Hi Holger, thanks for the response.
    Both work great, except the Set Focus. Not sure why.
    Any ideas?

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Validating vb user form text box - help!

    Hi, Beam_me_up,

    except the Set Focus. Not sure why.
    Iīm afraid I donīt see a mistake on the spot. If there is no other code that may interfere: I will have to build an example, look at it, hopefully sort it out and come back with a solution.

    Ciao,
    Holger

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Validating vb user form text box - help!

    Hi, Beam_me_up,

    please apply uniqueref_Exit (starts on leaving the textbox and may be cancelled) instead of uniqueref_AfterUpdate (another control has the focus):
    Please Login or Register  to view this content.
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    11-27-2008
    Location
    Bristol
    Posts
    13

    Re: Validating vb user form text box - help!

    It worked! Thanks!!!

  7. #7
    Registered User
    Join Date
    11-27-2008
    Location
    Bristol
    Posts
    13

    Re: Validating vb user form text box - help!

    Sorry I solved this too soon. There's a small problem, I've got ten records listed unique ref 1-10. I don't know why, but the data results for 10 are showing when the uniqueref entered is both 1 and 10.

    2-9 are all correct. It's just 1 (the first record) that's a problem. 1 is on row 16, 10 on row 7 (2-9 in between). There's no other values in column A. Any ideas??

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Validating vb user form text box - help!

    Hi, Beam_me_up,

    strange I like to keep my numbers in ascending order.

    Please Login or Register  to view this content.
    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    11-27-2008
    Location
    Bristol
    Posts
    13

    Re: Validating vb user form text box - help!

    You beauty !!! :-)))

+ 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