Consulting

Results 1 to 14 of 14

Thread: Staff ID Number format

  1. #1

    Staff ID Number format

    Hi Anyone,
    I have a textbox in my userform with the name txtStaffID. what I want is when the form is loaded the textbox txtStaffID to be visible with the Staff ID number.

    Say if I write the Staff ID as 1 in the textbox txtStaffID, I want the number to be S0001.

    When I click the Ok button in my form I want the number to automatically change to S0002 and so on.

    Any help would be greatly appriciated.

    Thanks in advance.

    Cheers
    Fiza

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Me.TextBox1

    .Text = Application.Text(.Text, "\S0000")
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    your code does not seem to work the way as I wanted could you modify the following code so that the number appears as S0001
    Private Sub UserForm_Initialize()
      With Worksheets("CustomerList").Range("F7")
        .Value = .Value + 1
        Me.txtStaffID = Format(.Value, "0000")
      End With
    End Sub

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Not as elegant as something Bob would provide but maybe something like this:

    [VBA]Private Sub UserForm_Initialize()
    Dim snum As Long
    snum = Worksheets("CustomerList").Range("F7").Value + 1
    Me.txtStaffID = "S000" & snum
    Worksheets("CustomerList").Range("F7").Value = Worksheets("CustomerList").Range("F7").Value + 1
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    I got it fixed now. I did replace the "0000" with "\S0000"
    this is how the code is now
    Private Sub UserForm_Initialize()
      With Worksheets("CustomerList").Range("F7")
        .Value = .Value + 1
        Me.txtStaffID = Format(.Value, "\S0000")
      End With
    End Sub
    Anyways, Thanks for the help.
    I do appreciate that.

  6. #6
    The number in the staffID textbox shows a new number each time I load the form and also the number in F7 of the worksheet gets changed along with that. is there a way to modify the code so that the number in the userform gets changed but not in the worksheet "F7".

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [VBA]Private Sub UserForm_Initialize()
    With Worksheets("CustomerList").Range("F7")
    ' .Value = .Value + 1
    Me.txtStaffID = Format(.Value + 1, "\S0000")
    End With
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    I'm sorry with this modification neither the number in the userform nor the referenced number in the worksheet gets changed.

    What im trying to get is each time I load the form the ID number in the form to be changed to the next available number but not the number in the worksheet.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add a CustomDocumentProperty to your workbook and increment it as required

    [vba]
    Private Sub UserForm_Initialize()
    ActiveWorkbook.CustomDocumentProperties(1) = _
    ActiveWorkbook.CustomDocumentProperties(1) + 1
    TextBox1 = ActiveWorkbook.CustomDocumentProperties(1)
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Can you please specify your code in detail. I tried with the following modification. But it does not seem to work
        ActiveWorkbook.CustomDocumentProperties(1) = _
        ActiveWorkbook.CustomDocumentProperties(1) + 1
        txtSatffID = ActiveWorkbook.CustomDocumentProperties(1)

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Did you add a Property to the Workbook properties?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another approach.

    First declare MyID as a public variable in a standard module

    [vba]

    '-------------------------------------------------------------
    Private Sub Workbook_Open()
    '-------------------------------------------------------------
    GetId
    End Sub


    '-------------------------------------------------------------
    Private Sub GetId()
    '-------------------------------------------------------------
    Dim myID As Long

    myID = 1 ' in case it doesn't already exist
    On Error Resume Next
    myID = Evaluate(ThisWorkbook.Names("UniqueId").RefersTo) + 1
    ThisWorkbook.Names.Add Name:="UniqueId", RefersTo:="=" & myID
    End Sub
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    What im trying to get is each time I load the form the ID number in the form to be changed to the next available number but not the number in the worksheet.
    or for the form opening to be counted
    [VBA]Private Sub UserForm_Initialize()
    GetID
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    thanks for the help. I'm happy now

Posting Permissions

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